Issues with char's and if and else

Ok well, for a project I have to make a program that asks you questions. It's very simple. I ran into a problem; I made a char named "country" and I "made" it so it will ask "Where do you live" and if you say "Costa Rica" it will say "You live in Costa Rica! I do too", and if you say anything else it will say "You live in _____! I live in Costa Rica". Boom. simple right. nope. No matter what I type it will trigger "else". Please help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <stdio.h>
#include <string>
#ifdef __WIN32__
#include <windows.h>
#else
#include <unistd.h>
#endif
using namespace std;

int main(void)
{
    cout<<"Hi!\n";
    Sleep(300);
    string name;
    cout<<"What is Your name?\n";
    getline(cin, name);
    cout<<"Well hello "<<name<<" nice to meet you"<<endl;
    Sleep(800);
    string age;
    cout<<"How old are you?";
    cout<<endl;
    getline(cin, age);
    Sleep(800);
    cout<<"Oh you are "<<age<<" That's neat\n";
    cout<<"Let me ask you a question\n";
    Sleep(400);
    int MQ;
    cout<<"What's 3 plus 5?\n";
    cin>>MQ;
if (MQ == 8)
{
    cout<<"Your'e right! "<<MQ<<" Is the right answer!\n";
}
else if (MQ > 8)
 {
    cout<<MQ<<" is too high!\n";
 }
else
{
    cout<<MQ<<" is too low\n";
}

    char country[256];
    cout<<"OK well, Where do you live?\n";
    cin>>country;
if (country == "Costa Rica")
{
    cout<<"You live in "<<country<<", I do too!\n";
}
else
{
    cout<<"You live in "<<country<<", I live in Costa Rica\n";
}
}
Line 47: char arrays don't support the equality operator (==). Use std::string instread.
How would std::string be used in the "if" piece (i'm new to this :3)
Nevermind
1
2
3
4
5
6
    string country;
    cout<<"OK well, Where do you live?\n";
    cin>>country;
    if (country == "Costa Rica")
    {   cout<<"You live in "<<country<<", I do too!\n";
    }

Nope same result, I'm get "You live in Costa Rica! I live in Costa Rica" which is the "else" piece.
Does your capitialization match? std::string compare is an exact match.
cin by itself is just getting "Costa", it's not reading Rica after the space character. Try using the getline, and put in the ignore.

1
2
    cin.ignore();
    getline(cin,country);
Thank you so much guys, I got it too work!
Topic archived. No new replies allowed.