0

The following is an edited code snippet from one of my larger programs.
(Original code can be found here)

I've made into a run-able program (It has errors)

#include<iostream>
#include<math.h>
using namespace std;
int main(){
    char op;
    double n1,n2;
    while(true){
        cin>> n1 >> op >> n2;
        switch(op){
            case '+' : cout<<n1 + n2 ; break;
            case '-' : cout<< n1 - n2 ; break;
            case 'x' :
            case '*' : cout<< n1 * n2 ; break;
            case '/' : cout<< n1/n2 ; break;
            case '^' : cout<< pow(n1,n2); break;
            case '<' : (n1<n2)? cout<<"True":cout<<"False"; break;
            case '>' : (n1>n2)? cout<<"True":cout<<"False"; break;
            case '=' : (n1==n2)? cout<<"True":cout<<"False"; break;
            case '%': int N1 = floor(n1); int N2 = floor(n2); cout << N1 % N2; break;
            default : cout<< "That operation is not available";
        }
    }
}

Note : I am using Code::Blocks running on Windows Vista.

There are two issues that I am encountering with the code.

  • I am unable to put the % part as a case in the switch-case. My compiler is throwing an error when I do this. The errors are:
    |line 20| jump to case label [-fpermissive]
    |line 19| crosses initialization of 'int N2'
    |line 19| crosses initialization of 'int N1'

  • When I enter a gibberish value (string) for the input, the program goes into an infinite loop.

Why are these things happening?

MisterGeeky
  • 254
  • 1
  • 8
  • 18
  • 6
    What is the error you get when you make '%' a switch case? – David says Reinstate Monica Sep 26 '13 at 13:41
  • Not sure if this would matter, but do any of those need to be escaped? That's a problem that I run into when I get gibberish results in something. – zero298 Sep 26 '13 at 13:46
  • @Dgrin91: Aw shucks, that was important. Ok, I've replaced the code with the code with errors. – MisterGeeky Sep 26 '13 at 14:11
  • @zero298: I'm not sure what you mean by escaped. Btw, I don't get gibberish results. My problem is that it goes into an infinite loop when I enter gibberish into the input. I'm in search of a solution that doesn't involve terminating the loop. – MisterGeeky Sep 26 '13 at 14:26

2 Answers2

4

I am unable to put the % part as a case in the switch-case. My compiler is throwing an error when I do this.

You forgot to show us what you tried, and what the error was, but I guess you tried to declare and initialise some variables within the switch block:

switch(op){
    //...
    case '%': int N1 = floor(n1); int N2 = floor(n2); cout << N1 % N2; break;
    //...
}

That's not allowed, to prevent the program from jumping into the variables' scope without initialising them. Instead, you'll need to introduce an inner block to scope the variables within that case:

case '%': {int N1 = floor(n1); int N2 = floor(n2); cout << N1 % N2; break;}
          ^                                                               ^

or don't bother with the variables at all:

case '%': cout << floor(n1) % floor(n2); break;

When I enter a gibberish value (string) for the input, the program goes into an infinite loop.

That's because you're not checking the result of the input. The simplest fix is to end the loop when input fails:

while (cin>> n1 >> op >> n2) {
    switch (op) {
        //...
    }
}

Alternatively, you could check the result in the loop, clearing the error (with cin.clear()) if it fails.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Oh thank you :D but I've updated the code a bit and made into a runnable program. Please check it out. Sorry for not being clearer. Also, when I tried your method for %, my compiler gives the error: `invalid operands of types 'double' and 'double' to binary 'operator%'` – MisterGeeky Sep 26 '13 at 14:17
  • 1
    @Nick: that error message is correct: [floor](http://www.cplusplus.com/reference/cmath/floor/) returns "the value of *x* rounded downwards (as a floating-point value)". `%`, as the error indicates, only works on integers. Either cast the result of `floor` to `int` (and then [you don't need `floor`](http://stackoverflow.com/questions/9695329/c-how-to-round-a-double-to-an-int)) or use `fmod`. – Jongware Sep 26 '13 at 15:45
0
  • Not sure why % causes issues: can you show the error code? Sounds like a typo to me
  • You must check the success of input/conversion operations. When it fails, you never break out of the loop, since your op code is gibberish and you hit the default case.
Alexander L. Belikoff
  • 5,698
  • 1
  • 25
  • 31
  • I've edited the code and made it into a runnable program so you can see what's going wrong. I hope this makes the question more clearer. – MisterGeeky Sep 26 '13 at 14:20