Even or odd followed by multiplication

I'm working on a homework assignment for a beginning C++ class and I'm a bit lost.
Here's the assignment:

Create a c++ program which ask the user to input a number.

The output of the program should be one of the following: You entered an EVEN number. OR You entered an ODD number.

if the user entered an ODD number, ask them to enter another number. Multiply this number by the first number and output the result.

The even/odd part is pretty easy- I got that to work. I've gotten completely lost on the second part. I'm currently getting the following two errors in a few different lines:

error: expected primary-expression before ‘;’ token

error: expected ‘}’ before ‘else’
If anyone could give me a hint as to what I'm doing wrong, I'd greatly appreciate it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include <iostream>
#include <cstdlib>
using namespace std;

int main () {
        int num1; // This is the original number entered by the user.
        int num2; // This is the second number entered if the first number is odd.
        cout << "Enter a number: "<<;
        cin >> num1 >> ;
        if (num1 % 2 == 0) {
                cout << num1 << " Your number is even." << endl;
        }
         else   {
                cout << num1 << " Your number is odd. Please enter another number: " << endl;
                cin >> num2 >> ;
                cout << " Your two numbers multiplied equals: " << (num1 *= num2) << endl;
        }
} // end of main ()
Does your compiler not show you which line the error is from.
Line 9: cin >> num1 >> ;
change to cin>>num1;
It does. I've pasted below. Also, I thought C++ didn't care about spaces?

021015_ltalon.cpp: In function ‘int main()’:
021015_ltalon.cpp:8:36: error: expected primary-expression before ‘;’ token
cout<< "Enter a number: "<<;
^
021015_ltalon.cpp:9:20: error: expected primary-expression before ‘;’ token
cin>>num1>>;
^
021015_ltalon.cpp:15:28: error: expected primary-expression before ‘;’ token
cin>>num2>>;
^
Last edited on
you still dont get it.
Its not about space. its about >>;

this >> is steam extraction operator.
You must extract from a stream into something.
just like int cin>>num1.
You cannot do cin>>; extraction into nothing.
Last edited on
Nope. I totally don't get it. That's why I'm taking a beginning C++ class.
I did figure out the additional >> at the end and changed my code to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstdlib>
using namespace std;

int main () {
        int num1; // This is the original number entered by the user.
        int num2; // This is the second number entered if the first number is odd.
        cout<< "Enter a number: "<<endl;
        cin>>num1;
        if (num1 % 2 == 0) {
                cout<< num1<< " Your number is even." <<endl;
        }
         else   {
                cout<<num1<< " Your number is odd. Please enter another number: " <<endl;
                cin>>num2;
                cout<< " Your two numbers multiplied equals: " <<(num1*num2)<<endl;
        }
} // end of main ()


Now I'm getting the following error:

021015_ltalon.out: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/../lib64/crt1.o:(.text+0x0): first defined here
021015_ltalon.out: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/../lib64/crti.o:(.fini+0x0): first defined here
021015_ltalon.out:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/../lib64/crt1.o:(.rodata.cst4+0x0): first defined here
021015_ltalon.out: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/../lib64/crt1.o:(.data+0x0): first defined here
021015_ltalon.out: In function `__data_start':
(.data+0x8): multiple definition of `__dso_handle'
/opt/gcc4.9.1/lib/gcc/x86_64-unknown-linux-gnu/4.9.1/crtbegin.o:(.data+0x0): first defined here
021015_ltalon.out: In function `main':
(.text+0x168): multiple definition of `main'
/tmp/ccO3LJa2.o:021015_ltalon.cpp:(.text+0x0): first defined here
021015_ltalon.out: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/../lib64/crti.o:(.init+0x0): first defined here
/opt/gcc4.9.1/lib/gcc/x86_64-unknown-linux-gnu/4.9.1/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
021015_ltalon.out:(.dtors+0x8): first defined here
/opt/gcc4.9.1/lib/gcc/x86_64-unknown-linux-gnu/4.9.1/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
021015_ltalon.out:(.data+0x10): first defined here
/usr/bin/ld: error in 021015_ltalon.out(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status

I have never gotten an error like this. It's an ugly one! The only thing I can think is that I'm not using the correct library? Am I close?
Last edited on
There is nothing wrong with the edited code.
From your errors, i understand that you are compiling on linux from the terminal.
Also, you might have made a mistake to use gcc instead of g++.
Hm. I usually use c++ to compile. I'll try g++. Thanks!
Topic archived. No new replies allowed.