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?