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...

Recommended Answers

All 6 Replies

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.

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.

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;
     .
     .
    ;
}
}

You need a stack to implement calculator.

Member Avatar for iamthwee

You need a stack to implement calculator.

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

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.