DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   New to C++ (http://www.daniweb.com/forums/thread162312.html)

eedythh2 Dec 15th, 2008 1:47 am
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...

StuXYZ Dec 15th, 2008 2:32 am
Re: New to C++
 
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.

cikara21 Dec 15th, 2008 3:32 am
Re: New to C++
 
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.

AHUazhu Dec 15th, 2008 4:56 am
Re: New to C++
 
 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;
    .
    .
    ;
}
}

ithelp Dec 15th, 2008 6:10 am
Re: New to C++
 
You need a stack to implement calculator.

iamthwee Dec 15th, 2008 9:52 am
Re: New to C++
 
Quote:

Originally Posted by ithelp (Post 757620)
You need a stack to implement calculator.

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

daviddoria Dec 15th, 2008 8:49 pm
Re: New to C++
 
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...


All times are GMT -4. The time now is 4:40 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC