How do I remove zeros between my outputs? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
bool getAnswer(int a);
int main ()
{
string questions[5] = {"CPU stands for 'Central Processing Unit'", "RAM stands for 'Reserved Access Main'", "HDD stands for 'Hard Drive Doubler'", "SSD stands for 'Solid State Drive'", "CPP stands for 'C Programming Plus'"};
for (int i = 0; i < 5; i++)
{
cout << "Question " << ++i << " \n";
i--;
cout << questions[i] << "\n";
cout << getAnswer(i) << endl;
}
}
bool getAnswer(int a)
{
bool answer[5] = {true, false, false, true, false};
bool user[5];
string input;
cout << "Answer " << ++a << " \n";
a--;
cout << "Enter a true or false answer: " << "\n";
cin >> input;
while (input != "T" && input != "t" && input != "F" && input != "f" && input != "True" && input != "true" && input != "False" && input != "false")
{
cout << "Invalid entry, try again!\nEnter a true or false answer: " << "\n";
cin >> input;
}
if (input == "T" || input == "t" || input == "True" || input == "true")
{
user[a] = true;
}
else if (input == "F" || input == "f" || input == "False" || input == "false")
{
user[a] = false;
}
if (answer[a] == user[a])
{
cout << "Correct!\n";
}
else if (answer[a] != user[a])
{
cout << "Incorrect!\n";
}
}
In the output between the correct/incorrect and next question, I keep getting a "0" in-between. How do i remove them.
Ex:
Question 1
CPU stands for 'Central Processing Unit'
Answer 1
Enter a true or false answer:
f
Incorrect!
0
Question 2
RAM stands for 'Reserved Access Main'
Answer 2
Enter a true or false answer:
t
Incorrect!
0

Just remove cout << "correct" and cout << "incorrect" and change the return type from bool to std::string.
From:
bool getAnswer(int a) {
...
if (answer[a] == user[a])
cout << "Correct!\n";
else if (answer[a] != user[a])
cout << "Incorrect!\n";
}
To:
std::string getAnswer(int a) {
...
if (answer[a] == user[a])
return "Correct!\n";
else if (answer[a] != user[a])
return "Incorrect!\n";
}
What happens in you program is that your function prints on the screen the answer (as that is what your function does inside the last two ifs). After that, your program tries to print what your function returns through:
cout << getAnswer(i) << endl;
Now, you declared the return value of getAnswer()to be of type bool but, actually, you did not specified a return statement. Consequently, the value returned by your function can be either 1 or 0 (undefined behaviour), in your case it is 0, the value you see.
Tip
Use std::cout instead of cout. See Why is “using namespace std” considered bad practice?

You declared getAnswer() to return a bool value, but never return anything. One way to fix your output is to honor this return type and change
if (answer[a] == user[a])
{
cout << "Correct!\n";
}
else if (answer[a] != user[a])
{
cout << "Incorrect!\n";
}
to
return answer[a] == user[a];
Then instead of
cout << getAnswer(i) << endl;
do
if (getAnswer(i)) {
cout << "Correct!\n" << endl;
} else {
cout << "Incorrect\n" << endl;
}

your problem lies with the return value of the getAnswers
cout << getAnswer(i) << endl;
you are already outputting if the answer is correct or not within the getanswer function. and again you are using cout to output the return value of the getAnswer
you can either :
cout << "Question " << ++i << " \n";
i--;
questions[i]
getAnswer(i);
cout << endl;
or you can simply stop doing the output in getAnswer and return a string to containing the message.
std::string getAnswer(int a)
{
bool answer[5] = {true, false, false, true, false};
bool user[5];
string input;
...
...
...
if (answer[a] == user[a])
{
input = "Correct!\n";
}
else if (answer[a] != user[a])
{
input = "Incorrect!\n";
}
return input;
}
if you do it like that you don't need to change anything in your main. make sure to change the decleration of the getAnswers from
bool getAnswer(int a);
to
std::string getAnswer(int a);

Related

calling function on while loop

I'm making a calculator program but I already encounter a problem. Well, my code is in a loop that will call a function to display the choices and then ask the user to pick, a/s/m/d are the choices. If the input is on the choices, it will proceed to the next step. Otherwise, it will loop and then call the function again.
#include <iostream>
using namespace std;
void home()
{
cout << "\nChoose your operation:" << endl;
cout << "\tType [A] for Addition" << endl;
cout << "\tType [S] for Subtraction"<< endl;
cout << "\tType [M] for Multiplication" << endl;
cout << "\tType [D] for Division" << endl;
}
int main()
{
char operation;
bool no_operator = true;
int design = 73;
for (int i = 0; i < design; i++){
if (i == 25){
cout << " WELCOME TO CALCULATOR ";
i += 22;
}
else i == 72 ? cout << "*\n" : cout << "*";
}
while (no_operator){
home();
cout << "\nOperation: ";
cin >> operation;
if (operation == 'A' || operation == 'a')
{
cout << "\nIt will going to add numbers";
no_operator = false;
}
else if (operation == 'S' || operation == 's')
{
no_operator = false;
cout << "\nIt will going to subtract numbers";
}
else if (operation == 'M' || operation == 'm')
{
no_operator = false;
cout << "\nIt will going to multiply numbers";
}
else if (operation == 'D' || operation == 'd')
{
no_operator = false;
cout << "\nIt will going to divide numbers";
}
else
{
cout << "\tInvalid Input: You must enter A/S/M/D only\n";
//home();
}
}
return 0;
}
My problem is it will run the '''home()''' in else statement even if the input is correct on the second loop.
I want to stop the '''home()''' to be called when the input is correct
Your code works perfectly fine. Make sure you're inputting the correct letters.
Also for this code, a "do while()" loop would be better.
You program is working perfectly fine as the input is correct it does not show the home rather print the message it will going to divide etc.

Why won't this C++ While loop work? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm simply trying to get the user to put in their name/age and verify if it's correct. If not then they get 4 tries before the program will abort. However my while loops don't loop, instead they just continue on to the next loop. I've tried a variation of things inside the while parenthesis (op != 1) (!(op = 1)) etc.
int main() {
system("Color 0A");
string name;
int age;
int tries = 0;
int op = 0;
cout << "Hello User" << endl;
Sleep(3000);
while ((op != 1) && (tries < 4)) {
name = entName(name);
cout << "So your name is " << name << "?" << endl;
cout << "Enter '1' for YES or '2' for NO. ";
cin >> op;
if (op == 1) {
cout << "Perfect!";
}
if (op == 2) {
cout << "Please Try Again!";
tries+ 1;
}
if (tries = 4) {
//abort the program
}
}
int op2 = 0;
int tries2 = 0;
while ((op2 != 1) && (tries2 < 4)) {
op2 = 3;
age = entAge();
cout << "So you are " << age << " years old?" << endl;
while ((op2 != 1) && (op2 != 2)) {
cout << "Enter '1' for YES or '2' for NO. ";
cin >> op2;
if (op2 == 1) {
cout << "Perfect!\n";
}
if (op2 == 2) {
cout << "Please Try Again!\n";
tries2++;
}
if (tries2 = 4) {
//abort the programhi
}
}
}
return 0;
}
I'm fairly new to C++ so I'm sorry if it does have a simple answer. But anyway, I've been debugging this for over half an hour and I looked online for 20+ minutes.
if (tries = 4) {
//abort the program
}
Change this to
if (tries == 4) {
//abort the program
}
And
f (op == 2) {
cout << "Please Try Again!";
tries+= 1; // tries+ 1;
}
You can increment value in C++ like this tries+ 1;. Either use tries+= 1; or tries++;
tries+ 1; should be tries += 1; or tries++;
And,
if (tries = 4) {
//abort the program
}
should be:
if (tries == 4) {
//abort the program
}
Your program should look like this:
int main()
{
system("Color 0A");
string name;
int age;
int tries = 0;
int op = 0;
cout << "Hello User" << endl;
Sleep(3000);
while ((op != 1) && (tries < 4)) {
name = entName(name);
cout << "So your name is " << name << "?" << endl;
cout << "Enter '1' for YES or '2' for NO. ";
cin >> op;
if (op == 1) {
cout << "Perfect!";
}
if (op == 2) {
cout << "Please Try Again!";
tries+= 1;
}
if (tries == 4) {
//abort the program
}
}
int op2 = 0;
int tries2 = 0;
while ((op2 != 1) && (tries2 < 4)) {
op2 = 3;
age = entAge();
cout << "So you are " << age << " years old?" << endl;
while ((op2 != 1) && (op2 != 2)) {
cout << "Enter '1' for YES or '2' for NO. ";
cin >> op2;
if (op2 == 1) {
cout << "Perfect!\n";
}
if (op2 == 2) {
cout << "Please Try Again!\n";
tries2++;
}
if (tries2 == 4) {
//abort the programhi
}
}
}
You have forget to use = sign at multiple places. tries = 4 should be tries == 4 for comparing variable tries with numeric 4. tries = 4 was reassigning the variable tries to four and your while loop was getting terminated after it's first run. Also, tries + 1 should be tries += 1 or tries++ to increment the value of tries variable by one.

Calling a member of an array in an if/else statement

I'm trying to write an if/else statement that says 'if the the random question chosen is the first in the array do this ....' etc for all the members of the array. However it is defaulting every question chosen as the first member of the array and carrying out that action. How can I get it to separate that?
if (choice == 1)
{
enum fields{ QUESTS, ANS_1, ANS_2, ANS_3, ANS_4, NUM_FIELDS };
string QUEST[NUM_QUEST][NUM_FIELDS] =
{
{ "What course is this?\n", "A)C++\n", "B)DID\n", "C)Intro to Game\n", "D)Yoga" },
{ "Who am I?\n", "A)Bill\n", "B)Nye\n", "C) 24601\n", "D)No one\n" },
{ "Are you actually reading this?\n", "A) Yes\n", "B)No\n", "C)Maybe\n", "D)Who wants to know?\n" },
{ "Will this program work?\n", "A)Of course it will!\n", "B)It might\n", "C)Are you kidding me?\n", "D)Gods only know." },
{ "Where would I rather be?\n", "A)Home\n", "B)Europe\n", "C)Anywhere but here\n", "D)All of the above\n" }
};
srand(static_cast<unsigned int>(time(0)));
int randomQuest = (rand() % NUM_QUEST);
string question = QUEST[randomQuest][QUESTS];
cout << question;
string printAns1 = QUEST[randomQuest][ANS_1];
string printAns2 = QUEST[randomQuest][ANS_2];
string printAns3 = QUEST[randomQuest][ANS_3];
string printAns4 = QUEST[randomQuest][ANS_4];
cout << printAns1;
cout << printAns2;
cout << printAns3;
cout << printAns4;
cout << "\nAnswer:";
string answer;
cin >> answer;
//PROBLEM IS HERE. KEEPS DEFAULTING TO THIS IF STATEMENT AND IGNORING THE REST SO THAT IT THINKS THE ANSWER IS ALWAYS A
if (question == QUEST[randomQuest][0])
{
if (answer == "A")
{
cout << "Correct. Proceed.";
}
else if (answer != "A")
{
cout << "Failure. Leave. Or, you know, try again.";
}
}
else if (question == QUEST[randomQuest][1])
{
if (answer == "C")
{
cout << "Correct. Proceed.";
}
else if (answer != "C")
{
cout << "Failure. Leave. Or, you know, try again.";
}
}
else if (question == QUEST[randomQuest][2])
{
if (answer == "D")
{
cout << "Correct. Proceed.";
}
else if (answer != "D")
{
cout << "Failure. Leave. Or, you know, try again.";
}
}
else if (question == QUEST[randomQuest][3])
{
if (answer == "C")
{
cout << "Correct. Proceed.";
}
else if (answer != "C")
{
cout << "Failure. Leave. Or, you know, try again.";
}
}
else if (question == QUEST[randomQuest][4])
{
if (answer == "D")
{
cout << "Correct. Proceed.";
}
else if (answer != "D")
{
cout << "Failure. Leave. Or, you know, try again.";
}
}
}
You did not post the full source, so I just posted an example of how I would do this. Even thought it has a few more lines, its much more readable IMHO.
#include <stdio.h>
#include <vector>
#include <string>
struct Question
{
unsigned char answer;
std::string title;
std::vector<std::string> options;
};
int main()
{
std::vector<Question> questions;
// Define the questions
Question questionOne;
Question questionTwo;
Question questionThree;
Question questionFour;
Question questionFive;
questionOne.answer = 'c';
questionOne.title = "What course is this?";
questionOne.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
questionOne.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
questionOne.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
questionOne.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
questionTwo.answer = 'a';
questionTwo.title = "Who am I?";
questionTwo.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
questionTwo.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
questionTwo.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
questionTwo.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
questionThree.answer = 'b';
questionThree.title = "Are you actually reading this?";
questionThree.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
questionThree.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
questionThree.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
questionThree.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
questionFour.answer = 'c';
questionFour.title = "Will this program work?";
questionFour.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
questionFour.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
questionFour.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
questionFour.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
questionFive.answer = 'd';
questionFive.title = "Where would I rather be?";
questionFive.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
questionFive.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
questionFive.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
questionFive.options.push_back("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
// Save them
questions.push_back(questionOne);
questions.push_back(questionTwo);
questions.push_back(questionThree);
questions.push_back(questionFour);
questions.push_back(questionFive);
// Change to a random question accordingly
for (auto& question : questions)
{
// Show question title
std::cout << question.title << std::endl;
// Show question options
for (auto& option : question.options)
{
std::cout << option << std::endl;
}
// Get the user input accordingly
unsigned char userInput = 0;
// Validate user input
if (userInput == question.answer)
{
std::cout << "Congratulations! Next question coming up..." << std::endl;
}
else
{
std::cout << "Wrong answer. Next question coming up..." << std::endl;
}
}
return 0;
}

C++ Function being called when it isn't supposed to

I'm nearly finished working on a small guessing game, but i have run into a problem I don't know how to work around.
The problem is with the check_guess function that is checking to make sure the guess being input is a number between 1 and 100.
When running the program the first time, everything works fine.
http://i.imgur.com/pprunDT.png (I would post images if my reputation weren't so low)
But every time after, where yes to play again is chosen, the program runs through the check_guess function and displays "Invalid Input" when it shouldn't
http://i.imgur.com/8OSnSJt.png
I'm not sure why the program is behaving this way.
The code for the entire program is here:
#include <iostream>
#include <cstdlib> //for rand
#include <ctime> //for time
#include <string>
#include <sstream> //for conversions from string to int
using namespace std;
int check_guess(int tries) { //function for limiting the input of guess
string guess = "";
int result = 0;
do {
getline (cin, guess);
istringstream convert(guess);
if ( !(convert >> result) || (result < 1 || result > 100) ) {
result = 0;
cout << "Invalid Input.\n" << endl;
cout << "You have " << tries << " tries: ";
}
} while (result == 0);
return result;
}
bool play_again() { //function for limiting the input of mode
bool quit;
string yn;
do {
cin >> yn;
if ( yn == "y" || yn == "yes" ) {
quit = false;
}
else if ( yn == "n" || yn == "no" ) {
quit = true;
}
else {
yn = "invalid";
cout << "Invalid input.\n\nEnter 'y' or 'n': ";
}
} while ( yn == "invalid" );
return quit;
}
int main()
{
srand(time(0)); //sets seed to be random
int mystery = 0; //defines mystery number
int guess = 0; //defines guess
int tries = 5; //defines trys
bool quit = false; //defines replay or quit
cout << "----------------------------------\n";
do { //while mode is not set to quit, keep playing
tries = 5; //resets tries each new game
mystery = rand() % 100 + 1; //sets mystery number to be random
guess = 0;
cout << "Pick a number between 1 and 100.\n\nYou have 5 tries: ";
while (tries != 0) { //loops until you have no tries left
guess = check_guess(tries);
if (guess == mystery) { tries = 0; } //if you guess right it ends the loop
else { tries--; } //guessing wrong lowers tries by 1
if ( tries != 0 && guess > mystery) {
cout << guess << " is too high.\n" << endl;
cout << "You have " << tries << " tries: ";
}
if ( tries != 0 && guess < mystery) {
cout << guess << " is too low.\n" << endl;
cout << "You have " << tries << " tries: ";
}
}
if (guess == mystery) { //if guess == mystery by time loop ends you win
cout << "Got it! You Win!\n" << endl;
}
else { //if not, you lose
cout << "You Lose! The number was: " << mystery << ".\n" <<endl;
}
cout << "-------------------\n";
cout << "Play Again?(y/n): "; //ask user to play again
quit = play_again();
cout << "-------------------\n";
if (quit == false)
cout << endl;
} while (quit == false);
cout << "----------------------------------" << endl;
return 0;
}
I'm not sure how to fix this.
this line:
cin >> yn;
only reads the 'y' but not the end of line. As a result, the next execution of this instruction
getline (cin, guess);
initializes guess to an empty string.
On line 19, import the code "cin.ignore();" without quotations.
So your code reads as
`int check_guess(int tries) { //function for limiting the input of guess
string guess = "";
int result = 0;
do {
getline (cin, guess);
istringstream convert(guess);
if ( !(convert >> result) || (result < 1 || result > 100) ) {
result = 0;
cin.ignore();
cout << "Invalid Input.\n" << endl;
cout << "You have " << tries << " tries: ";
}
} while (result == 0);
return result;
}
`
and so on. This stops input into the console briefly. You're code is reading the 'y' to try again as the input for the number when you restart as well. Putting in the little line cin.ignore(), stops it from inputting y twice.
Change play_again() to:
bool play_again() { //function for limiting the input of mode
bool quit;
string yn;
do {
getline (cin, yn);
if ( yn == "y" || yn == "yes" ) {
quit = false;
}
else if ( yn == "n" || yn == "no" ) {
quit = true;
}
else {
yn = "invalid";
cout << "Invalid input.\n\nEnter 'y' or 'n': ";
}
} while ( yn == "invalid" );
return quit;
}

Trying to use two diffrent arrays to ask and answer questions

I am trying to use 2 different arrays 1 for questions and the for answers but when I select the correct answer for any of the selected questions past "2" it always gives me incorrect answer and I cant see why can anyone held me please?
#include "Questions.h"
using namespace std;
//struct quiz
//{
// string question[MAXITEMS];
// string answers[MAXITEMS];
//};
const int MAXITEMS = 10;
int main ()
{
//quiz listQuiz[MAXITEMS];
//
//ifstream QuestionFile("Questions2.txt");
//char a;
//int count = 0;
//
// if(!QuestionFile) // file testing
// {
// cout<< " error opening file" << endl;
// return 1;
// }
//
// for (int i=0; i<MAXITEMS; i++)
// {
// QuestionFile>>listQuiz[i].
//return 0;
string question[MAXITEMS] = {"How_many_cards_of_each_suit_are_there?", "How_many_suits_are_there_in_a_standard_pack_of_cards?", "How_many_kings_are_in_a_standard_pack_of_cards?", "How_many_cards_are_in_a_standard_deck_of_cards?","How_many_black_suits_are_there_in_a_standard_pack_of_cards?", "How_many_red_suits_are_in_a_standard_pack_of_cards?", "Whats_the_number_of_the_card_that_comes_before_jack?", "How_many_cards_in_each_set_of_suits_are_there?", "What_is_the_lowest_number_in_a_standard_pack_of_cards?", "What_is_the_highest_number_in_a_standard_pack_of_cards?"};
string answers[MAXITEMS] = {"4", "4", "4", "52", "2", "2", "10", "13", "2", "10"};
int userInput = 0;
int tries = 0;
bool isGameOver = false;
cout << "select 1 to start game" << endl; //gives option to start and quit game
cout << "select 2 to quit game" << endl;
cin >> userInput;
if (userInput == 2)
{
isGameOver = true;
return 0;
};
// error message if 1 or 2 is not input
do
{
if (userInput!=1 && userInput!=2)
{
cout << " Your input is not valid! please try again:" << endl; // try switch cases for the different outcomes
cout << "select 1 to start game" << endl;
cout << "select 2 to quit game" << endl;
cin >> userInput;
if (userInput == 2)
{
isGameOver = true;
return 0;
};
while (!(cin >> userInput))
{
cin.clear(); // clear the error flags
cin.ignore(INT_MAX, '\n'); // discard the row
cout << "Your input is not valid! please try again: ";
cout << "select 1 to start game" << endl;
cout << "select 2 to quit game" << endl;
}
cout << userInput << endl;
}
// reprisent all characters as number to stop while roblem
// when game starts gives option to select question and shows all questions
if(userInput == 1)
{
// // system("pause");
// //return-1;
//// };
// while(QuestionFile) // while read is working
// {
//
// // for display
//QuestionFile >> question[count]; // read into array
//cout << count << " " << question << endl;
// count++;
// }
//
// for (int i = 0; i < count ; ++i)
// {cout << " array" << i << " is ::";
// cout << question[i]<< endl;
// }
// cout << question[0]; //reads data in cell
// //QuestionFile.close();
// //system ("pause");
do
{
cout << "select question" << endl;
for(int i = 0; i != MAXITEMS; i++)
{
cout << i << " " << question[i] << endl;
}
int selectQestion;
cin >> selectQestion;
if(selectQestion == 0||1||2||3||4||5||6||7||8||9 && tries != 2)
{
cout << "Enter your answer" << endl;
string userAnswer;
cin >> userAnswer;
while (!(cin >> userAnswer))
{
cin.clear(); // clear the error flags
cin.ignore(INT_MAX, '\n'); // discard the row
cout << "Your input is not valid! please try again: ";
}
if (userAnswer == answers[0])
{
cout << "Correct answer" << endl;
}
else{
cout << "incorrect try again" << endl;
tries++;
cin >> userAnswer;
if (userAnswer == answers[0])
{
cout << "Correct answer" << endl;
}
else
cout << "Incorrect" << endl;
}
}
if (selectQestion == 0||1||2||3||4||5||6||7||8||9 && tries == 2)
{
cout << "you can no longer answer this question" << endl;
cout << "try another question" << endl;
}
}
while(userInput == 1);
}
}
while(isGameOver == false);
}
I'm sure we've been through this before
if (selectQestion == 0||1||2||3||4||5||6||7||8||9 && tries == 2)
is incorrect. This version is correct.
if ((selectQestion == 0 || selectQestion == 1 || selectQestion == 2 || selectQestion == 3 || selectQestion == 4 || selectQestion == 5 || selectQestion == 6|| selectQestion == 7|| selectQestion == 8 || selectQestion == 9) && tries == 2)
But really you should use a little bit of logic and simplify, this version is even better
if (selectQestion >= 0 && selectQestion <= 9 && tries == 2)
Much better.
Fix that problem (you have it in at least two places) and if your program is still not working post again.
This doesn't make sense:
string question[MAXITEMS] = {"How_many_cards_of_each_suit_are_there?",
"How_many_suits_are_there_in_a_standard_pack_of_cards?",
"How_many_kings_are_in_a_standard_pack_of_cards?",
"How_many_cards_are_in_a_standard_deck_of_cards?",
"How_many_black_suits_are_there_in_a_standard_pack_of_cards?",
"How_many_red_suits_are_in_a_standard_pack_of_cards?",
"Whats_the_number_of_the_card_that_comes_before_jack?",
"How_many_cards_in_each_set_of_suits_are_there?",
"What_is_the_lowest_number_in_a_standard_pack_of_cards?",
"What_is_the_highest_number_in_a_standard_pack_of_cards?"};
string answers[MAXITEMS] = {"4", "4", "4", "52", "2", "2", "10", "13", "2", "10"};
To me, the answers and questions aren't in synch. And of course, it would be MUCH easier to deal with this if you have question and answer as part of a struct (or class):
struct QuestionAndAnswer
{
std::string question;
std::string answer;
}
QuestionAndAnswer qAndA[MAXITEMS] =
{
{ "Question 1", "Answer for question 1" },
{ "Question 2", "Answer for question 2" },
...
};
And this code doesn't do what you think it does:
if(selectQestion == 0||1||2||3||4||5||6||7||8||9 && tries != 2)
As suggested in another answer, you could do if (selectOption == 0 || selectOption == 1 ..., but I would suggest that you use a switch statement:
switch(selectOption)
{
case 0:
case 1:
... // more cases go here.
case 9:
if (tries != 2)
...
else // tries == 2
...
break;
default:
... Stuff to do when input was not a valid selection.
break;
}
It is much easier to read a switch than a 11 item long if-condition.
A further problem is this:
do {
...
} while(userInput == 1);
The variable userInput isn't changing inside that loop...
You also have a bug here:
if (userAnswer == answers[0])
You probably want to check that the answer is the answer to the selected question, not the first question.
May I suggest that you develop smaller parts of the code at a time - change one little thing, test it, if it doesn't work, figure out why, then write a bit more code. I've not even compiled or run your code, and I've spotted at least four distinct errors that - yes, I've done programming for over 30 years, so I have some experience in spotting errors.
This:
if (selectQestion == 0||1||2||3||4||5||6||7||8||9 ...)
is equivalent to:
if (selectQuestion == 1 ...)
The right hand side of the == sign is an expression for which C++ calculates a value and substitutes that value in place of that long series of ||'s.