English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I just started programming in C++ and need help. I created a basic adding program and it won't compile. What is wrong with it?
Here's the program:
//This program will add numbers
#include
#include
#include
using namespace std;

int main (int argc, char *argv[]);
{
char quit;

quit = '\0';
while (quit != 'q')
{
int x;
cout << "enter the 1st value";
cin >> x;
int y;
cout << "enter the 2nd value";
cin >> y;
int a;
a = x + y;
cout << "The sum of the two values is:";
cout << a << endl;
}
system("PAUSE");
return 0;
}

2007-08-12 10:24:22 · 3 answers · asked by unknown 1 in Computers & Internet Programming & Design

According to the compiler the error s on line 8. It says: "expected unqualified-id before '{' token"

2007-08-12 10:27:46 · update #1

I tried removig the ";" and more errors actually appeared.

2007-08-12 10:33:45 · update #2

3 answers

For starters, you DONT want a ; after your main function :

int main (int argc, char *argv[]);
^ WRONG!

Update: And the new errors are?
You probably need different header files -- specific to the compiler you are using. Which compiler are you using?

Try using stdio instead of cstdio and stdlib instead of cstdlib

2007-08-12 10:28:33 · answer #1 · answered by mdigitale 7 · 0 0

mdigitale is correct about the stray semicolon after main. He expressed it a bit inaccurately though.

You don't want a semicolon after a function *definition*. You want it after a declaration. In your case, you are defining main. You are writing the code inside it. Careful on the semicolons.

mdigitale is however, incorrect about the headers. , , and are the correct forms. The change was made in the 98/99 revision of C++. Yes, that's nearly a decade ago. If you try to compile with the C style header format in any modern compiler, you will get a diagnostic message. Usually as a warning.

Just as a note, cstdlib isn't needed because you use no functions from it.

After removing the semicolon, you should have no further errors. If you do, do tell us **verbatim** what those errors are. As magical programmers seem to be, we aren't psychics. We do not read your mind remotely.

Your logic has an error in it. The while loop depends on quit, but quit will never change because you never asks for user input.

When posting code, please use a pastebin like http://www.rafb.net/paste/ . You can google for other nopaste or pastebin sites.The idea is to preserve indentation, and perhaps have formatting so we can actually read your code. Then we can help you.

2007-08-12 11:22:00 · answer #2 · answered by csanon 6 · 0 1

along with above advices use
while(getchar(quit)!='q')

and add these lines after cout<
cout<<"Enter q to quit";

2007-08-12 23:07:30 · answer #3 · answered by i_am_the_next_best_one 5 · 0 0

fedest.com, questions and answers