"if... else" and "loops"?? What am I missing?

Pages: 12

As for your code, you want to put the checks for whether the user entered 0 before you output the answer or your program is going to crash. Also even if the user does enter 0 you're not doing anything about, and whenever you divide by zero on a computer the program crashes, so try using exit(1) or return 0.


Sorry, I don't follow you. How can I do something about it?



The reason the else on line 92 keeps getting displayed is because you left out your curly braces on line 81 so the else on line 92 is an else to the if statement on line 87. This means it will be displayed whenever the user doesn't enter 0 when they're dividing. Just give your else if statement curly braces and you'll be good.


Ok... Like this?

1
2
3
4
5
6
7
8
9
10
11

else if (function == '%'){      //Line 81            
                   
                   
                 cout  <<  "The answer to  "  << m  <<  "  "  << function  <<  "  "  << n  <<  "   "  
                   <<  "is:   "<<  m % n  <<  endl;
              
                 if (n == 0)
              
                   cout << "Cannot modulus by 0.  Please re-enter."  << endl;
                   }      
Yes, that should fix the problem with the else statement on line 92. What I meant by put the check before you output the answer is that whenever you divide by zero your program will crash. At the moment you are dividing by zero before you check if n is equal to 0. To fix this change your code to something like this:
1
2
3
4
5
6
7
8
9
10
else if (function == '%')
{        
	if(n == 0)
	{
		std::cout << "Cannot modulus by 0." << std::endl;
		return 0;
	}
        cout  <<  "The answer to  "  << m  <<  "  "  << function  <<  "  "  << n  <<  "   "  
        <<  "is:   "<<  m % n  <<  endl;          
} 


This will check if you're dividing by zero first and exit the program if you are. Otherwise it will carry out the operation.
Last edited on
hmm... hope this will help:

you must to know that it's like a script / scenario of (how) your program will react, e.g:

1
2
3
4
5
6
7
8
9
10
else if (function == '%'){      //Line 81            
                   
                   
                 cout  <<  "The answer to  "  << m  <<  "  "  << function  <<  "  "  << n  <<  "   "  
                   <<  "is:   "<<  m % n  <<  endl;
              
                 if (n == 0)
              
                   cout << "Cannot modulus by 0.  Please re-enter."  << endl;
                   }     


the computer will read the first instruction when / if it met the condition. like you reading a book or a scenario, from up-down, left-right, right? so (maybe) this is what the computer think:

computer: "the function is '%', what am i gonna do? oh tell the user "the answer to (variable_m) % (variable_n) is (variable_m % variable_n)"

the computer does it because it was the first instruction from the program (code) when the computer met that condition. while the computer calculate the division it crash because integer cannot divided by 0. so, if you met that condition (division by 0) the if line will never be exceuted.

btw, you know the use of curly braces, right?
I got it to work correctly. I really appreciate the help.

Thanks,

Ray
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std ;
int main(int nNumberofArgs, char* pszArgs[])
{
double value1;
double value2;
cout << "Welcome to the Calculator....\nEnter First number...";
cin >> value1;
cout << "Enter Second number...";
cin >> value2;
int choice;
cout << "Choose the operation to be applied :\n1 for addition\n2 for subtraction\n3 for multiplication\n4 for division\n";
cin >> choice;

switch (choice)
{
case 1 :
cout << "The sum is " << value1+value2 << endl;
break ;
case 2:
cout << "The difference is " << value1-value2 << endl;
break ;
case 3:
cout << "The product is " << value1*value2 << endl;
break ;
case 4:
cout << "The quotient is " << value1/value2 << endl;
break ;
default :
cout << "You didn't enter any correct binary operation"
<< endl ;
break;
}

system("PAUSE");
return 0;
}
Topic archived. No new replies allowed.
Pages: 12