Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,374 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,185 people online right now. Registration is fast and FREE... Join Now!




four function calculator with user input and switch statement

2 Pages V  1 2 >  
Reply to this topicStart new topic

four function calculator with user input and switch statement

ser
post 29 Oct, 2006 - 09:10 AM
Post #1


D.I.C Head

**
Joined: 29 Oct, 2006
Posts: 55


My Contributions


this is the code that i have done but i seem to have an error but i'm not sure where. this is the code:
CODE

#include <iostream.h>
#include<coino.h>
void main()
{
int number1, number2;
cout << "Please enter a number: ";
cin >> number1;
cout << "Please enter another number: ";
cin >> number2;
cout << "Please enter a a valid operator: ";
cin >> op;
switch (op)
{
case +:
cout << "The result is: " << number1 + number2 << endl;
break;
case -:
cout << "The result is: " << number1 - number2 << endl;
break;
case *:
cout << "The result is: " << number1 * number2 << endl;
break;
case /:
cout << "The result is: " << number1 / number2 << endl;
break;
default: cout << "You Have entered an invalid operator." << endl;
break;
cout<< "do you want to another calculation? (y or n)";
}
getche();
}
User is offlineProfile CardPM

Go to the top of the page

BitByte
post 29 Oct, 2006 - 09:37 AM
Post #2


D.I.C Head

**
Joined: 9 Aug, 2006
Posts: 194



Thanked 2 times
My Contributions


You need to put the operators in single quotes: '*' '/' etc..

EDIT: Actually looking at it again, you have a few errors. main() must return an int so its

CODE
int main()


and

CODE
return 0;


you don't need the conio.h header file, unless you are adding more to the program. getche(), not needed. Use cin.get(); And finally, op is not declared. Declare it as:

CODE
char op;


drop the .h on the iostream header file. It's C++. And, jst noticed this aswell. Put using namespace std; below you header files.

This post has been edited by BitByte: 29 Oct, 2006 - 09:58 AM
User is offlineProfile CardPM

Go to the top of the page

ser
post 29 Oct, 2006 - 09:47 AM
Post #3


D.I.C Head

**
Joined: 29 Oct, 2006
Posts: 55


My Contributions


this is the code but i still have an error even when i have added the ' ' to the operators.

CODE

#include <iostream.h>
#include<coino.h>
void main()
{
int number1, number2;
cout << "Please enter a number: ";
cin >> number1;
cout << "Please enter another number: ";
cin >> number2;
cout << "Please enter a a valid operator: ";
cin >> choice;
switch (choice)
{
case '+':
cout << "The result is: " << number1 + number2 << endl;
break;
case '-':
cout << "The result is: " << number1 - number2 << endl;
break;
case '*':
cout << "The result is: " << number1 * number2 << endl;
break;
case '/':
cout << "The result is: " << number1 / number2 << endl;
break;
default: cout << "You Have entered an invalid operator." << endl;
break;
cout<< "do you want to another calculation? (y or n)";
}
getche();
}
User is offlineProfile CardPM

Go to the top of the page

BitByte
post 29 Oct, 2006 - 09:51 AM
Post #4


D.I.C Head

**
Joined: 9 Aug, 2006
Posts: 194



Thanked 2 times
My Contributions


Look at my last post. It would help next time if you post the error messages you are getting. Helps us help you. biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

ser
post 29 Oct, 2006 - 10:05 AM
Post #5


D.I.C Head

**
Joined: 29 Oct, 2006
Posts: 55


My Contributions


i have added the changes but i still have an error.
CODE

#include <iostream>
#include<coino.h>
int main()
{
int number1, number2;
cout << "Please enter a number: ";
cin >> number1;
cout << "Please enter another number: ";
cin >> number2;
cout << "Please enter a a valid operator:;
cin.get();
char op();
switch (cop)
{
case '+':
cout << "The result is: " << number1 + number2 << endl;
break;
case '-':
cout << "The result is: " << number1 - number2 << endl;
break;
case '*':
cout << "The result is: " << number1 * number2 << endl;
break;
case '/':
cout << "The result is: " << number1 / number2 << endl;
break;
default: cout << "You Have entered an invalid operator." << endl;
break;
cout<< "do you want to another calculation? (y or n)";
}
return o;
}
User is offlineProfile CardPM

Go to the top of the page

Xing
post 29 Oct, 2006 - 10:08 AM
Post #6


D.I.C Addict

Group Icon
Joined: 22 Jul, 2006
Posts: 723



Thanked 2 times

Dream Kudos: 1575
My Contributions


ser, what compiler are you using?
Any descent C++ compiler would warn you against using old .h headers. And read BitByte's complete post.
User is offlineProfile CardPM

Go to the top of the page

BitByte
post 29 Oct, 2006 - 10:13 AM
Post #7


D.I.C Head

**
Joined: 9 Aug, 2006
Posts: 194



Thanked 2 times
My Contributions


CODE
#include <iostream>
//#include<coino.h> take this out

using namespace std;

int main()
{
int number1, number2;
cout << "Please enter a number: ";
cin >> number1;
cout << "Please enter another number: ";
cin >> number2;
cout << "Please enter a a valid operator: ";  // <-- missing quotes
char op; // don't put brackets at the end of char op;
cin >> op; // forgot this
// cin.get(); // this should be before return 0;

switch (op) // you put cop not op
{
case '+':
cout << "The result is: " << number1 + number2 << endl;
break;
case '-':
cout << "The result is: " << number1 - number2 << endl;
break;
case '*':
cout << "The result is: " << number1 * number2 << endl;
break;
case '/':
cout << "The result is: " << number1 / number2 << endl;
break;
default: cout << "You Have entered an invalid operator." << endl;
break;
cout<< "do you want to another calculation? (y or n)";
}
return 0; // return 0 (ZERO) not o
}


copy and paste this into your IDE
User is offlineProfile CardPM

Go to the top of the page

ser
post 29 Oct, 2006 - 10:19 AM
Post #8


D.I.C Head

**
Joined: 29 Oct, 2006
Posts: 55


My Contributions


the compiler taht i'm using is microsft visual c++. sorry but the only error message that i'm getting is 1 error i dont even know where the error is.

CODE

#include <iostream>
#include<coino.h>
namespace std;
int main()
{
int number1, number2;
cout << "Please enter a number: ";
cin >> number1;
cout << "Please enter another number: ";
cin >> number2;
cout << "Please enter a a valid operator:;
cin.get();
char op();
switch (op)
{
case '+':
cout << "The result is: " << number1 + number2 << endl;
break;
case '-':
cout << "The result is: " << number1 - number2 << endl;
break;
case '*':
cout << "The result is: " << number1 * number2 << endl;
break;
case '/':
cout << "The result is: " << number1 / number2 << endl;
break;
default: cout << "You Have entered an invalid operator." << endl;
break;
cout<< "do you want to another calculation? (y or n)";
}
return o;
}
User is offlineProfile CardPM

Go to the top of the page

BitByte
post 29 Oct, 2006 - 10:27 AM
Post #9


D.I.C Head

**
Joined: 9 Aug, 2006
Posts: 194



Thanked 2 times
My Contributions


You are missing the " at the end of the sentence where you ask for the second number. If you double click on the error message in visual c++ it will go to the line where the error is. Did you not look at the last post i did? It shows you where all the errors are.

Right here it is again. Now look at this one and compare it to yours. Look at mine and then yours and you will see all the mistakes you have made. Don't be shy about using spaces, it makes your code so much easier to read and the compiler ignores them anyway.

CODE
#include <iostream>

using namespace std;

int main()
{
    int number1, number2;
    char op;

    cout << "Please enter a number: ";
    cin >> number1;

    cout << "Please enter another number: ";
    cin >> number2;

    cout << "Please enter a a valid operator: ";
    cin >> op;


switch (op)
{
    case '+' :
        cout << "The result is: " << number1 + number2 << endl;
        break;

    case '-' :
        cout << "The result is: " << number1 - number2 << endl;
        break;

    case '*' :
        cout << "The result is: " << number1 * number2 << endl;
        break;

    case '/' :
        cout << "The result is: " << number1 / number2 << endl;
        break;

    default  :
        cout << "You Have entered an invalid operator." << endl;
        break;

    cout << "do you want to another calculation? (y or n)";
}

    return 0;
}


This post has been edited by BitByte: 29 Oct, 2006 - 10:23 AM
User is offlineProfile CardPM

Go to the top of the page

horace
post 29 Oct, 2006 - 10:28 AM
Post #10


D.I.C Addict

Group Icon
Joined: 25 Oct, 2006
Posts: 573



Thanked 4 times

Dream Kudos: 50
My Contributions


a few more errors found - ammended program below - altered lines indicated by **
CODE

#include <iostream>
//#include<coino.h>             ** remove - it should be conio.h !!
using namespace std;            // ** add using!
int main()
{
int number1, number2;
cout << "Please enter a number: ";
cin >> number1;
cout << "Please enter another number: ";
cin >> number2;
cout << "Please enter a a valid operator: "; // ** add a "
//cin.get();                                 ** remove
char op;                                       // ** remove ()
cin >> op;                                    // ** add
switch (op)
{
case '+':
cout << "The result is: " << number1 + number2 << endl;
break;
case '-':
cout << "The result is: " << number1 - number2 << endl;
break;
case '*':
cout << "The result is: " << number1 * number2 << endl;
break;
case '/':
cout << "The result is: " << number1 / number2 << endl;
break;
default: cout << "You Have entered an invalid operator." << endl;
break;
cout<< "do you want to another calculation? (y or n)";
}
cin.get();                                     // ** add
cin.get();                                     // ** add
return 0;                                      //** replace o with 0
}
User is offlineProfile CardPM

Go to the top of the page

ser
post 29 Oct, 2006 - 10:39 AM
Post #11


D.I.C Head

**
Joined: 29 Oct, 2006
Posts: 55


My Contributions


thank you very much for your help it know works i have seen where i was going wrong thanks again
User is offlineProfile CardPM

Go to the top of the page

ser
post 29 Oct, 2006 - 10:50 AM
Post #12


D.I.C Head

**
Joined: 29 Oct, 2006
Posts: 55


My Contributions


this part of the code does not work and i need it to be shown on the screen
[code]
cout << "do you want to do another calculation? (y or n)";
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 11/22/08 06:02AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month