Comparing String Arrays

Hey...
I was writing this code as a little project to help me study the presidents, (right now I only coded for the first ten). I never get "if (flag)" to be true.
I had it print the contents of InputString and it is fine. For some reason it won't properly compare the strings in lines 34-49. Thanks guys...




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
57
58
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <string>

using namespace std;
void PrintNames();

int PresidentGuess;

const string President[10]={"George-Washington","Thomas-Jefferson","James-Madison","James-Monroe",
    "John-Quincy-Adams","Andrew-Jackson","Martin-Van-Buren","William-Henry-Harrison","John-Tyler","James-K.-Polk"};
    
int main(int argc, char *argv[])
{
    char input;
    cout<< "Would you like to see the first ten Presidents? y/n\n";
    cin>> input;
    switch(input){
                  case 'y': 
    PrintNames(); // display names
    break;

    
                  case 'n':
    break;
                  default:
    cout<<" Invalid answer. Moving on.";
}
while(1){
         
srand ( time(NULL) );

PresidentGuess= rand() % 10 +1;// Generate random number
string StringInput;
cout<< "What was President number ";
cout<< PresidentGuess;
cout<<"?(No spaces and a dash between any name/initial)\n";
cin>>StringInput;
bool flag(President[PresidentGuess] == StringInput);
if (flag){ 
      cout<< "Nice!!!\n";
}
else{
     cout<<"Sorry wrong answer\n";
}

system("PAUSE");
    return EXIT_SUCCESS;
}
}

void PrintNames(){
for(int i=0; i!=10; i++){
             cout<< President[i];
             cout<< "\n";
             }
             }
Array indices start at zero so President[0] is the first president.
I understand that... I tried Presidents[PresidentGuess-1]... I thought that would fix it, but It still didn't work...
Seems to work for me. Are you sure you spelled correctly? Correct upper and lower case?
Oh wait.... I tried it again and it worked... Thanks! but one more thing, ff I have my strings include a space it doesn't work. I think the space is terminating the string early, but I don't know how I would fix it besides adding dashes.... any ideas?
instead of cin>>StringInput; try getline(cin, StringInput);
Thanks I'll try that...
Nope It won't work it immediately goes negative and displays the fail message....
Last edited on
Topic archived. No new replies allowed.