ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
switch (operator) {
case '+':
printf("Operator: %c\n", operator);
break;
case '-':
printf("Operator: %c\n", operator);
break;
case '/':
printf("Operator: %c\n", operator);
break;
case '*':
printf("Operator: %c\n", operator);
break;
default:
printf("You didn't type in a valid operator, you typed in: %c\n", operator);
return 1;
}
This can also be shortened to:
Code:
switch (operator) {
case '+':
case '-':
case '/':
case '*':
printf("Operator: %c\n", operator);
break;
default:
printf("You didn't type in a valid operator, you typed in: %c\n", operator);
return 1;
}
now there is nothing wrong with your version.
Specially since it looks like you may be expanding it into a simple calculator.
Doing it your way allows you to be sure all the breaks are in place, though you might want to change the formats so that each one is unique, which allows you to know which entry was used - but it isn't really necessary. The only advantage is that it lets you be certain of a case '+' really was the one taken as printing out the switch value (still useful), would look the same no matter which case entry was taken.
I'm just meaning that something like "printf("1. operator entry %c\n"..." with the "1" replaced by "2" and "3" would allow you more confidence that there is only one possiblity selected.
There are also times when the combined form is useful. Such as when skipping blanks or newlines in the input. No need for two blocks that do the same thing.
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Original Poster
Rep:
Thanks for the tips jpollard!
I wasn't thinking about expanding it into a basic calculator. I was just trying to understand the switch statement better, but it's not a bad idea to write a basic calculator, so I did. The only thing is, while it handles it if you don't type in a valid operator, it doesn't handle it properly if you try and divide a number that doesn't evenly divide. So for example if you divide 7 by 2, it gives you 3, but that's not right, given that 3 + 3 = 6, and not 7. I'm not sure exactly how I can get it to handle that situation properly. I know I have to use float, but if I do that, then it means I've gotta make the firstNumber and secondNumber variables float's which I'm not sure is wise to do. So I'm not sure exactly what I should do here?
Now from what I've been reading; a prime number is a number that cannot be evenly divided by a smaller number, and from what I read, is also called an odd number, correct? So by that logic, 7 is a prime number because if you divide it by 2, you really get 3.5
Here's my code anyways;
Code:
#include <stdio.h>
int firstNumber, secondNumber;
char operator;
int main(void)
{
puts("Enter operator and two numbers: ");
scanf("%i %c %i", &firstNumber, &operator, &secondNumber);
switch ( operator ) {
case '+':
printf("Result is: %i\n", firstNumber + secondNumber);
break;
case '-':
printf("Result is: %i\n", firstNumber - secondNumber);
break;
case '/':
if ( (firstNumber <= 0 ) || ( secondNumber <= 0 ) ) {
printf("You cannot divide by 0!\n");
return 1;
}
else {
printf("Result is: %i\n", firstNumber / secondNumber);
break;
}
case '*':
printf("Result is: %i\n", firstNumber * secondNumber);
break;
default:
printf("You didn't type in a valid operator, you typed in: %c\n", operator);
return 2;
}
return 0;
}
7 / 2 IS 3. This is integer division, not float or double.
Another operator is "%" - a modulus. This provides the remainder of an integer division. In this case 7%2 is 1, which is the remainder from a division.
The only thing is, while it handles it if you don't type in a valid operator, it doesn't handle it properly if you try and divide a number that doesn't evenly divide. So for example if you divide 7 by 2, it gives you 3, but that's not right, given that 3 + 3 = 6, and not 7. I'm not sure exactly how I can get it to handle that situation properly. I know I have to use float, but if I do that, then it means I've gotta make the firstNumber and secondNumber variables float's which I'm not sure is wise to do. So I'm not sure exactly what I should do here?
You can fix that by using a typecast:
Code:
printf("Result is: %f\n", (float) firstNumber / secondNumber);
... but don't worry about that yet, you won't see that until Chapter 6. I'm just showing it for the sake of completeness.
For now, it's probably best to just show the remainder:
Other than the above, all I have to say on this one is that in the divide section, you should have the 'break' outside of the 'else' block: you get away with it because of the return in the if, but it's not strictly right.
Oh, and your prompt string "Enter operator and two numbers:" implies that it's expecting the operator first (polish notation) when it's not, but that's just cosmetic.
You're definitely making progress.
P.S. While all primes are odd, not all odds are prime.
If you don't want to allow negative integers, or 0 for the numerator, then that's a separate thing. For example, the above will generate that error message for the following legitimate calculations:
0 / 4 which should be 0
-4 / 2 which should be -2
4 / -2 which should be -2
"You cannot divide by 0!" should only be produced where secondNumber is 0.
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881
Original Poster
Rep:
Thanks guys.
I changed the prompt of it, cuz it was a bit confusing, thanks again for your help GazL. I also did what you said with the division, and it fixed it, so now it gives the result I was trying to get, eg 7 / 2 = 3.5 I also done what you said with displaying the remainder as well.
I fixed that error message about dividing by zero, so I can see what you mean about that hydrurga. Thanks again.
I was reading a few things to try and understand what jpollard was talking about in terms of "integer division". Does it just mean that the fractional part doesn't apply, cuz that term has always confused me. I read this for one thing, but I'm still not really clear about it to be honest.
Sorry if I've forgotten anything - I tried to remember everything.
Here's my updated code anyway;
Code:
#include <stdio.h>
int firstNumber, secondNumber;
char operator;
int main(void)
{
puts("Enter numbers to calculate: ");
scanf("%i %c %i", &firstNumber, &operator, &secondNumber);
switch ( operator ) {
case '+':
printf("Result is: %i remainder %i\n", firstNumber + secondNumber, firstNumber % secondNumber);
break;
case '-':
printf("Result is: %i remainder %i\n", firstNumber - secondNumber, firstNumber % secondNumber);
break;
case '/':
if ( secondNumber <= 0 ) {
printf("You cannot divide by 0!\n");
return 1;
}
else {
printf("Result is: %f\n", (float) firstNumber / secondNumber);
break;
}
case '*':
printf("Result is: %i remainder %i\n", firstNumber * secondNumber, firstNumber % secondNumber);
break;
default:
printf("You didn't type in a valid operator, you typed in: %c\n", operator);
return 2;
}
return 0;
}
It is only if secondNumber is exactly equal to 0 that an error about division by zero should be generated. If it is -4, for example, then that would be legitimate.
case '/':
if ( secondNumber == 0 ) {
printf("You cannot divide by 0!\n");
return 1;
}
else {
printf("Result is: %f remainder %i\n", (float) firstNumber / secondNumber, firstNumber % secondNumber);
break;
}
I don't know if this has been mentioned elsewhere but personally I wouldn't bother using the else clause in this case. If the if statement works out to be true then you are going to return with no further processing required. Therefore I would code it is as:
Code:
case '/':
if ( secondNumber == 0 ) {
printf("You cannot divide by 0!\n");
return 1;
}
printf("Result is: %f remainder %i\n", (float) firstNumber / secondNumber, firstNumber % secondNumber);
break;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.