If else problem

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
//Subtraction program - Brian Story

#include <stdlib.h>
#include <iostream>
using namespace std;

int main() {
    int a, b, c, d, score; //Setting a, b, c, and score as an integer.
    score = 0; // Score is equal to 0, so it starts you from the beginning every time.
    do  // loop while score is less then 10
    
    {
          
    srand (time(NULL)); // generates random numbers based on the time.
    
    a = rand() % 10 + 1; // Calculates a as a random number from 1-10
    b = rand() % 10 + 1; // Calculates b as a random number from 1-10
    d = a - b; //Subtracts a and b and outputs the answer to d.
    cout << a << " " << b << endl; // Shows user the random numbers generated
    
    cout << a << " - " << b << " =?" << endl; // Same as above, but now asks for an answer.
    cin >> c; // user inputs the answer as c
    if (c==d) //compares the users answer and the real answer calculated above
    
         cout << " Correct" << endl; // if its true tell the user its correct
         score++; // adds 1 to score -- causes problem in program --
    else // Complains that there is an expected ; before else
        cout << " Incorrect" << endl; // if its true tell the user its not correct
        } while (score<10); //Do whhile score is less then 10

        cout << " You win!"; // Once the loop is complete tell the user he wins.
    return 0; 
}



The compile stops at else and claims that theres a missing ;

I am new and right now im assuming I used score ++ in an improper way, if I delete the scoree++ then it compiles fine, but I need to add one to score every time you complete a problem.

I'm open to any comments and suggestions.
You need braces enclosing everything you want to be done after the if's and else's IF there is more than one statement to be done.

1
2
3
4
if(thisthing == thatthing)
       cout << "Then do this" << endl;
else 
       cout << "Do something else" << endl; 


is fine but for more than one

1
2
3
4
5
6
7

if(thisthing == thatthing)
{
       cout << "Do this" << endl;
       cout << "And that" << endl;
}else
       cout << "Print this" << endl;


So you see, that is why it compiles fine when you removed score++, because there was only one statement then.

Also else has to be immediately after the if(something){} block. (A block is the statements encompassed in a pair of curly braces)

Hope that helps. I'm also new to C++ but I help who I can.
Last edited on
Thanks, I understand loud and clear. Everything compiled fine now.
No problemo.
write a program asking user to chose p for pine, o for oak, or M for mahogany. show the price of the table pine - 100, oak- 225 and mahogany- 310 if user chooses any other than listed make price 0
write aprogram to for user to enter 2 integers and a character. if character is A add the two integers, if S, subtract the second from the first integer, if M, multiply. displat the result
this is the code and it´s fine ,a bracket it´s missing

#include <stdlib.h>
#include <iostream>
using namespace std;

int main() {
int a, b, c, d, score;
score = 0;
do

{

srand (time(NULL));

a = rand() % 10 + 1;
b = rand() % 10 + 1;
d = a - b;
cout << a << " " << b << endl;

cout << a << " - " << b << " =?" << endl;
cin >> c;
if (c==d)
{
cout << " Correct" << endl;
score++;
}
else
cout << " Incorrect" << endl;

} while (score<10);

cout << " You win!";
return 0;
}
Last edited on
that program works fine but you may want to only seed the program once.

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
#include <stdlib.h>
#include <iostream>
#include <ctime>                  // needed for time in your seed, or i need it for vc++
using namespace std;

int main() {
int a, b, c, d, score;
score = 0;

srand (time(NULL)); //moved out here so that it only seeds the srand once!

do

{

a = rand() % 10 + 1;
b = rand() % 10 + 1;
d = a - b;
cout << a << " " << b << endl;

cout << a << " - " << b << " =?" << endl;
cin >> c;
if (c==d)
{
cout << " Correct" << endl;
score++;
}
else
cout << " Incorrect" << endl;

} while (score<10);

cout << " You win!";
return 0;
}


this is also a good idea exercise that can be improved. for one, you can use rand a new number and check if it is even or odd, if odd "-" if even "+".
Topic archived. No new replies allowed.