help new C++ progrmaer program a calcuator

A

andrewquerol

this is my source it gives me an error i cant dshiper:

#include <iostream>
using namespace std;

int main ()
{
int i;
cout << "Please enter +, -, *, or /: ";
cin >> i;
int o;
cout << "Now enter a number to " << i << ":";
cin >> o;
int p;
cout << "Now Enter a number to " << i << o;
cin >> p;
cout << "The Awnser Is: " << oip << ".\n";
}
 
O

osmium

this is my source it gives me an error i cant dshiper:

#include <iostream>
using namespace std;

int main ()
{
int i;
cout << "Please enter +, -, *, or /: ";
cin >> i;
int o;
cout << "Now enter a number to " << i << ":";
cin >> o;
int p;
cout << "Now Enter a number to " << i << o;
cin >> p;
cout << "The Awnser Is: " << oip << ".\n";

I didn't try to compile this but I would expect that one of the errors comes
down to "no variable named oip". In that context, cout expects the name of a
variable. Programiing is not something one picks up while whiling away a
Sunday afternoon. Speculating on how things might work is rarely a good
idea, certainly not for a beginner..
 
A

andrewquerol

I didn't try to compile this but I would expect that one of the errors comes
down to "no variable named oip". In that context, cout expects the name of a
variable. Programiing is not something one picks up while whiling away a
Sunday afternoon. Speculating on how things might work is rarely a good
idea, certainly not for a beginner..




- Show quoted text -- Hide quoted text -

- Show quoted text -

well there is no var named oip
because line 8 is asking for symbol for i o"i"p
say * than it would turn oip to o*p
line 11 is asking for o
say 5 than it would turn o*p to 5*p
line 14 is asking for p
say 5 than it would be 5*5
shuldent that work?
 
R

Robert Bauck Hamar

this is my source it gives me an error i cant dshiper:

What's the error?
#include <iostream>
using namespace std;

int main ()
{
int i;
cout << "Please enter +, -, *, or /: ";
cin >> i;

Here, i is an int. Will cin read a character to an int?
int o;
cout << "Now enter a number to " << i << ":";
cin >> o;
int p;
cout << "Now Enter a number to " << i << o;
cin >> p;
cout << "The Awnser Is: " << oip << ".\n";

I guess your compiler can't compile this last line? Saying something that
oip is undeclared?

The problem is that you can't do this. To say it shortly: The compiler
translates C++ expressions like

o + p

to something your computer can understand. What this is, is different on
different computers. Additionally it also depends on the types of o and p.

What I'm trying to say: If you want to read in the operator, you also have
to translate the expression yourself. There's no easy way to tell the
compiler: I want the operation described in this variable.
 
J

John Harrison

well there is no var named oip
because line 8 is asking for symbol for i o"i"p
say * than it would turn oip to o*p
line 11 is asking for o
say 5 than it would turn o*p to 5*p
line 14 is asking for p
say 5 than it would be 5*5
shuldent that work?

No it shouldn't. C++ is not a language you can just make up, you have to
follow the rules.

Something like this (untested).

int main ()
{
char i;
cout << "Please enter +, -, *, or /: ";
cin >> i;
int o;
cout << "Now enter a number to " << i << ":";
cin >> o;
int p;
cout << "Now Enter a number to " << i << o;
cin >> p;
int answer;
switch (i)
{
case '+':
answer = o + p;
break;
case '-':
answer = o - p;
break;
case '*':
answer = o * p;
break;
case '/':
answer = o / p;
break;
}
cout << "The Awnser Is: " << answer << ".\n";
}

john
 
J

JT

because line 8 is asking for symbol for i
say * than it would turn oip to o*p
...
shuldent that work?

No.

By your same argument, the line "cin >> o" will break,
because i is "*", so it becomes "c*n >> o" which doesn't work...

Do you see what I mean?

- JT
 
A

andrewquerol

What's the error?



Here, i is an int. Will cin read a character to an int?


I guess your compiler can't compile this last line? Saying something that
oip is undeclared?

The problem is that you can't do this. To say it shortly: The compiler
translates C++ expressions like

o + p

to something your computer can understand. What this is, is different on
different computers. Additionally it also depends on the types of o and p.

What I'm trying to say: If you want to read in the operator, you also have
to translate the expression yourself. There's no easy way to tell the
compiler: I want the operation described in this variable.

Your code worked like a charm thanks for teaching me that funtion

Andrew
 
A

andrewquerol

No it shouldn't. C++ is not a language you can just make up, you have to
follow the rules.

Something like this (untested).

int main ()
{
char i;
cout << "Please enter +, -, *, or /: ";
cin >> i;
int o;
cout << "Now enter a number to " << i << ":";
cin >> o;
int p;
cout << "Now Enter a number to " << i << o;
cin >> p;
int answer;
switch (i)
{
case '+':
answer = o + p;
break;
case '-':
answer = o - p;
break;
case '*':
answer = o * p;
break;
case '/':
answer = o / p;
break;
}
cout << "The Awnser Is: " << answer << ".\n";
}

john- Hide quoted text -

- Show quoted text -

Yor Code worked like a charm but i added %

so i added
case '%':
answer = o % p;
break;
Thanks for teaching that code

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top