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.
#include <iostream>
#include <string>
usingnamespace 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
constint 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";
}
elseif ((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;
}
#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.htmlint 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_constantsconstint 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
constint 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/tolowerfor( unsignedint 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
}