【问题标题】:Why am i getting access violation at printf? [duplicate]为什么我在 printf 中遇到访问冲突? [复制]
【发布时间】:2017-04-09 04:25:31
【问题描述】:

调用堆栈如下: ucrtbased.dll!01905fcc() 未知 [下面的帧可能不正确和/或丢失,没有为 ucrtbased.dll 加载符号] [外部代码] 篮球.exe!_vfprintf_l(_iobuf * const _Stream, const char * const _Format, __crt_locale_pointers * const _Locale, char * _ArgList) 第 639 行 C++ 篮球.exe!printf(const char * const _Format, ...) 第 954 行 C++

basketball.exe!main() 第 81 行 C++ [外部代码]

//I keep getting an access violation.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int x;
    string playername, hometown,position,height,Class;
    int weight;

    cout << "Enter player number: ";
    cin >> x;

    switch(x){
    case 0: {
        playername = "Brandon Sampson";
        position = "Guard";
        weight = 193;
        hometown = "Baton Rouge, LA";
        height = "6-5";
        Class = "Sophomore";
        break;
    }
    case 1: {
        playername = "Dupo Reath";
        position = "Forward";
        weight = 235;
        hometown = "Perth, Australia";
        height = "6-10";
        Class = "Junior";
        break;
    }
    case 2: {
        playername = "Antonio Blakeney";
        position = "Guard";
        weight = 197;
        hometown = "Sarasota, Fla";
        height = "6-4";
        Class = "Sophomore";
        break;
    }
    case 3: {
        playername = "Elbert Robinton III";
        position = "Center";
        weight = 290;
        hometown = "Garland, Texas";
        height = "7-1";
        Class = "Junior";
        break;
    }
    case 4: {
        playername = "Skylar Mays";
        position = "Guard";
        weight = 205;
        hometown = "Baton Rouge, La";
        height = "6-4";
        Class = "Junior";
        break;
            }
    case 5: {
        playername = "Kieran Hayward";
        position = "Guard";
        weight = 195;
        hometown = "Sydney, Australia";
        height = "6-4";
        Class = "Freshman";
        break;
            }
    case 10: {
        playername = "Branden Jenkins";
        position = "Guard";
        weight = 180;
        hometown = "Maywood, Ill";
        height = "6-4";
        Class = "Junior";
        break;
    }

    }
            printf("Name:  %s", playername);
            printf("Position: %s\n", position);
            printf("Height: %s", height);
            printf(" Weight: %d", weight);
            printf("Hometown: %s\n", hometown);
            printf("Class: %s\n", Class);

            return 0;

}

【问题讨论】:

  • 为什么........... printf?
  • 嗯,今天学到了一些新东西。将 printf 与字符串一起使用时存在未定义的行为,并且您可能正在为缺少的 case 块(6、7、8 等)插入值。请改用 std::cout。
  • 显然将所有 print f 替换为 cout 作品。但为什么?你不能把 cin/cout 和 scanf/printf 混合起来吗?下面工作正常。 cout
  • 被开启的案例是球员号码。这是一个不完整的 LSU 篮球名册计划(简单)。基本上,您输入一个球员号码,它会打印出拥有该号码的球员的所有信息。基本上,整个程序通过使用 cout 更改所有 printf 实例来工作。那么,在使用 case 语句来决定字符串值时,您可以不使用 printf 吗?还是打印字符串时不能使用 printf?
  • @Rabbit84 printf 是一个 C 函数。 C 不知道 std::string 是什么。这会崩溃(在这种情况下表现为崩溃的未定义行为),因为%s 告诉printf 您将提供一个指向以空字符结尾的字符数组的指针。 std::string 不仅仅是一个字符数组。基本上,您将方钉插入圆孔中,printf 太老太笨了,无法分辨两者之间的区别。

标签: c++ printf access-violation


【解决方案1】:

在查看了某人链接的其他帖子后,如果您使用 printf("Follow this command: %s", myString.c_str()); 显然可以使用 printf . .c_str() 允许以我最初尝试的方式使用它。感谢您的帮助和链接。我不知何故错过了那个帖子。 :)

【讨论】:

  • 感谢@Fang 提供另一篇文章的链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多