Jump to content
  • Advertisement
Sign in to follow this  
Xsis

Calculator (I'm total newbie!)

This topic is 4595 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!