Need Help Please

Hey I'm new with C++ trying to learn and fully understand if statements and how to check user input.

Trying to achieve:

Have user only able to enter answer for corresponding question, e.g.

For question 1 the answer should be any variant of the word "blue", however if I enter "blue" for the question 2 I still get question correct but this should be wrong and the correct answer should be any variant of the word "glass". Apologises in advance if it not up to a professional level in terms of layout.

Any help is appreciated.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>


using namespace std;

int main ()

{

// Greetings and Instructions

    string greeting="Hey there you!!!\n\nYou ready to get some points\n\n ";
    string instructions="Please enter your answer once you've read the question\n\n";
    string correctAnswer="Well Done!! You just got 10 POINTS!!\n\n";
    string wrongAnswer="Try again!!\n";

//Declaring an array (containers) of strings
	
    string  questions[4];   
	
//Defining individual questions to each array string (containers)	
    
    questions[1]="What colour is the sky?\n";
    questions[2]="What material is a window made out of?\n";
 

//Define some varibles to store

          const int addTenToScore = 10;
		int playerScore = 0;
		
		string answer = "";

//Welcome player and give instructions

	    cout << greeting << endl;			//start asking questions
            cout << instructions << endl;		//show user possible answers


for (int i=1; i<3; i++) 
{

		cout << questions[i]     // Shows next question to user
		getline(cin, answer);	 // Get users input 


		
// Check to see if correct answer is inputted by user

if  ((i=='1') && (answer == "Blue") || (answer == "BLUE") || (answer == "blue"))
	
	{
		cout<<correctAnswer;
		playerScore = addTenToScore;
		cout<<playerScore<<"\n\n";
	}


else if ((i=='2') && (answer == "Glass") || (answer == "GLASS") || (answer == "glass"))

	{
		cout<<correctAnswer;
		playerScore = addTenToScore;
		cout<<playerScore<<"\n\n";
	}


else

	{
		cout<<wrongAnswer;
		questions[i] = questions[i];
	}

}

system ("PAUSE");

// END
    return 0;
}
Looks like some of your if statement logic is not completely sound.

^ = &&
v = ||
Remember that if (A ^ b) v C is true if C is true.

Try changing the if statement to check the i value every single time you check the
color value.

For instance

 
if (  (i == 1 && answer == "Blue") || (i == 1 && answer == "BLUE")  || (i == 1 && answer == "blue"))


The same applies for your other if statements

erock
Thanks very much appreciated erock

Worked first time as intended. :))
No problem!

Glad I could help!

erock
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <string>
#include <cctype> // for std::tolower()

// using namespace std; '' ideally avoid; just type in std:: as required
// http://www.parashift.com/c++-faq/using-namespace-std.html

int main ()
{

    // Greetings and Instructions
    // const added
    const std::string greeting="Hey there you!!!\n\nYou ready to get some points\n\n ";
    const std::string instructions="Please enter your answer once you've read the question\n\n";
    const std::string correctAnswer="Well Done!! You just got 10 POINTS!!\n\n";
    const std::string wrongAnswer="Try again!!\n";

/*
//Declaring an array (containers) of std::strings

    std::string  questions[4];

//Defining individual questions to each array std::string (containers)

    questions[1]="What colour is the sky?\n";
    questions[2]="What material is a window made out of?\n";
*/
    // http://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants
    const int NUM_QUESTIONS = 4 ; // avoid magic numbers

    // we can initialize the array when we define it
    const std::string questions[NUM_QUESTIONS] =
    {
        "What colour is the sky?\n",
        "What material is the pane of a window made out of?\n",
        "Does a dog have a Buddha-nature or not?\n",
        "Who thinks that 'least worst option' is English?\n"
    };

    // we can also define an array containing the answers
    const std::string correct_answers[NUM_QUESTIONS] = { "blue", "glass", "mu", "americans" };

    //Define some varibles to store
    const int addTenToScore = 10; // const, that is good
    int playerScore = 0;

    std::string answer ; // = "" ; // by default a string i\s initialized as an empty string

    //Welcome player and give instructions
    // http://www.parashift.com/c++-faq/endl-vs-slash-n.html
	std::cout << greeting << '\n' /*endl;*/ //start asking questions
    /* cout */ << instructions << '\n' ; /*endl;*/ //show user possible answers


    for( int i = 0 ; i < NUM_QUESTIONS ; ++i )
    {
         std::cout << questions[i] ;   // Shows next question to user
         std::getline( std::cin, answer );	 // Get users input

         // Check to see if correct answer is inputted by user

         // convert answer to all lower case
         // http://en.cppreference.com/w/cpp/string/byte/tolower
         for( unsigned int i = 0 ; i < answer.size() ; ++i )
             answer[i] = std::tolower( answer[i] ) ;

         if( answer == correct_answers[i] )
         {
             std::cout << correctAnswer ;
             playerScore += /*=*/ addTenToScore;
             std::cout << "score till now: " <<  playerScore << '\n' ;
         }

         else std::cout << wrongAnswer ;
    }

    // there is an implicit return 0 ; at the end of main
}
Last edited on
@ JLBorges

Wow thank-you for this, great input and this helps me out loads on understanding and getting a feel for logical thinking.

Well guess I'm off to edit my code. And do some reading via the links you left. :)
Last edited on
Topic archived. No new replies allowed.