0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
> what is goto statement ?
http://lmgtfy.com/?q=c%2B%2B+goto

> and how can i write c,q,ln ?
Don't understand the question. cout << c << q << ln << endl; ? Or did you mean something other than "display the contents of the variables on the screen"?
0
cooop's Avatar, Join Date: Jul 2009
Go4Expert Member
i mean how can i write this ?
‘c’ to clear the current accumulator
‘n’ for ln
‘q’ for quit
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Oh I see. You can't. The way you've done it - taking numeric input for the operation the user wants to do - means you can only accept numerical commands. But the assignment explicitly says you should get + for add, - for subtract and so on. So instead of int which_one; cin >> which_one; you need to declare which_one to be a char so that the user can enter a character command.

Also note the assignment states "Use a separate function enter_code to prompt the user for instruction code" and mentions another function as well - if you don't do this then after all the work you've done you'll still fail it.

Untested:
Code:
char enter_option()
{
  char option;
  int valid=0;
  while (!valid)
  {
    cin >> option;
    if (strchr(option,"+-*/pcslnq"))
      valid=1;
    else
      cout<<"Invalid operation; please re-enter"<<endl;
  }
  return option;
}
The assignment doesn't ask you to display a menu.

One thing I'm not clear on is why left_operand *and* right_operand are needed. On a handheld calculator typically you use the current accumulator and a single new number, so you would enter + to add, then enter a new number and press =, and that would add the new number to the accumulator. Perhaps you could ask what should happen if the user enters +: does the program ask for two numbers, or does it ask for one and add the number to the accumulator? If it asks for two numbers and + adds those, what does it do with the number in the accumulator - does the new result overwrite that?

More untested:
Code:
int main()
{
  char opt;
  float left_operand=0,right_operand=0,acc=0;
  top:
  opt=enter_option();
  if (opt=='q')
    return 0;
  enter_operand(&left_operand,&right_operand,opt);
  acc=compute(left_operand,right_operand,opt);
  cout<<acc;
  goto top;
  return 0;
}

void enter_operand(float *left, float *right, char opt)
{
  if (strchr(opt,"+-*/psln"))
  {
    cin>>*left;
  }
  if (strchr(opt,"+-*/p"))
  {
    cin>>*right;
  }
}

float compute(float left, float right, char opt)
{
  switch (opt)
  {
  case '+': return left+right;
  case '-': return left-right;
  case '*': return left*right;
  case '/': return left/right;
  case 'p': return pow(left,right);
  case 'c': return 0;
  case 's': return sqrt(left);
  case 'l': return log(left);
  case 'n': return ln(left);
  }
  return 0;
}
Well there you go. Surprised myself; I don't normally do homework, but watching you struggle with this is too painful. This is the first pass - I haven't compiled this code so I don't know if there are any silly errors (there probably are). You'll have to figure out what to #include to make the function calls strchr, log, pow etc work OK but I imagine those will be in your course notes somewhere.

I've resolved the question about the accumulator by having the result overwrite the previous value. If you want to add up a few numbers then you'll need to keep re-entering the current result, e.g. to add 2,3,5,2 you need to do 2+3=5, 5+5=10, 10+2=12. This is not like a calculator where you would usually do 2+3+5+2= and that would display 12 with the interim results displayed as you enter each option.
0
cooop's Avatar, Join Date: Jul 2009
Go4Expert Member
switch (opt)
{
case '+': return left+right;
case '-': return left-right;
case '*': return left*right;
case '/': return left/right;
case 'p': return pow(left,right);
case 'c': return 0;
case 's': return sqrt(left);
case 'l': return log(left);
case 'n': return ln(left);

can i use if statement instead of witch ?
cuz i cant use anything but "if" in this hw ;S
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Yes; switch statements are functionally equivalent to if/else if ladders and are easier to read. You can replace
Code:
  switch (opt)
  {
  case '+': return left+right;
  case '-': return left-right;
  case '*': return left*right;
  // etc
  }
with
Code:
if (opt=='+') return left+right;
else if (opt=='-') return left-right;
else if (opt=='*') return left*right;
// etc