Simple program, need a little help

Here is my program : #include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
int a, b, c, d, e, f;
cout << "These are the numbers you`re allowed to use: ";
srand (time(NULL));
for (int i=0;i<5;i++){

a = rand()% 26;
cout << endl<< a;}
cout << "\nThis is the result you have to get using the numbers given, you can use any arithmetic operation you know as long as it contains the numbers given: ";
b = rand() % 26;
cout << b << endl;
cin >> c;
if (c==b){
cout <<"good";
}
else {
cout << "wrong";
}


return 0;
}

So, everything works fine except when i put the arithmetic operation as c, even though it is equal to b, it says wrong.
For example: my number were 5 3 10 1 19, and I had to get 23.
I have put the following input: ((19+3)+(10/5))-1.... which is 23, but still it says wrong... Its a simple program, please help.
You seem to be expecting the program to magically determine the result of your calculation. It won't do that. It will just try to read a single integer from whatever you type. In your case, the first character is a parenthesis, which is not part of an integer, so cin gives up and doesn't read anything.
I get good every time I run this program. I just put in the correct answer even if that is not one of the number options. :)

There is much work to be done in order for this to run correctly. First you will need to get user input into a string. Then convert substrings into integers. Maybe some sort of switch statement to get the correct math operators working. Also add checks to make sure user input contains only numbers that are part of the options and that the user isn't cheating, like I was...
Well how do I convert the substrings into integers... can you give me some sort of code example?
Well, here is something for you to play around with.
You will need to include the <string> header and <ctype.h> header.

1
2
3
4
5
6
7
string s;
int c;
getline(cin,s); // to get user input

if(isdigit(s[3]) && is digit(s[4])) { //check to see if these are numbers
     c = stoi(s.substr(3,5)); //string to int saved in c
}
Last edited on
Topic archived. No new replies allowed.