为什么我在printf上遇到访问冲突?

时间:2016-11-24 19:46:10

标签: c++ printf access-violation

下面的调用堆栈:     ucrtbased.dll!01905fcc()未知     [下面的框架可能不正确和/或缺失,没有为ucrtbased.dll加载符号]     [外部代码]     basketball.exe!_vfprintf_l(_iobuf * const _Stream,const char * const _Format,__ crt_locale_pointers * const _Locale,char * _ArgList)Line 639 C ++     basketball.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;

}

1 个答案:

答案 0 :(得分:0)

在审查了其他帖子之后,如果你使用printf,那么printf显然会起作用(“按照这个命令:%s”,myString.c_str()); 。 .c_str()允许它以我最初尝试的方式使用。感谢您的帮助和链接。我早些时候错过了那篇文章。 :)

相关问题