Not very good at debugging yet but I’m getting a few errors. A few expected ‘(‘ ‘)’ and ‘;’
Also ‘else’ without a previous ‘if’, no match for ‘operator>>’ in cout
I know this is easy, but still trying to get my foot in the door. Thanks 🙂
#include <iostream>
#include <cstdlib>
using namespace std;
int main() // guess number game
{
int x;
cout >> "Please enter a number\n";
getline(cin x);
int y = rand();
while x != y
{
if x < y;
cout >> "Go higher";
else;
cout >> "Go lower";
}
}
This is wrong,
std::ostreamsonly provide theoperator<<to insert formatted data. Usecout << "Please enter a number\n";instead.First, you’re missing a
,, since getline needs two or three arguments. But sincexis anintegerand not astd::stringit is still wrong. Think about it – can you store a text line inside of an integer? Usecin >> xinstead.While this doesn’t seem wrong there’s a logical error.
rand()is a pseudo random number generator. It uses a seed as start value and some kind of algorithm (a*m + b). Thus you have to specify a start value, also called seed. You can specify this by usingsrand(). The same seed will result in the same order of numbers, so use something likesrand(time(0)).Use parenthesis. And drop the additional
;. A stray semicolon;in your program resembles the empty expression.EDIT: Working code: