If's and Else's not working

HELP!! I have been at this for almost 2 hours.

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
#include <cstdlib>
#include <iostream>
#ifdef WIN32
#include <windows.h>
#endif
#include <clearscreen.h>

using namespace std;

int main()
{
    int selectionFirst;
    int selectionPrologue;
    bool definitionPone;
    cout << "\t\t\tHonors World History study helper v.1\n\n";
    cout << "This program will ask you to select a chapter, then a section in that chapter,  and then show each definition for five (5) seconds, then will ask you to give   the vocabulary word.";
    cout << "\n\nChoose the Chapter to study:\n\n";
    cout << "1.) Prologue\n\n";
    cout << "2.) Chapter 1";
    cout << "\n\n";
    cin >> selectionFirst;
    
    if (selectionFirst == 1)
    {
    ClearScreen();
    cout << "\nPlease choose the section you would like to study: \n\n";
    cout << "1.) Section 1\n\n";
    cin >> selectionPrologue;
        if (selectionPrologue == 1)
        {
        ClearScreen();
        cout << "A system for controlling society is... ";
        cin >> definitionPone;
            if(definitionPone == 'Government' or definitionPone == 'government')
            {
            cout << "great. Moving on.\n\n";
            }
            else
            {
            cout << "incorrect. Moving on.\n\n";
            }
        ClearScreen();
        cout << "Form of government in which citizens rule and make laws directl rather than through representatives.";
        }
    }    
    system("PAUSE");
    return 0;
}


Now don't mind the System("Pause") that makes it easier on me (for the moment).

My problem is right here:

1
2
3
4
5
6
7
8
9
10
cout << "A system for controlling society is... ";
        cin >> definitionPone;
            if(definitionPone == 'Government' or definitionPone == 'government')
            {
            cout << "great. Moving on.\n\n";
            }
            else
            {
            cout << "incorrect. Moving on.\n\n";
            }


It is totally bypassing the if statement and going to the else. I took out the whole else statement for a test and the cout never showed up. Can you guys help?

bool definitionPone;

I think boolean only accepts yes / no and not a string for your if statement.
Hi!

I think something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 string definitionPone;


 ...
 cout << "A system for controlling society is... ";
 getline(cin, definitionPone);

 if (definitionPone.compare("Government") == 0) 
 {
  cout << "great. Moving on.\n\n";
 }
 else 
 {
  cout << "incorrect. Moving on.\n\n";
 }



related links:

http://www.cplusplus.com/reference/string/getline/
http://www.cplusplus.com/reference/string/string/compare/

bye
your definitionPone variable is a bool type variable, so you can not do
cin<<definitionPone

this will solve your problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 

string definitionPone;

cout << "A system for controlling society is... ";
        cin >> definitionPone;
            if(definitionPone == "Government" || definitionPone == "government")
            {
            cout << "great. Moving on.\n\n";
            }

            else
            {
            cout << "incorrect. Moving on.\n\n";
            }
hunter, though all previous posts were correct in the points they raised, none of them was complete.

As all of them said, you won't get the result you expect, if you try to read a string into boolean, so do as said and use string definitionPone;. Your string comparisons were incorrect too, though last posters fixed them, they never said anything about them. Use double quotes for strings and apostrophes for characters.

I also have some recommendations about doing a quiz program. I would separate the program from the questions (you can store questions in a simple XML file), because it would be possible to modify questions w/o recompiling the program and it could be reused with different questions later.
Last edited on
that's not the way to compare strings, it would be like this:

1
2
3
4
5
6
7
8
9
10
#include<string>
if(strcmp(definitionPone,"Government")==0|| strcmp(definitionPone,"government")==0)
            {
            cout << "great. Moving on.\n\n";
            }

            else
            {
            cout << "incorrect. Moving on.\n\n";
            }


you must use the string compare for that

hope it helps
that's not the way to compare strings

Why not? std::string class has overloaded comparision operators
code like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

int main()
{
    std::string text;
    std::cin >> text;   //or better getline
    
    if (text == "yes")
       /* do something */
    
   return 0;
}


sorry for offtopic
Last edited on
idk about your coding but line 34 has the word or in it... could that be the problem... or did c++ make || == or??
please tell

oh yah bool type is true false...

so
change it string type...

#include <iostream>
#include <string>

int main()
{
string answer;

cout << "Enter answer: ";
getline(cin, answer); // or cin >> answer;

if (answer == "Government" || "government")
statements....
Last edited on
C++ has both || and or with the same meaning.
Notice that you can't use that operator like this: answer == "Government" || "government"
the right way of using it is answer == "Government" || answer == "government"
|| and or does the same thing... Some compliers implement oras a macro which expands to ||.
There are a bunch of other alternative operators :
http://www.cplusplus.com/reference/clibrary/ciso646/
WHOA!

Thanks soo much guys (and gals?) You have been most helpful. sikac's post fixed my problem :D. Thanks again. I'll be sure to let you know of the final result ;)

/* Edit */

Okay, it works but i just tried to add another definition and its skippping the you are right message.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
string defintionPtwo;
        ClearScreen();
        cout << "Form of government in which citizens rule and make laws directly rather than    through representatives is... ";
        cin >> defintionPtwo;
            if (defintionPtwo == "Direct Democracy" or defintionPtwo == "direct democracy")
            {
            cout << "Thats correct! Moving on.";
                 int seconds = 1;
                      #ifdef WIN32
                 Sleep(seconds*3000);
                      #else
                 sleep(seconds);
                      #endif
            }
            else
            {
            cout << "Sorry, you are wrong. The correct answer was Direct democracy. Moving on.";
            int seconds = 1;
        #ifdef WIN32
           Sleep(seconds*3000);
        #else
           sleep(seconds);
        #endif
            }


It goes right to the you are wrong even though the answer was right. Any ideas?
Last edited on
The cin reads "Direct Democracy" as two individual string! So the statement

cin >> defintionPtwo;

yields just "Direct".

You can check this if you print out string defintionPtwo with the help of cout.
use getline( cin, definitionPtwo ); to get an entire line of input in your string.
in your 5 line of code, you can not use "or" it has to be || ..
Topic archived. No new replies allowed.