調用堆棧: ucrtbased.dll 01905fcc()未知 [下面的幀可能是不正確的和/或缺失,加載ucrtbased.dll無符號] [外部代碼] basketball.exe _vfprintf_l(_iobuf * const的! _Stream,爲const char * const的_format,__crt_locale_pointers * const的_locale,字符* _ArgList)639線C++ basketball.exe!的printf(爲const char * const的_format,...),954線C++爲什麼我在printf訪問衝突?下面
basketball.exe! main()Line 81 C++ [External Code]
//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? – LogicStuff
那麼,今天學到了一些新東西。在字符串中使用printf時存在未定義的行爲,並且您可能爲缺少的大小寫塊(6,7,8等)插入值。改用std :: cout。 – 2016-11-24 19:56:14
顯然,用cout作品取代了所有的印刷品。但爲什麼?你能不能將cin/cout與scanf/printf混合?下面的工作正確。 cout <<「Player name:」<< playername << endl; cout <<「Position:」<< position << endl; \t cout <<「Height:」<< height <<「Weight:」<< weight << endl; <<「家鄉:」<<故鄉<< endl; cout <<「Class:」<< Class << endl; – Rabbit84