• Advertisement
Sign in to follow this  

Calculator (I'm total newbie!)

This topic is 4325 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, i've been trying to learn c++ these days, and now I wanted to try to make a small calculator! just one problem, I can't remember what i'm doing wrong here, I know theres something wrong. just take a look at the code, I can't seem to find it, (I know, it's screwed)
#include <iostream>
using namespace std;

int main()

{
    int n1;
    char var;
    int n2;
    
    cout<<"please type in first number: \n";
    cin>> n1;
    cout<<"\n /*-+ ?\n";
    cin>> var;
    cout<<"please type in the number you wish to"<< var <<" with. \n";
    cin>> n2;
     
    if ( var == * ) {
            cout<< n1*n2 "is your answer";
            }
    if (var == /) {
            cout<< n1/n2 "is your answer";
}
    if (var == -) {
            cout<< n1-n2 "is your answer";
            }
    if (var == +) {
            cout<< n1+n2 "is your answer";
            }
    if (var == default) {
            cous<<"that's not a valid option";
            }
            cin.get();
            }
            

Share this post


Link to post
Share on other sites
Advertisement
In your if(var==...) statements, you need to enclose the characters with single quotes, like

if(var=='+') ...

HTH

[Edit] Oh, and if(var==default) does not make any sense and will not compile. Instead, you'd be better to go

if(var=='+')
...
else if(var=='-')
...
else if(var=='*')
...
else if(var=='/')
...
else cout << "not a valid operation\n";

Share this post


Link to post
Share on other sites
Thanks :-D I thought I was missing something!!

Now for the testing! :-D Thanks for your reply!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Try putting +,-,/ and * in single quotes like this
var == '*'

also var == default won't work (what is default anyway?)
If I were I would use switch and cases

Share this post


Link to post
Share on other sites
The Default was meant to mean like,

566 / 0 = error. (not possible)

but i'm trying to figure out how to do that with a If Statement, just not going to good i must admit! i'm pretty stuck! so that's a new problem I need to get solved! :-)

Share this post


Link to post
Share on other sites
Quote:
Original post by Xsis

if ( var == * ) {
cout<< n1*n2 "is your answer";
}
if (var == /) {
cout<< n1/n2 "is your answer";
}
if (var == -) {
cout<< n1-n2 "is your answer";
}
if (var == +) {
cout<< n1+n2 "is your answer";
}



You need an << between the calculation and the next string, so:

cout<< n1-n2 << "is your answer";


Also, were you confused about the divide by zero? If you just want something simple, you can just have an if statment inside the if statement for division:

if ( var == / ) {
if( n2 == 0 ){
cout << "Error: can't do divide-by-zero.";
}
else{
cout<< n1/n2 << "is your answer";
}
}

Share this post


Link to post
Share on other sites
Well, for division by zero error, simplest way would be


....
else if(var=='\')
{
if(n2==0)
{
cout << "division by zero error\n";
return 0;
}

cout << n1/n2 << endl;
}
else ...



but post back if you have any problems. I dig writing calculators.

Share this post


Link to post
Share on other sites
Wow, actually I didn't know you could make a If statement in a If statement! sounds pretty damn screwed up if you ask me! but now I know :-D

Share this post


Link to post
Share on other sites
Quote:
Original post by Xsis
Wow, actually I didn't know you could make a If statement in a If statement! sounds pretty damn screwed up if you ask me! but now I know :-D


You can have an if inside an if inside an if inside an if, ad infinitum.

Okay, well not really to infinity, but I don't know the limit. Does anyone know? Does anyone have the C/C++ standard lying around?

Anyway, you can also have loop nested as well. So, you can have a while inside a while, inside a for, etc, etc.

You can have pretty much any if/else, loop construct nest inside another construct (doesn't have to be the same constructor or the same type of construct).

Share this post


Link to post
Share on other sites
How rare! :-P

Well now i just noticed, Either I need to change the hole code, or something is totally wrong here, It compiles ok! but, everytime I put in a number, the char and then a number again, press the enter button, It should give me the result of the calculation. right? but it just closes as quick as I have entered the enter button :-P


 #include <iostream>
using namespace std;

int main()

{
int n1;
char var;
int n2;

cout<<"please type in first number: ";
cin>> n1;
cout<<"\n /*-+ ? ";
cin>> var;
cout<<"please type in the number you wish to "<< var <<" with: ";
cin>> n2;

if ( var == '*' ) {
cout<< "\nYour Answer is: "<< n1*n2;
}


if (var == '-') {
cout<< "\nYour Answer is: "<< n1-n2;
}
else if (var == '+') {
cout<< "\nYour Answer is: "<< n1+n2;
}
else if ( var == '/' ) {
if( n2 == 0 ){
cout << "Error: can't do divide-by-zero.";
}
else {
cout<< "\nYour Answer is: "<< n1/n2;
}

cin.get();
}

cout<< "\n\n\nThank you for trying my new calculator.\n Made By Xsis";

cin.get();
}

Share this post


Link to post
Share on other sites
Well, I've never used it but I think cin.get() SHOULD make your program wait for a key before exiting. Code looks okay at first glance.

Easiest way to see if this is the problem IMO is to run a DOS prompt, cd to the directory the executable is in and run it from there, then the screen output will be preserved even if the program is terminating automatically without waiting for a key.

If you are developing for Win32, you could try system("pause"); at the end (you'll need stdlib.h) but this is highly implementation dependant.

Share this post


Link to post
Share on other sites
I think you should use switch instead of many if/else. Because switch is faster than if/else.

switch(var)
case '+':
....
break;

case '-':
....
break;

case '/':
....
break;

case '*':
....
break;

default:
....
}

Share this post


Link to post
Share on other sites
you can just use getchar() for C code too

that makes to program wait till you press a key

just put that at the end of your code

Share this post


Link to post
Share on other sites
Quote:


Originally Posted By Easily Confused

Well, I've never used it but I think cin.get() SHOULD make your program wait for a key before exiting. Code looks okay at first glance.

Easiest way to see if this is the problem IMO is to run a DOS prompt, cd to the directory the executable is in and run it from there, then the screen output will be preserved even if the program is terminating automatically without waiting for a key.

If you are developing for Win32, you could try system("pause"); at the end (you'll need stdlib.h) but this is highly implementation dependant.


Well it works fine I see! so just the pause thing, and Invisal, I'm trying not to change the code completely, but thanks for the advice :-D I'll use it the next time!

Share this post


Link to post
Share on other sites
Quote:
Original post by Endar
You can have an if inside an if inside an if inside an if, ad infinitum.

Okay, well not really to infinity, but I don't know the limit. Does anyone know? Does anyone have the C/C++ standard lying around?


There is no limit. The program needs to fit into memory, though.

Share this post


Link to post
Share on other sites
That's correct :-D
Hehe!

found out the calculator close problem, it seems that the last time you press enter (at the second number) it activates the next cin.get() :-) so just placed one more and it works! :-D

Share this post


Link to post
Share on other sites
Yeah, actually don't use system("pause") ever. Someone has just pointed out in another thread that if someone malicious puts a program called pause.exe in the same directory as your program, they can do anything they want when your program ends. Hadn't thought of that.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement