Fresh Blue Fresh Blue - 3 years ago 102
C++ Question

C++ If Statement Run-Time Check Failure #2

I have a code here:(ignore the

friends_name
, that's fine)

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";


When I run it it says


Run-Time check failure #2 - stack around the variable 'friend_sex' was corrupted.

Answer Source

Two solutions:

  1. Change char friend_sex[1] to char friend_sex, and "m" to 'm', "f" to 'f'.
  2. Change char friend_sex[1] to char friend_sex[2]. And change the == judgement to strcmp.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download