Problem

#include <iostream>

using namespace std;


int main()

{ int random_number;
int random_number2;
string answer;

random_number = rand() % 10 + 1;
random_number2 = rand() % 10 + 1;
}

{ cout << "Is "random_number" equal to "random_number2" "?" ";

cin << answer;
if (random_number) = (random_number2) and answer = "yes"
then cout << "You are correct!"
else if (random_number) != (random_number2) and answer = "yes"
then cout << "You are incorrect!"
else if (random_number) != (random_number2) and answer = "no"
then cout << "You are correct!";

cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
while(true){};
}


That is the program so far...I want the program to compare 2 random numbers and ask you if they are =, then if your answer is correct it will display "you are correct!" and if you aren't then vice versa. I don't know how I messed it up, maybe some grammar isn't correct or maybe I just assigned the wrong integer or what have you. Please help!
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
#include <iostream>

using namespace std;


int main()

{	int random_number;
int random_number2;
string answer;

random_number = rand() % 10 + 1;
random_number2 = rand() % 10 + 1;
}//This bracket is not needed here...

{ cout << "Is "random_number" equal to "random_number2" "?" "; 
//Multiple mistakes here, where are your << operators for cout? 
//And why the bracket at the beginning?
//The << operators need to preceed anything that 
//is not in "quotes" and end anything in "quote"

cin << answer; //You are using the output operator << instead  of the input operator >>
if (random_number) = (random_number2) and answer = "yes"
  //when making comparisons, you use the == operator not the = assignment operator
  //"and" is not used to compare multiple arguements, && is used
  //Every comparison should be in the same bracket, but can be in 
  //multiple brackets as long as there is one bracket enclosing everything
  then cout << "You are correct!"
  else if (random_number) != (random_number2) and answer = "yes"
    then cout << "You are incorrect!"
    else if (random_number) != (random_number2) and answer = "no"
      then cout << "You are correct!";
   //in c++ "then" is replaced by { brackets
   //Basically statements below conditions are always in {} brackets
    
    cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;//lol
  while(true){};
}

//I realise you might be coming from a 100% object oriented programming 
//language or just never programmed before So I will go ahead and below show you what I mean 



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
#include <iostream>

using namespace std;

int main()
{	
  int random_number;
  int random_number2;
  string answer;
  
  do
  {
    
    random_number = rand() % 10 + 1;
    random_number2 = rand() % 10 + 1;
    
    cout << "Is " << random_number << " equal to " << random_number2 << "?\n"; 
    
    
    cin >> answer;
    if ((random_number == random_number2) && (answer == "yes"))
    {
      cout << "You are correct!\n";
    }
    else if ((random_number != random_number2) && (answer == "yes"))
    {
      cout << "You are incorrect!\n";
    }
    else if ((random_number != random_number2) && (answer == "no"))
      cout << "You are correct!\n"; 
    //You can have no brackets if you are only doing one thing after the condition
    
    //cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
    //The above can be replaced with system("cls") for windows or system("clear") for unix/linux
     system("clear");
  } while(true);
  
  return 0;
}



EDIT: like I GuNNeR I (welcome :D) mentioned below, reading the c++ tutorials on the website is a good first step to understanding what I did above. http://www.cplusplus.com/doc/tutorial/
Last edited on
closed account (G30oGNh0)
First off. You close your main prior to program finalization.

cout << "Is "random_number" equal to "random_number2" "?";

This statement is invalid, to output variables you do not need to use quotation. Instead use the operator << as you would for text. So this line becomes.

cout << "Is " << random_number << " equal to " << random_number2 << "?";

Next.

if (random_number) = (random_number2) and answer = "yes"

Correct procedure would be...

1
2
3
4
5
6
7
if(random_number == random_number2)
{
      //Function if equal...
} else if (random_number != random_number2)
{
     //Function if not equal
}


Furthermore, your 'while' statement is invalid. To create a while loop use your while statement AFTER your main() and BEFORE the function(s) you wish to loop.

Your code is some of the worst I've seen. I strongly suggest reading through the websites tutorials for basic C++ applications, it will teach you the fundamentals and theory you need to know :) Good luck!

P.S This is my first post, I signed up about 5 minutes ago :)
Thanks for the help, there's alot less mistakes in my compiler. My compiler doesn't accept the && symbol though. It doesn't accept the == symbol, it accepts = however. Then again, it might work but for some reason visual studio says it can't find the .exe of my program whe ni try to debug it...which is stupid since I haven't been able to make a .exe since I cant debug it.
closed account (Dy7SLyTq)
ummm r u sure it doesnt accept == and &&? also im really jealous of your name
Yeah, apparently visual studio doesn't accept == and &&. It might just be the context their in though, but I doubt it. Thanks, but I don't know what's so great about my name. It's not like it's creative or anything, just 4 letters.

So if anyone else who uses visual studio can tell me what's wrong with my program...I would be very thankful.
Last edited on
closed account (Dy7SLyTq)
its an (maybe unintentional) reference to a magic character
You mean Jace the mind whatsit?
closed account (Dy7SLyTq)
yes
closed account (G30oGNh0)
Which Visual studio are you using? I use vc++ 2010, and logical operators work fine for me.

How are you using the && and == operators?
I am using Microsoft Visual C++ 2010 express.

This is my most recent program:

#include <iostream>

using namespace std;

int main()
{
int random_number;
int random_number2;
string answer;

do
{

random_number = rand() % 10 + 1;
random_number2 = rand() % 10 + 1;

cout << "Is " << random_number << " equal to " << random_number2 << "?\n";


cin >> answer;
if ((random_number == random_number2) && (answer == "yes"))
{
cout << "You are correct!\n";
}
else if ((random_number != random_number2) && (answer == "yes"))
{
cout << "You are incorrect!\n";
}
else if ((random_number != random_number2) && (answer == "no"))
cout << "You are correct!\n";

system("clear");
} while(true);

return 0;
}

PS: For some reason when I take away the first quote on "yes"... it accepts the == symbols throughout the whole program. Also in the following statement, the cin >> is not accepted: cin >> answer;
The cin is accepted but not the >>
Last edited on
closed account (G30oGNh0)
A copy of Smac89's code. Interesting.

To the eye it seems faultless, however if it's giving you problems you could do some serious inception if statements(I think, more conventionally named, 'Nested'?).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//.......
cin >> answer;
if ((random_number == random_number2)
{
     if(answer == "yes")
     {
          cout << "You are correct!\n";
     }
     else 
     {
          cout << "You are incorrect!\n";
     }
}

// I'm sure you get the idea. 


Also if your using Visual C++ Express, as I guess I'd say your using windows. so change,

system("clear");

to

system("cls");

EDIT: Had to change from 'else if' to 'else'

EDIT2:

I'd say it is easier to compare an integer than a string, instead of the user typing 'yes' . So try using 0 as yes and 1 as no.

Or ( a more drastic approach ) would be designing your own comparison algorithm. Easiest way to compare 2 entities is using XOR, the ^ operator.

if(random_number ^ random_number2 = 0)
{
cout << "Numbers are the same!\n";
}
else
{
cout << "Numbers are not equal!\n";
}

Last edited on
so the XOR approach would replace?

cin >> answer;
if ((random_number == random_number2)
{
if(answer == "yes")
{
cout << "You are correct!\n";
}
else
{
cout << "You are incorrect!\n";
}
}

Also...have you ever had one of your programs when you are trying to debug it and compile it say it can't find the .exe of it...but you haven't compiled it yet?
Last edited on
#include <string>
closed account (G30oGNh0)
^ +1, I'm ashamed of myself for missing that one, I've been staring at code all day.

EDIT: And no it does not replace, it were an example. And no I haven't came across that problem.
Last edited on
IT FRICKEN WORKS!!! IT REALLY FRICKEN WORKS!!! SUCK ON THAT WORLD!!!!!
I AM BORIS, AND I AM INVINCIBLE!!!!!!
Thnks so much guys...really helpful. That was my first program and it works now. Thanks.
closed account (G30oGNh0)
1
2
_Jace.levelUp("Congratulations");
_OPThread.close();
Topic archived. No new replies allowed.