So i started lurning C++ and it's going pretty well. So i thought lets start to write without any help, and the help i need search it up. But now i've made a simple program to do some mathematics and it works half.
Let me explain how it works:
First you enter a number then the second one, those 2 will be add together. After that i want to make a little menu if you want to add (+) or substract (-). Now there is where it goes wrong. The if works but the else doesn't.
See line 47.
#include <iostream>
usingnamespace std;
int main ()
{
// Declaring rVariabele 1
int rVariabele1;
cout << "Please enter the first number: ";
// Print rVariabele 1
cin >> rVariabele1;
cout << "The first number you've entered is: " << rVariabele1 << ".\n\n";
// Declaring rVariabele 2
int rVariabele2;
cout << "Please enter the second number: ";
// Print rVariabele 2
cin >> rVariabele2;
cout << "The second number you've entered is: " << rVariabele2 << ".\n\n";
// Declaring rVariabele 3
int rVariabele3;
rVariabele3 = rVariabele1 + rVariabele2;
// Print rVariabele 3
cout << "Now you have 2 numbers let's add them together!\n";
cout << "That resolves into the number: " <<rVariabele3 << ".\n\n";
// Print extra
cout << "Now if we can add them together we\n";
cout << "can probably do some more tricks.\n";
cout << "What do you want to do?\n\n";
// Option 1
int vOption;
cout << "Press 1 to do some addition (+)\n";
// Option 2
cout << "Press 2 to do some substraction (-)\n\n";
cin >> vOption;
if (vOption == 1)
cout << "You have chosen for option 1!\n";
cout << "What number shall we add to our existing number?\n";
// Declaring rVariabele 4
int rVariabele4;
cin >> rVariabele4;
// Print rVariabele 4
cout << "The number you've chosen is: \n"<< rVariabele4 << ".\n";
// Declaring rVariabele6
int rVariabele6;
rVariabele6 = rVariabele4 + rVariabele3;
cout << "That make's: " << rVariabele6 << ".\n";
else
{
cout << "hello";
}
// Terminate
return 0;
}
The error message i get when i have the else command is:
math.cc:53: error: expected primary-expression before ‘else’
math.cc:53: error: expected `;' before ‘else’
You did not include brackets for your if statement. Because there are no brackets, the lines following the if statement (with the exception of the very first line) are not part of that conditional. So, the error is reporting that the else statement does not follow an if statement.
The error message i get when i have the else command is:
math.cc:53: error: expected primary-expression before ‘else’
math.cc:53: error: expected `;' before ‘else’
|
The first error is because in line 46 you don't have the closing } or the opening ( and because of that the second error occurs thing that the else needs the ; when in truth it doesn't. remember when you have multiple statments in an if you must have the {} in them
int main ()
{
// Declaring rVariabele 1
int rVariabele1;
cout << "Please enter the first number: ";
// Print rVariabele 1
cin >> rVariabele1;
cout << "The first number you've entered is: " << rVariabele1 << ".\n\n";
// Declaring rVariabele 2
int rVariabele2;
cout << "Please enter the second number: ";
// Print rVariabele 2
cin >> rVariabele2;
cout << "The second number you've entered is: " << rVariabele2 << ".\n\n";
// Declaring rVariabele 3
int rVariabele3;
rVariabele3 = rVariabele1 + rVariabele2;
// Print rVariabele 3
cout << "Now you have 2 numbers let's add them together!\n";
cout << "That resolves into the number: " <<rVariabele3 << ".\n\n";
// Print extra
cout << "Now if we can add them together we\n";
cout << "can probably do some more tricks.\n";
cout << "What do you want to do?\n\n";
// Option 1
int vOption;
cout << "Press 1 to do some addition (+)\n";
// Option 2
cout << "Press 2 to do some substraction (-)\n\n";
cin >> vOption;
if (vOption == 1)
{
cout << "You have chosen for option 1!\n";
cout << "What number shall we add to our existing number?\n";
// Declaring rVariabele 4
int rVariabele4;
cin >> rVariabele4;
// Print rVariabele 4
cout << "The number you've chosen is: \n"<< rVariabele4 << ".\n";
// Declaring rVariabele6
int rVariabele6;
rVariabele6 = rVariabele4 + rVariabele3;
cout << "That make's: " << rVariabele6 << ".\n";
}
else
{
cout << "hello";
}
Wow tnx for the quick reply!
ill test it a.s.a.p but im updating my system now!
But i seems obvious with the { } that it will work!
Thanks alot anyway :)!
edit:/
hmmm i've used to code of gogo but now im stuck on a error with main
/math.cc:5: multiple definition of `main'
If you want to test with integers for a response like to add or subtract, you don't have to use cin>>. The best way to do it is to use the Switch() function. It'd go like this:
1 2 3 4 5 6 7 8 9 10 11
switch (vOption)
{
case 1:
code here for addition
break; // to skip the rest of the switch statement
case 2:
code here for subtraction
break;
default:
code here incase user enters anything but 1 or 2.
}
math.cc:5: multiple definition of `main'
Resolve: Look at the code and see if there are two main() functions
Or just post the code you got NOW again and i'll fix it for sure.
#include <iostream>
usingnamespace std;
int main ()
{
// Declaring rVariabele 1
int rVariabele1;
cout << "Please enter the first number: ";
// Print rVariabele 1
cin >> rVariabele1;
cout << "The first number you've entered is: " << rVariabele1 << ".\n\n";
// Declaring rVariabele 2
int rVariabele2;
cout << "Please enter the second number: ";
// Print rVariabele 2
cin >> rVariabele2;
cout << "The second number you've entered is: " << rVariabele2 << ".\n\n";
// Declaring rVariabele 3
int rVariabele3;
rVariabele3 = rVariabele1 + rVariabele2;
// Print rVariabele 3
cout << "Now you have 2 numbers let's add them together!\n";
cout << "That resolves into the number: " <<rVariabele3 << ".\n\n";
// Print extra
cout << "Now if we can add them together we\n";
cout << "can probably do some more tricks.\n";
cout << "What do you want to do?\n\n";
// Option 1
int vOption;
cout << "Press 1 to do some addition (+)\n";
// Option 2
cout << "Press 2 to do some substraction (-)\n\n";
cin >> vOption;
if (vOption == 1)
{ // Need braces {}
cout << "You have chosen for option 1!\n";
cout << "What number shall we add to our existing number?\n";
// Declaring rVariabele 4
int rVariabele4;
cin >> rVariabele4;
// Print rVariabele 4
cout << "The number you've chosen is: \n"<< rVariabele4 << ".\n";
// Declaring rVariable6
int rVariable6;
rVariable6 = rVariabele4 + rVariabele3;
cout << "That make's: " << rVariable6 << ".\n";
}
else
{
cout << "hello";
}
// Terminate
return 0;
}
Ok i've found out maybe the issue i guess..
I use netbeans on linux ubuntu 8.10 and i've found this link http://www.gidforums.com/t-8318.html
With netbeans u use "projects" with more builds in it. So i deleted the int main () from a second file and now this one works. Really strange i say so..
So from now on it's not the code with the problem :)
@Warrior2089 tnx for the switch i read it before but will now consider going even deeper in the code ^^
Thanks alot guys :)!