Conditions with random numbers

I'm practicing using random number generators and making a basic math game with user input. I need help including conditions with equations. The game will display a random number and the user will be asked to double it. Thanks in advance!

Here's what I have so far:

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string>

using namespace std;

const int LOW = 1;
const int HIGH = 10;

int main()
{
int num;
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
num = rand() % (HIGH - LOW + 1) + LOW;

string ans;
cout << "Double the number: " << num << endl;
getline (cin,ans);
//This is where I need a condition for whether the number is correctly doubled or not.
//I know how to write 'if else' statements but not with math.

return 0;
}
Your number is held in num. Add another variable to hold num * 2. After the user inputs the string "ans", convert the string to a number and compare that result to the variable holding the doubled number.
Thanks for the reply! I have tried something like this but it didn't work:

if (ans == num * 2)
{}

Would you mind giving me an example? I'm relatively new to programming.

std::cout<<num*2<<"is the answer";
As koothkeeper said first you must convert the string to a integer
Try with the stoi function on string library

http://www.cplusplus.com/reference/string/stoi/
Topic archived. No new replies allowed.