Problems with Calculator Program

Pages: 12
Hi,
This is my first post since i just joined. Ive wanted to learn C++ so i decided to download the Dev C++ from Bloodshed dot com. And when i made a Calculator program it had like 11 errors. even though i watched a pro on youtube make it.
I fixed a few of the problems that were just missing letters but the first problem starts at.
cin >> operation;

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <windows.h>

#include <iostream>
using namespace std;

int main()
{
	//State Variables
	int first_number;
	int second_number;
	char operation;
	int go_on = 5;
	do
	{
// Welcome the User...
cout << "******Welcome to My DOS Calculator******" << endl;
cout << "________________________________________" << endl;
cout << endl;

//Ask them to put the First Number...
cout << "Please input the First Number..." << endl;
cin >> first_number;

//Ask for the Operation...
cout << "Please input a Operation (i.e. + - / *)"
cin >> operation;

//Ask for Last Number...
cout << "Please input the Second Number..."
cin >> second_number;

//Make the Addition operation Work...
if( cin, operation == '+' )
cout << first_number << " + " << second number << " = " << first_number+second_number << endl;

//Make the Subtraction operation Work...
if( cin, operation == '-' )
cout << first_number << " - " << second number << " = " << first_numbe-second_number << endl;

//Make the Division operation Work...
if( cin, operation == '/' )
cout << first_number << " / " << second number << " / " << first_number/second_number << endl;

//Make the Multiplication operation Work...
if( cin, operation == '*' )
cout << first_number << " * " << second number << " * " << first_number*second_number << endl;
system ("pause");
system ("cls");
} while (go_on == 5);
return 0;


If u can tell me whats wrong with the whole code i would appretiate it.
Thanks,
Nfs4masters
You forgot to put semi-colons at the end of the quotes.
Also, your compiler might complain about undeclared variables because they might be spelled incorrectly so you should double check to make sure.

Try to fix the misspellings and add the missing symbols that the compiler tells you about and then try it out and see what you come up with :).

EDIT: Also, with calculators it is a good idea to use an expanded integer type such as "double" instead of "int", because double will hold multiple decimal places while int only holds a single.
Last edited on
Yeah okay now i feel stupid.
I did what u said about looking over it, and half of the problems were missing symbols and letters XD.
But now the very last problem is this part
return 0;
which i dont understand

EDIT: and if i put the "double" instead of "int" It will allow me to put more numbers in my equation correct? or is it something i dont understand correctly?
Last edited on
Does the compiler say "expected `}' at end of input?"

You probably need to add a bracket to close the program.
yes it does say that

EDIT: Put the bracket and it......WORKED.
Thank you Advent. my program can be compiled sucessfully.
But theres another problem...

It says i dont have Debugging Information? what is that?
Last edited on
Hm that's weird, so you went under 'execute' and chose to 'compile' it?

I just compiled it and fit worked fine for me. Did you try running the Exe by clicking on the actual exe file?
Yeah. I compiled it and it was fine. then when i debugged it. it said

"your project does not have debugging information. do you want to enable debugging and rebuilt your project."

so i click yes...and nothing happens

EDIT: it does show the window that says compiling and when its done i press close and nothing happens...
Last edited on
update of the coding

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <windows.h>

#include <iostream>
using namespace std;

int main()
{
	//State Variables
	int first_number;
	int second_number;
	char operation;
	int go_on = 5;
	do
	{
// Welcome the User...
cout << "******Welcome to My DOS Calculator******" << endl;
cout << "________________________________________" << endl;
cout << endl;

//Ask them to put the First Number...
cout << "Please input the First Number..." << endl;
cin >> first_number;

//Ask for the Operation...
cout << "Please input a Operation (i.e. + - / *)" << endl;
cin >> operation;

//Ask for Last Number...
cout << "Please input the Second Number..." << endl;
cin >> second_number;

//Make the Addition operation Work...
if( cin, operation == '+' )
cout << first_number << " + " << second_number << " = " << first_number+second_number << endl;

//Make the Subtraction operation Work...
if( cin, operation == '-' )
cout << first_number << " - " << second_number << " = " << first_number-second_number << endl;

//Make the Division operation Work...
if( cin, operation == '/' )
cout << first_number << " / " << second_number << " / " << first_number/second_number << endl;

//Make the Multiplication operation Work...
if( cin, operation == '*' )
cout << first_number << " * " << second_number << " * " << first_number*second_number << endl;
system ("pause");
system ("cls");
} while (go_on == 5);
return 0;
}


But please u this code so we are both on the right page. if i need more than the main.cpp file tell me because im lost here. this is a 5minute program to make and its taking me 5hours
Last edited on
this is a 5minute program to make and its taking me 5hours


dont worry, we all had that in the beginning :)
I think i remember doing this very same program (from the same tutorial), spending days on improving it :P

if you want a challenge, try adding stuff like exponents, roots etc. :)
and try to rewrite the program using a switch (see http://cplusplus.com/doc/tutorial/control/ for more info on that)

As for the program...

Try using float or double instead of int. Currently if you were to input decimal numbers, the program fails..
Also, try to avoid using this kind of while loop, because this creates an infinite loop. Try doing something like
1
2
3
4
5
6
7
char ans;
while(ans!='n') //instead of the while go_on==5 and the do loop. Just place this where the do-loop currently is :)
//code...

cout<<"would you like to continue (y/n)?"; //at the end of the program
cin>>ans;


by adding this you can also remove the system("pause") part...
Also, I don't really see why you have included windows.h. You're not using any commands of this library, so why add it? :)

Try to avoid using stuff like system("pause") and system("cls"), for more on that read: http://cplusplus.com/forum/articles/11153/

Hope it helped :)

Cheers!
I did what u said. But when i compiled and ran it. It looked like one of those spy movies when it looks like the guy is downloading information and u see that black screen with a billion words being processed. When i tried to see what it said. it Said

"would you like to continue (y/n)?"

I tried pressing n or y and enter but nothing happened (For the record, it did ask for first number, operation, and second number. its when i pressed enter for answer that it did this)

1
2
3
4
5
6
7
8
9
char ans;
while(ans!='n') //instead of the while go_on==5 and the do loop. Just place this where the do-loop currently is :)
//code...

cout<<"would you like to continue (y/n)?"; //at the end of the program
cin>>ans;
} while (go_on == 5);
return 0;
}


I had ^^^^^^^^^^ right under the multiplication code. but thank you for helping me at least get the DOS window open. lol i JUST started using C++ and im like lost on where to start.
I can do a Pythagorean Theorem program. on Python 1.3 easy, but this is different.
you should remove the while(go_on ==5);

that ought to work :)

and btw in the divisions you should make it
1
2
cout << first_number << " / " << second_number << " = " << first_number/second_number << endl;
because currently you have a "/" where the "=" is :)
and you have the same problem in the multiplications part
Last edited on
Okay now im getting compiling problems.

1
2
3
4
5
6
7
8
char ans;
while(ans!='n') //instead of the while go_on==5 and the do loop. Just place this where the do-loop currently is :)
//code...

cout<<"would you like to continue (y/n)?"; //at the end of the program
cin>>ans;
return 0;
}


ANd thanks about the Divison thing. lol
1
2
3
4
5
6
7
8
9
char ans;
while(ans!='n'){ //instead of the while go_on==5 and the do loop. Just place this where the do-loop currently is :)
//code...

cout<<"would you like to continue (y/n)?"; //at the end of the program
cin>>ans;
} //notice that the return 0 should be outside the loop, because otherwise it'd have to terminate the program every time it loops, which isn't possible :p
return 0;
} //end of main 
Last edited on
Okay your going to have to compile it with me becasue im getting problems with every change i make.

in function `int main()':
expected `while' before numeric constant
expected `(' before numeric constant
expected `)' before `,' token

is what it says on my Compiler
This is the code now...

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;

int main()
{
	//State Variables
	double first_number;
	double second_number;
	char operation;
	double go_on = 5;
	do
	{
// Welcome the User...
cout << "******Welcome to My DOS Calculator******" << endl;
cout << "________________________________________" << endl;
cout << endl;

//Ask them to put the First Number...
cout << "Please input the First Number..." << endl;
cin >> first_number;

//Ask for the Operation...
cout << "Please input a Operation (i.e. + - / *)" << endl;
cin >> operation;

//Ask for Last Number...
cout << "Please input the Second Number..." << endl;
cin >> second_number;

//Make the Addition operation Work...
if( cin, operation == '+' )
cout << first_number << " + " << second_number << " = " << first_number+second_number << endl;

//Make the Subtraction operation Work...
if( cin, operation == '-' )
cout << first_number << " - " << second_number << " = " << first_number-second_number << endl;

//Make the Division operation Work...
if( cin, operation == '/' )
cout << first_number << " / " << second_number << " = " << first_number/second_number << endl;

//Make the Multiplication operation Work...
if( cin, operation == '*' )
cout << first_number << " * " << second_number << " = " << first_number*second_number << endl;
char ans;
while(ans!='n') //instead of the while go_on==5 and the do loop. Just place this where the do-loop currently is :)
//code...

cout<<"would you like to continue (y/n)?"; //at the end of the program
cin>>ans;
} //notice that the return 0 should be outside the loop, because otherwise it'd have to terminate the program every time it loops, which isn't possible :p
return 0;
} //end of main  
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;

int main()
{
	//State Variables
	double first_number;
	double second_number;
	char operation;
	double go_on = 5;
        char ans;
	while(ans!='n')  //the while loop goes here
	{
// Welcome the User...
cout << "******Welcome to My DOS Calculator******" << endl;
cout << "________________________________________" << endl;
cout << endl;

//Ask them to put the First Number...
cout << "Please input the First Number..." << endl;
cin >> first_number;

//Ask for the Operation...
cout << "Please input a Operation (i.e. + - / *)" << endl;
cin >> operation;

//Ask for Last Number...
cout << "Please input the Second Number..." << endl;
cin >> second_number;

//Make the Addition operation Work...
if( cin, operation == '+' )
cout << first_number << " + " << second_number << " = " << first_number+second_number << endl;

//Make the Subtraction operation Work...
if( cin, operation == '-' )
cout << first_number << " - " << second_number << " = " << first_number-second_number << endl;

//Make the Division operation Work...
if( cin, operation == '/' )
cout << first_number << " / " << second_number << " = " << first_number/second_number << endl;

//Make the Multiplication operation Work...
if( cin, operation == '*' )
cout << first_number << " * " << second_number << " = " << first_number*second_number << endl;

cout<<"would you like to continue (y/n)?"; //at the end of the program
cin>>ans; //the while doesn't need to be here
} //notice that the return 0 should be outside the loop, because otherwise it'd have to terminate the program every time it loops, which isn't possible :p
return 0;
} //end of main   


K i see what you're doing.. You should've put the while loop at the place of the do loop. As a matter of fact, most programs don't use do-while-loops, but just while-loops, cuz it gives the same result.

Also try to think about the codes/ advice people give (also in the tut) and try to understand it. Just copying everything wont make you better ;)

Cheers!
Thank You it works now
i even added the system ("cls"); at the end of it to make it look fresh after I say yes.
And i undertand what you mean by not just copying the codes.

EDIT: Now this is about the theme... What program can i use to make it look like a regular windows program?
Last edited on
haha you're gonna have to stick to console programs for a loong time ;)

but after you've masterd the console programs (I suggest you at least read this tutorial: http://cplusplus.com/doc/tutorial/) then you go to windows api's. Which can be done using visual studio. and I think (BUT I'M NOT SURE) that you can also do it using a normal c++ ide like devc++, code::blocks, ms visual c++ etc..

But first master this ;)

Cheers!
Coincidently, I made GUI calculator myself and I've started programming about a month ago. I used MFC though, but MFC is great for simple applications (download visual studio express).
http://fresh-grass.deviantart.com/art/Alligator-Calculator-v1-4-173474644

I think the best way to learn is just by trying to write common programs (i.e. exercises from books). You'll learn new stuff on the way. Then come here and try to write programs for helpless users :)
Just a few things:

cout << "******Welcome to My DOS Calculator******" << endl;

You mean your calculator is a console program, rather than a GUI one. The console might look like DOS did, but it has nothing to do with it.


nfs4masters wrote:
i even added the system ("cls"); at the end of it to make it look fresh after I say yes.

You should avoid system calls. There's a good explanation here: http://www.cplusplus.com/forum/articles/11153/


kaduuk wrote:
but after you've masterd the console programs (I suggest you at least read this tutorial: http://cplusplus.com/doc/tutorial/) then you go to windows api's. Which can be done using visual studio. and I think (BUT I'M NOT SURE) that you can also do it using a normal c++ ide like devc++, code::blocks, ms visual c++ etc..

First, you don't need to "master" console programs to go on to GUI programming. There's a lot of details that are specific to console programs, and knowing them won't be useful or helpful at all when working with GUI. I'd say use console programs to get the hang of the syntax, learn how to read and write to files, parse stuff, and go on to do what interests you. Sincere interest is the most powerful motivator I know of. Put it to good use.

Second, you can certainly write Windows programs with any IDE. It's just code, after all. You just need to have the SDK available.
Pages: 12