# Thread: Problem with printf (in a while loop)

1. ## 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: _

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

3. Originally Posted by camel-man
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. 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. I had no idea that about that myself. What the heck is my teacher teaching me?!

6. Thanks. I am learning to grasp the concept.

7. Originally Posted by curlygeek
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