Ok, so I am creating a Math Quiz. Just something basic for now, but at the end of my while loop I want to accumulate the output, meaning if you do 5 questions of addition, the output will store the expressions of all 5 questions and use a conditional operator to say if the answer is Correct or Wrong, and it will be displayed at the very end.
I have the same program in Java, but I want to switch it over to C++ since I really like C++, and desire more of it, and that is why I would like to figure this one out.
Java:
output += "\n" + number1 + " - " + number2 + " = " + answer + ((number1 - number2 == answer) ? " CORRECT" : " WRONG");
My while loop w/ the output accumulator in C++:
while (count <= NUMBER_OF_QUESTIONS) {
num1 = 1 + rand() % 50;
num2 = 1 + rand() % 50;
if (num1 < num2) {
temp = num2;
num2 = num1;
num1 = temp;
}
cout << "\n"<< num1 << " + " << num2 << " = " << endl;
cin >> answer;
if (num1 + num2 == answer) {
cout << "Right!" << endl;
correctCount++;
}
else
cout << "Wrong! Should be " << (num1 + num2) << endl;
// Increase count
count++;
// Prepare all questions if correct or wrong, for output
output += // The rest...
}
//and for final output
cout << output;
without using any STL libraries : just store your variable in an array.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#define NUMBER_OF_QUESTIONS 5
int main()
{
srand ( time(NULL) );
int count = 0;
//Init a
int output[NUMBER_OF_QUESTIONS][4];
while (count <= NUMBER_OF_QUESTIONS) {
int num1 = 1 + rand() % 50;
int num2 = 1 + rand() % 50;
int answer = 0;
if (num1 < num2) {
int temp = num2;
num2 = num1;
num1 = temp;
}
std::cout << "\n"<< num1 << " + " << num2 << " = " << std::endl;
std::cin >> answer;
if (num1 + num2 == answer) {
std::cout << "Right!" << std::endl;
//correctCount++;
}
else
{
std::cout << "Wrong! Should be " << (num1 + num2) << std::endl;
}
// Prepare all questions if correct or wrong, for output
output[count][0] = num1;
output[count][1] = num2;
output[count][2] = answer;
output[count][3] = (num1 + num2 == answer);
// Increase count
count++;
}
for (int i =0; i < NUMBER_OF_QUESTIONS; i++)
{
std::cout << "Q" << i << " : " ;
std::cout << output[i][0] << " + " << output[i][1];
std::cout << " = " << output[i][2] << " . ";
if (output[i][3])
std::cout << "Right Answer ! " << std::endl;
else
std::cout << "Wrong! Should be " << (output[i][0] + output[i][1]) << std::endl;
}
}
So, if i'm reading this correctly you want to loop through lets say 5 questions.
so you'd have something like...
5+12 =
user input
13+32 =
user input
31 + 1 =
user input
27+ 15 =
user input
11+11 =
user input
then the program would then output the following:
5+12 = 17
CORRECT
13+32 = 45
CORRECT
31 + 1 = 33
WRONG
27+ 15 = 42
CORRECT
11+11 = 11
WRONG
if thats what you want, then you can try something like this:
#define MAX_QUESTIONS 5
#define OPERANDS 2
#define LEFT 0
#define RIGHT 1
int main()
{
int ques[MAX_QUESTIONS][OPERANDS];
int ans[MAX_QUESTIONS];
for(int i=0; i < MAX_QUESTIONS; i++)
{
ques[i][LEFT] = 1 + rand() % 50;
ques[i][RIGHT] = 1 + rand() % 50;
if (ques[i][LEFT] < ques[i][RIGHT])
{
int temp = ques[i][RIGHT];
ques[i][RIGHT] = ques[i][LEFT];
ques[i][LEFT] = temp;
}
cout << "\n" << ques[i][LEFT] << " + " << ques[i][RIGHT] << " = " << endl;
cin >> answer[i];
}
for(int i = 0; i < MAX_QUESTIONS; i++)
{
cout << "\n" << ques[i][LEFT] << " + " << ques[i][RIGHT] << " = " << answer[i] << "\n" << (((ques[i][LEFT] + ques[i][RIGHT]) == answer[i])?"CORRECT":"WRONG") << endl;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.