Random Number Generator with a twist
Aug 28, 2013 at 6:45pm UTC
Hello, I have to come up with a program that I have to create which is:
The Computer guesses a random calculation "Value1 Operator Value2 = Answer." The computer randomly displays two of the four objects. The Player must guess the remaining two objects. If the player gets one right then the computer says "one is right." The game should continue until the user has provided all the correct calculations.
This is what I have, BUT I have 2 errors that I can't seem to figure out. Any help is greatly appreciated!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast <unsigned int >(time(0))); //Random Number Generator
double value1 = rand() % 50 + 1; //Random number between 1 and 50
double value2 = rand() % 50 + 1;
int operatorDefine = rand() % 4 + 1; // random number between 1 and 4 to determine random operator
char operator1;
double answer;
int defineObject1 = rand() % 3 + 1;
int defineObject2 = rand() % 3 + 1;
double defineObject3;
int guess;
if (operatorDefine == 1) // if-else to decide random operator
{
operator1 = '+' ;
}
else if (operatorDefine == 2)
{
operator1 = '-' ;
}
else if (operatorDefine == 3)
{
operator1 = '*' ;
}
else if (operatorDefine == 4)
{
operator1 = '/' ;
}
if (operator1 == '+' ) // if-else to solve the random equation
{
(answer = value1 + value2);
}
else if (operator1 == '-' )
{
(answer = value1 - value2);
}
else if (operator1 == '*' )
{
(answer = value1 * value2);
}
else if (operator1 == '/' )
{
(answer = value1 / value2);
}
cout << "Welcome to Guess My Number, with a twist!\n\n" ;
if (defineObject1 == 1) //if else to display the first piece of equation
{
cout << "Value 1 is " << value1 << endl;
}
else if (defineObject1 == 2)
{
cout << "Value 2 is " << value2 << endl;
}
else if (defineObject1 == 3)
{
cout << "The answer is " << answer << endl;
}
if (defineObject2 == 1) //if else to display the second piece of the equation
{
cout << "Value 1 is " << value1 << endl;
}
else if (defineObject2 == 2)
{
cout << "Value 2 is " << value2 << endl;
}
else if (defineObject2 == 3)
{
cout << "The answer is " << answer << endl;
}
if (defineObject1 == 1 && defineObject2 == 2) //if else to determine what the player needs to guess
{
(defineObject3 = answer);
}
else if (defineObject1 == 2 && defineObject2 == 1)
{
(defineObject3 = answer);
}
else if (defineObject1 == 1 && defineObject2 == 3)
{
(defineObject3 = value2);
}
else if (defineObject1 == 3 && defineObject2 == 1)
{
(defineObject3 = value2);
}
else if (defineObject1 == 2 && defineObject2 == 3)
{
(defineObject3 = value1);
}
else if (defineObject1 == 3 && defineObject2 == 2)
{
(defineObject3 = value1);
}
cout << "The operation is " << operator1 << endl;
cout << "Input the missing piece of the equation." << endl; //Retrieve player's guess
cin >> guess;
while (guess != defineObject3) // loop to determine if the player was correct
{
cout << "Sorry, but that is incorrect. Please try again." << endl;
cin >> guess;
}
else (guess == defineObject3);
{
cout << "Good Job, that is CORRECT!" << endl;
}
system("PAUSE" );
return EXIT_SUCCESS;
}
The errors I am getting in Visual Studio are:
error C2181 : illegal else without matching if, line 212
IntelliSense : expected a statement, line 212
Please help!
Thank you kindly!
Aug 28, 2013 at 7:08pm UTC
Line 209 is wrong.
Looks like you're trying to supply an argument to else, but else has no argument. It's the "default" case.
You just want:
1 2 3 4
else
{
cout << //whatever...
}
Ugh actually your whole format here is just no good. Hard to follow. An else has to follow an if, not a while.
Last edited on Aug 28, 2013 at 7:11pm UTC
Aug 28, 2013 at 7:29pm UTC
Doh.. Not sure how I missed that!
Thanks! corrected it and all is working well.
Best regards,
Frank
Topic archived. No new replies allowed.