Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

haakon Guttormsen
haakon Guttormsen
2,157 Points

c++ program not running as it should. Cant see the problem.

This little (part of) program should prompt the user to type an 'f' for female, and 'm' for male. I use two if statements to make this happen. The program runs, but not as it should.

haakon Guttormsen
haakon Guttormsen
2,157 Points

Here is the program:

char friend_sex = 0;
cout<< "Please enter a 'm' For male and 'f' For female (followed by 'enter'\n";
cin>> friend_sex;
if (friend_sex == m)
    {
        cout<< "If you see "<<name_friend<< " ask him to call me\n";
        }
if (friend_sex == f)
    {
        cout<< "If you see " <<name_friend<< " have her call me\n";
        }
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Unfortunately, it's impossible for us to see the problem as we can't see your code. You can post your code here and someone might be able to help, but please note that you are asking about code in a language that isn't taught here currently. C# and C++ are not the same language.

I will be moving this question to the "General Discussion" section as it does not directly pertain to C# or any of the Treehouse curriculum.

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! According to the code you've posted, the variable name_friend is undefined. I can only assume that you defined it elsewhere. You have declared the friend_sex as a character but are assigning an integer to it which is... odd, at best. Finally, you are trying to compare it to the characters 'f' and 'm', but unless f and m are variables elsewhere in your code that are holding the characters 'f' and 'm', then this will fail.

I took the liberty of reworking your code a bit and I declared at least one variable which is not shown in your original code:

int main()
{
    std::string name_friend = "Jennifer";
    char friend_sex;
    cout << "Please enter a 'm' For male and 'f' For female (followed by 'enter'\n";
    cin >> friend_sex;
    if (friend_sex == 'm')
    {
        cout << "If you see " << name_friend << " ask him to call me\n";
    }
    if (friend_sex == 'f')
    {
        cout << "If you see " << name_friend << " have her call me\n";
    }
    cin >> name_friend;
    return 0;
}

Note that I put an extra cin at the bottom which essentially does nothing other than make the console pause until you type something so you can actually see what was printed. Hope this helps! :sparkles: