Thread: Help with While/Switch loop--beginner

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    2

    Help with While/Switch loop--beginner

    Hey guys, this is my first post so bear with me here. I decided to learn how to program in C and have been working through "Programming in C" by Stephen G. Kochan, though I've hit a snag on one of the problems and no one has been able to help me yet. For those who are familiar with the book it is Chapter 6, exercise 4: write a simple printing calculator that takes an accumulator and performs operations with inputted numbers and operators.

    The program I've written compiles correctly and does all the math correctly, but for some reason it skips over the first print statement and then every print output is one step behind. So for instance if I do 10 S, 5 *, the output will be nothing after the 10 S, then 10.000000 after the 5 *, etc. I've tried moving the printf to different areas in the program and the output is always the same. Here is the code:

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    
    int main (void)
    {
        float number = 0.0, accumulator = 0.0;
        char operator;
        bool end_flag = false;
    
    
        printf ("Please enter calculations:\n");
    
    
        while ( end_flag == false ) {
    
    
                scanf ("%f %c\n", &number, &operator);
                switch (operator)
                {
                    case 'S':
                        accumulator = number;
                        break;
                    case '+':
                        accumulator += number;
                        break;
                    case '-':
                        accumulator -= number;
                        break;
                    case '*':
                        accumulator *= number;
                        break;
                    case '/':
                        accumulator /= number;
                        break; 
                    case 'E':
                        end_flag = true;
                        break;
                    default:
                        printf ("Unknown operator.\n");
                        break;
                }
    
    
                printf ("= %f\n", accumulator);
    
    
             }
    
    
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    You should remove the trailing \n from your scanf call.

    It doesn't mean read exactly one \n

    It means skip all whitespace (newlines, spaces, tabs) until it finds a character which isn't whitespace.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    2
    Thank you so much Salem! I can finally move on :P So for future reference, there is no need for \n in scanf calls, right?

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Ah, Salem's too fast for me. I didn't even notice the \n in the scanf() part.

    I was going to say though, your code's logic looked fine so I was puzzled why it wouldn't work but thank God Salem noticed that 'cause I sure didn't XD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch Statement (For Beginner)
    By Spink85 in forum C Programming
    Replies: 1
    Last Post: 03-15-2013, 09:12 PM
  2. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  3. switch statement problems: beginner in C
    By cameuth in forum C Programming
    Replies: 4
    Last Post: 11-21-2011, 04:46 PM
  4. Beginner :: Script to configure Cisco Switch
    By apit in forum C Programming
    Replies: 4
    Last Post: 09-07-2009, 09:03 PM
  5. Beginner if/switch question
    By GuitarNinja in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2009, 11:11 PM