954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

New to C++

I'm very new to C++

I have an assignment where I have to create a calculator. Should work in console window and should allow user to enter 1st then 2nd number and then operator user wishes... In addition I have to use a switch statement. Not only that but it should run inside a loop. I don't even know how to start a loop... :'(

command to exit if the ‘?’.

I'm a little lost...

eedythh2
Newbie Poster
3 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

So what is your question ??
What is a loop??

A loop is a group of statements that get called multiple times
e.g.

for(int i=0;i<10;i++)
   {
       // statements here
   }



which calls the statements 10 times
or

while(x>18)
{
   // Statements here
}


which calls the statements until x > 18. Note that if x is already
you don't get to run the statements in the loop.

There are other loop constructs, but for now: Write some code.

I would not try to do you assignment straight away, write a set of little test programs, e.g. print the number 1 to 10, then add them up.
then read a number from the keyboard etc. Post some code here, with a comment about what you can/can't understand and you will get a bit more useful feedback.

Note: We are not going to do you homework for you, but will help if you show effort.

StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
 

1. x as integer
2. y as integer
3. operator as char
4. get x from users
5. get y from users
6. get operator from users
7. switch operator, case '+': result=x+y , etc.
8. display result
9. while repeat, goto step 4
10. done.

cikara21
Posting Whiz
340 posts since Jul 2008
Reputation Points: 47
Solved Threads: 69
 
int main()
{
  int x,y;
  char opt; 
  while(1)
 {std::cout<<"please  input the x,y"<<endl;
   std:: cin>>x>>y;    
   std::cout<<"please  input the operate"<<endl;
   std::cin>>opt;
    switch (opt)
    case '+' : return x+y;
    case '-'   : return x-y;
     .
     .
    ;
}
}
AHUazhu
Newbie Poster
17 posts since Dec 2008
Reputation Points: 10
Solved Threads: 2
 

You need a stack to implement calculator.

ithelp
Nearly a Posting Maven
Banned
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128
 
You need a stack to implement calculator.

I don't think (s)he needs a stack for his/her question.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Yea - this question is clearly a "hello world" type program - they are obviously not getting into stacks yet. You're just confusing the issue instead of adding something constructive...

daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You