【发布时间】:2017-12-30 18:36:28
【问题描述】:
我这里有一个代码:(忽略friends_name,没关系)
char friend_sex [1];
cout << "Please enter m if your friend is male or f if your friend is female\n";
cin >> friend_sex;
if (friend_sex == "m") cout << "If you see " << friends_name << "! please ask him to call me\n";
if (friend_sex == "f") cout << "If you see " << friends_name << "! please ask her to call me\n";
当我运行它时,它会说
运行时检查失败 #2 - 变量“friend_sex”周围的堆栈已损坏。
【问题讨论】:
-
我敢打赌,因为您有一个数组,流操作员将其设为字符串,而不是字符。
-
由于
friend_sex衰减为指向char类型变量的指针,而"m"是指向char类型常量的指针,因此这两个指针几乎是不可想象的价值相当。常量和变量怎么可能在同一个地址?
标签: c++ if-statement char cin