Question:
C++ error question............?
This is Dan.
2011-01-29 08:10:14 UTC
This is probably going to have the most obvious answer (something stupid like a type), but I'm pretty new to C++ and every time I compile the source code I slapped together, it gives me an error on the like with the "else;" statement. It says two things: expected primary expression before "else;" and expected ";" before "else;". Here's my whole code (I know it's just for fun, but I use stuff like this to get used to it; I tell the user to enter the number 17 and if they do, they get congratulated. Otherwise, they get an error message).

#include
using namespace std;

int main() {
int n;

start:

cout << "Type the number 17, then press ENTER. " << endl;
cin >> n;
if (n != 17) {
cout << "Error: please re-enter number. " << endl;
else;
cout << "Congratulations! You're good at following instructions. " << endl;
}
system ("pause");
goto start;
return 0;
}
Four answers:
Levi
2011-01-29 10:38:14 UTC
Here, it's working now. There was a mistake with the late braces...



One note about your code: do not go any further with the “go to”. It’s a dead end in modular programming and it’s abolished by many programmers/teachers...



#include

using namespace std;



int main() {

int n;



start:



cout << "Type the number 17, then press ENTER. " << endl;

cin >> n;

if (n != 17)

{

cout << "Error: please re-enter number. " << endl;

}

else

{

cout << "Congratulations! You're good at following instructions. " << endl;

}



system ("pause");



goto start;

return 0;

}
ctbuckweed
2011-01-29 10:09:45 UTC
// this compile correctly



int main()

{

    int n;



start:



    cout << "Type the number 17, then press ENTER. " << endl;

    cin >> n;

    if (n != 17) {

        cout << "Error: please re-enter number. " << endl;

    else {

        cout << "Congratulations! You're good at following instructions. " << endl;

    }

    system ("pause");

    goto start;

    return 0;}

}
H
2011-01-29 08:16:39 UTC
else is a command statement, remove that ";" after the else, coz thats an error, also, u cannot bracket the if and else together, so after your "endl;" put a "}" and just leave the else statement as is( but fix the ";" error i told you about) . Your program should now work.
?
2017-01-21 17:59:31 UTC
i'm uncertain yet it style of feels you have difficulty with IE8 source codes and MS handler records. attempt installation Microsoft seen C++ re distributable 2005 and you haven't any longer reported your service %.?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...