A question from a beginner about his code.

I'm a complete beginner when it comes to C++. (I started yesterday)

Could someone please tell me why my code doesn't work, I've looked through it multiple times, but I can't seem to find the issue.


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

using namespace std;

main ()
{
    string subject1_name;
    string subject2_name;
    int subject1_age;
    int subject2_age;

    cout << "Please enter the first subject's name: " << "\n";
    getline( cin, subject1_name, '\n' );
    cout << "Please enter the second subject's name: " << "\n";
    getline( cin, subject2_name, '\n' );
    cout << "Now, please enter the first subject's age: " << endl;
    cin subject1_age;
    cout << "And then the second subject's age: " << endl;
    cin subject2_age;

    int subject1_overage = subject1_age - subject2_age;
    int subject2_overage = subject2_age - subject1_age;

    if ( subject1_age > subject2_age )
    {
        cout << subject1_name << " is oldest! " << subject1_overage << " years older, actually.\n";
    }
    
    else if ( subject2_age > subject1_age )
    {
        cout << subject2_name << " is oldest! " << subject2_overage << " years older, actually.\n";
    }
}


As you can see, it's a very simple program. It's purpose is to take 2 subjects names and ages, and then print out the name and age, and also tell how much one subject is older than another.

I hope you can help! :)
hiya.
firstly:
why my code doesn't work

is fairly useless for people. Which part of "not working" would be helpful i.e. some compiler errors or warnings.

Onto the problem.
When i compile your code on my box my compiler tells me this:

error C2146: syntax error : missing ';' before identifier 'subject1_age'
error C2146: syntax error : missing ';' before identifier 'subject2_age'

It also tells me the line numbers as well (18 and 20).
Can you see what you're missing now?

(also you want your main to return an int type, and before line 34 put a:
 
return 0;

in.
I appreciate your help

But am I supposed to put a ; after cin? That doesn't make sense to me, but I'll try it.

I tried it, but then when i compiled and ran the code, it completely stopped after I entered the second subject's name.

When it went: "Now, please enter the first subjects age: " it doesn't let me enter anything, and it just instantly skips to the next "cout".

What am I supposed to do here, I really don't understand.

But am I supposed to put a ;

No. The compiler is telling you that it got confused on lines 18 and 20, so you should look at those lines for a syntax error. When the compiler encoutners a syntax error, it tries to recover by looking for the next token it recognizes, which is usually a ;.

 
cin subject1_age;

should be:
 
cin >> subject1_age;



Last edited on
OOOHH, I'M SO STUPID, I KNEW THIS.

... god...

Thank you guys for helping my stupid mind.
Topic archived. No new replies allowed.