Thread: Problem with printf (in a while loop)

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    3

    Problem with printf (in a while loop)

    Here's my code:

    Code:
    while(1){
            q1=0;
            q2=0;
            q3=0;
            q4=0;
                printf("Enter store code and quantity: ");                             //Outputs this line repeatedly
                scanf("%c%d %d", &t, &m, &q);
            if (t=='E'){                                                  //Everything past here is non essential to the problem, I'd think...
                if(m==25){                                                                   
                    q1=q;
                    q1=18.*q1;
                    total=total+(float)q1;
                    printf("\n%d Ethernet cables (25 foot) costs $%d.00 (total = $%.2f)\n", q,q1,total);
                }else if(m==50){
                    q2=q;
                    q2=30.*q2;
                    total=total+(float)q2;
                    printf("\n%d Ethernet cables (50 foot) costs $%d.00 (total = $%.2f)\n", q,q2,total);
                }
            }else if(t=='U'){
                if(m==6){
                    q3=q;
                    q3=24.*q3;
                    total=total+(float)q3;
                    printf("\n%d USB cables (6 foot) costs $%d.00 (total = $%.2f)\n", q,q3,total);
                }else if(m==10){
                    q4=q;
                    q4=36.*q4;                 
                    total=total+(float)q4;
                    printf("\n%d USB cables (10 foot) costs $%d.00 (total = $.2%f)\n", q,q4,total);
                }
            }else if(t=='Q'){
                break;
            }
        }
    What's happening is that it double posts the ''Enter store code and quantity:'' line, and I have no idea why.
    My output looks like this:

    Enter store code and quantity: E50 1


    1 Ethernet cables (50 foot) costs $30.00 (total = $30.00)
    Enter store code and quantity: E50 1
    Enter store code and quantity:
    1 Ethernet cables (50 foot) costs $30.00 (total = $60.00)
    Enter store code and quantity: _
    Last edited by dark_rug; 10-12-2012 at 07:45 PM.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Is there an identical printf statement just outside the while loop?
    ahh actually I think you must have a problem with the new line character in your scanf statement.
    Check out this link it will explain the newline in your buffer http://cboard.cprogramming.com/c-pro...-expected.html
    In short put a space in front of your %c in scanf statment... example
    Code:
    scanf(" %c%d %d", &t, &m, &q);
    Notice the space before the c.
    Last edited by camel-man; 10-12-2012 at 07:57 PM.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    Quote Originally Posted by camel-man View Post
    Check out this link it will explain the newline in your buffer http://cboard.cprogramming.com/c-pro...-expected.html
    In short put a space in front of your %c in scanf statment... example
    Code:
    scanf(" %c%d %d", &t, &m, &q);
    Notice the space before the c.
    YOU ARE A LIFE SAVER. Well, a hurting neck, bloodshot eyes-saver. I'm still confused as to why that occurs, what's that about the whitespace?

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Scanf treats the enter as a newline character '\n'. So anytime you get done entering in a number that newline character gets left in the buffer. When you come back to your scanf statement and it looks for a character it takes in the '\n', therefore seeming like it skips entering in the character.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    6
    I had no idea that about that myself. What the heck is my teacher teaching me?!

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    Thanks. I am learning to grasp the concept.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by curlygeek View Post
    What the heck is my teacher teaching me?!
    I don't know your teacher thus I can't say if he's good or bad.
    But no teacher is perfect and no teacher will tell you every single bit you need to know (be it because of time limits, lack of knowledge, ...). You should therefore always look out for additional informations yourself. Nowadays it's pretty easy to go to the search engine of your choice, enter the name of the function and you get it's manual page.

    One important part of programming is debugging which apparently isn't taught early enough. In this particular case it would be pretty easy to stop the program after every scanf()-call and just print the values of the variables and check if they are as expected (without a debugger you would use simple prints but I don't recommend that).

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Failure to create delay in a loop with printf(). Why?
    By TropicalStarfis in forum C Programming
    Replies: 2
    Last Post: 02-15-2012, 01:02 PM
  2. Simple printf and for loop help
    By andrew89 in forum C Programming
    Replies: 5
    Last Post: 12-23-2011, 05:05 AM
  3. fscanf printf while loop help
    By BrandNew in forum C Programming
    Replies: 2
    Last Post: 03-10-2011, 03:10 AM
  4. printf problem
    By blackmisery in forum C Programming
    Replies: 5
    Last Post: 11-02-2006, 04:42 AM
  5. newbie - while loop and printf
    By nickch in forum C Programming
    Replies: 7
    Last Post: 04-14-2005, 02:33 PM