Thread: Calculator Program To Allow User 3 Inputs

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    81

    Calculator Program To Allow User 3 Inputs

    I know I'm probably writing questions along the same thing every time, but there is still stuff I don't get.

    Code:
    #include<stdio.h>
    
    int main()
    
    
    {
    int num1, num2, sum, count; /*Assign int variables of num1, num2, sum, count*/
    count = 0;
    char dummy, operation; /*Assign char variables of dummy, operation*/
    
    
    printf("Enter first number:"); /*Ask user for 1st number*/
    scanf("%d", &num1); /*Read 1st number*/
    printf("Enter operation (+ or - or * or /):"); /*Ask user for operation*/
    scanf("%c%c%c", &dummy, &operation, &dummy); /*Read operation*/
    printf("Enter second number:"); /*Ask user for 2nd number*/
    scanf("%d", &num2); /*Read 2nd number*/
    
    
    while (count <= 2); /*While count is smaller than or equal to 2 program loops*/
    
    
    {
    
    
    if ( operation == '+' ) /*If condition if operation is +*/ 
    {
    sum = num1 + num2; /*Sum if addition*/
    printf("The sum of %d and %d is %d" ,num1, num2, sum); /*Print answer to screen*/
    }
    
    
    if ( operation == '-' ) /*If condition if operation is -*/ 
    {
    sum = num1 - num2; /*Sum if subtraction*/
    printf("Subtracting %d from %d is %d" ,num1, num2, sum); /*Print answer to screen*/
    }
    
    
    if ( operation == '*' ) /*If condition if operation is **/ 
    {
    sum = num1 * num2; /*Sum if multiplication*/
    printf("Multiplying %d by %d is %d" ,num1, num2, sum); /*Print answer to screen*/
    }
    
    
    if ( operation == '/' ) /*If condition if operation is /*/ 
    {
    sum = num1 / num2; /*Sum if division*/
    printf("Dividing %d by %d is %d" ,num1, num2, sum); /*Print answer to screen*/
    }
    
    
    count = count + 1; /*count adds one*/
    }
    
    
    return 0;
    }
    This will compile, and will go so far as the second number input but then the program just stalls.

    I think if I crack this one I might be on the way to doing the others because whatever error I'm committing here is probably holding me back elsewhere.

    I understand somebody told me to use else but I tried that with last week's assignment and the basic program (without allowing 3 inputs) wouldn't work so I guessed I had to use four ifs. To do otherwise now would lead to me being even more confused I think.

  2. #2
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Go to line 20 and remove semicolon.
    Placing semicolon right after while(),means that loop and do nothing.

    I haven't read the rest but you may have issues when reading chars instead of ints. (line 15)

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You should clear the inputbuffer after every scanf.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Program now works fine, except it doesn't loop. Its like the while condition isn't there???

  5. #5
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Do you know how to use your debugger? You can walk through your code and see what it going on.

    EDIT: works for me, why are you looping anyways? Do you want this program to perform 3 different calculations?
    Last edited by rmatze; 10-19-2011 at 10:23 AM. Reason: works for me

  6. #6
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Quote Originally Posted by Interista View Post
    Program now works fine, except it doesn't loop. Its like the while condition isn't there???
    So, post your new version.
    Last edited by ch4; 10-19-2011 at 10:25 AM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    scanf("%c%c%c", &dummy, &operation, &dummy); /*Read operation*/
    What are you trying to do with this dummy character stuff? There's probably a better way.

    Code:
    while (count <= 2); /*While count is smaller than or equal to 2 program loops*/
    Ditch that semicolon, it's causing an infinite loop.

    Code:
    if ( operation == '+' ) /*If condition if operation is +*/
    I applaud your attempt to properly document your code, but stating the obvious in your comments is almost as bad as not commenting at all. Comments should only explain what is not obvious from reading the code. You can actually remove every single comment from your program, and it would still be easily understood by anybody with even a passing knowledge of C.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Quote Originally Posted by rmatze View Post
    Do you know how to use your debugger? You can walk through your code and see what it going on.
    Excuse me, but what's a debugger?

    Believe me, the course I'm on, if they can teach something they don't. They prefer to tell us a bunch of **** that has no relevance to the assignments we have to do then leave us drown.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Interista View Post
    Excuse me, but what's a debugger?
    You're excused . A debugger is probably the most useful tool in a programmer's belt. It allows you to step through your program one instruction at a time, and examine all kinds of stuff about the program while it's running, like what values are in certain variables. A good debugger does much, much more, but those are the most basic and useful features. You probably have one on your system. It often comes with the compiler.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    I'll definitely have a look at that.

    But is there any quick way to fix my code?

    I'm really up against the deadline for this week's assignment and need to get my stuff in quick. I still have 5 of 10 programs to write and am not getting any less confused.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Interista View Post
    But is there any quick way to fix my code?
    See my response in post #7.

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Its now telling me its expecting a declaration at the end of my statement.

    I've been at these 5 assignments for about 15 hours in total. I'm at breaking point. I really think I'll quit the whole course to be honest. Is not understandable at all. That's not an insult to you, you've been really helpful, but if I don't get this quick then my run is over.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Did you remove the semicolon at the end of your while statement? That was the big error. When I did that, it compiled with no errors or warnings, and produced the expected output. You need to post your new code and your exact error message (copy-paste the full message with the line number).

  14. #14
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    It compiles and runs, but instead of offering three opportunities to input number, operation, number, it just prints the answer 3 times.

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Well, look at the structure of your code. Where do you ask for input? Where is your loop? Learning to correlate program behavior with what the source code says is a fundamental skill for writing and debugging code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program for a calculator that exits if user hits 0
    By Interista in forum C Programming
    Replies: 2
    Last Post: 10-19-2011, 01:28 AM
  2. How to handle inputs if the user inputs the wrong thing
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 09-22-2010, 04:28 AM
  3. User inputs two characters and...
    By dre in forum C Programming
    Replies: 6
    Last Post: 09-18-2009, 01:51 PM
  4. program is checking for ent when user inputs out?
    By Blizzarddog in forum C++ Programming
    Replies: 1
    Last Post: 04-07-2003, 01:16 PM
  5. Using escape sequences as user inputs
    By musayume in forum C Programming
    Replies: 4
    Last Post: 12-11-2001, 09:35 AM