LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-09-2019, 08:19 AM   #271
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308

and if you want to simplify it:
Code:
#include <stdio.h>

char operator;

int main(void)
{

    puts("Enter operator: ");
    scanf("%c", &operator);

    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;

    }

    return 0;
}
 
1 members found this post helpful.
Old 02-09-2019, 04:02 PM   #272
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Code:
    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.
 
1 members found this post helpful.
Old 02-09-2019, 11:38 PM   #273
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
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;

}
 
Old 02-10-2019, 02:59 AM   #274
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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.
 
1 members found this post helpful.
Old 02-10-2019, 05:18 AM   #275
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Quote:
Originally Posted by jsbjsb001 View Post
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:
Code:
printf("Result is: %i remainder %i\n", firstNumber / secondNumber, firstNumber % secondNumber);

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.

Last edited by GazL; 02-10-2019 at 05:23 AM.
 
1 members found this post helpful.
Old 02-10-2019, 05:34 AM   #276
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Perhaps I'm just being thick but I don't understand:

Code:
if ( (firstNumber <= 0 ) || ( secondNumber <= 0 ) ) {
            printf("You cannot divide by 0!\n");
            return 1;	    

         }
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.

Last edited by hydrurga; 02-10-2019 at 05:39 AM.
 
2 members found this post helpful.
Old 02-10-2019, 05:46 AM   #277
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Good point Hydrurga, I meant to mention that but got distracted and forgot.
 
Old 02-10-2019, 06:14 AM   #278
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
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;

}
 
Old 02-10-2019, 06:34 AM   #279
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
You'll only get remainders on divide, no point putting them on all the others.
 
1 members found this post helpful.
Old 02-10-2019, 07:06 AM   #280
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Code:
if ( secondNumber <= 0 )
is still incorrect.

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.
 
1 members found this post helpful.
Old 02-10-2019, 07:09 AM   #281
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
I'll get the math one day, it just looks like it's going to be a very slow process as far as that's concerned.

Anyway, I've changed the code again, so hopefully it's finally right this time;

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\n", firstNumber + secondNumber);
         break;       
      
    case '-':
         printf("Result is: %i\n", firstNumber - secondNumber);
         break;
      
    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;
         
         } 
     
    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;

}
 
Old 02-10-2019, 07:22 AM   #282
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Just one thing regarding:

Code:
    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;
 
1 members found this post helpful.
Old 02-10-2019, 07:27 AM   #283
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Another point:

Code:
printf("Result is: %f remainder %i\n", (float) firstNumber / secondNumber, firstNumber % secondNumber);
I would suggest you either use integer division with a remainder, or generate a float result without a remainder, not the mixture you have here.

So:

6 / 4 = 1 remainder 2

or

6 / 4 = 1.5

NOT

6 / 4 = 1.5 remainder 2
 
1 members found this post helpful.
Old 02-10-2019, 07:27 AM   #284
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth, unfortunately...
Distribution: Currently: OpenMandriva. Previously: openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,881

Original Poster
Rep: Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063Reputation: 2063
Told I'd forgot something...

I'll get rid of the else then - I just thought for some reason it would be a good idea.

EDIT: I really done the mix of the integer and float for me - to try and remind myself what "remainder" means.

Last edited by jsbjsb001; 02-10-2019 at 07:30 AM. Reason: just seen hydrurga's post #283
 
Old 02-10-2019, 07:31 AM   #285
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by jsbjsb001 View Post
Told I'd forgot something...

I'll get rid of the else then - I just thought for some reason it would be a good idea.
It's logical, just unnecessary in this case because a true result for the if clause exits the flow altogether.
 
1 members found this post helpful.
  


Reply

Tags
c programming, learning c



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Finally decided to try to get libinput to work Timothy Miller Linux - Hardware 3 01-04-2018 08:04 PM
Decided to try Lubuntu 14.04 on my netbook... pcninja Ubuntu 4 04-20-2014 08:18 PM
Finally decided to get serious & learn a302svt LinuxQuestions.org Member Intro 1 07-19-2007 12:27 PM
Decided to try Debian some guidance required ninadb Debian 2 08-20-2004 11:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration