0

I am trying to make it to where when I press enter on input, it will proceed to the next part of the code e.g:

int first
int second
int answer

printf("Enter first number\n");
scanf("%d", &first);

printf("Enter second number\n");
scanf("%d", &second);

first + second = answer

printf("%d", answer);
return 0

that was just an example but my code is this:

#include <stdio.h>

int main()
{
    int pennies;
    int dollars;

    printf("How many pennies are in __ dollars?\n");
    scanf("%d", &dollars);
    pennies = dollars*100;
    printf("There are %d pennies\n", pennies);
    return 0;
}

And when I run it on terminal,it will ask "How many pennies are in __ dollars?" just like it should.

The problem is when I type in 7, it will just skip down a line and go on forever. I am trying to get it to where when I press enter, it will multiply 7 * 100 and output "There are 700 pennies". Please help.

(to those who saw this earlier, I forgot to remove scant("%d%*c", dollars); and just have scanf("%d", dollars); and that solution didn't work either)

Ryan
  • 31
  • 2
  • http://ideone.com/fDWRjg Interestingly, your code works fine on ideone. But Ed's solution works too. – Baldrickk Oct 28 '14 at 13:53
  • there is no way that " go on forever.", rather it will exit the program. However to get everything to print out, follow each printf() with fflush(stdout); BTW: the current code works fine on my linux system. – user3629249 Oct 28 '14 at 14:46
  • 1) Explain "go on forever" 2) `"%d%*c"` vs. `"%d"` will not make a difference with your code. 3) What is the return value from `scanf("%d", &dollars)`? It should be 1. – chux - Reinstate Monica Oct 29 '14 at 19:39

3 Answers3

0

I'm assuming you're using %d%*c to capture the newline after the data you enter, but you should be aware that the scanf() function, for many format string specifiers, will first skip whitespace, including newline characters.

If you want to do line based input, fgets() is usually the best way, you can use the code found here as one example. That article details a method by which you can avoid many of the problems usually encountered when doing user input in C.

The bottom line is that it's usually better to get an input line with fgets() and then process it in whatever manner you need.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

I think changing this:

scanf("%d%*c", &dollars);

to:

scanf("%d", &dollars);

should solve your issue as the %*c skips character inputs, in this case probably your new line character.

Ed Smilovici
  • 194
  • 1
  • 1
  • 12
0

You have written this line wrong!

scanf("%d%*c", &dollars);

just change this to,

scanf("%d", &dollars);

Here is the Demo.

Know more about the following line where and how to use it effectively here and the this article explains it in very detail!

 scanf("%d%*c);

See the following statement

scanf("%d%*c%d",&a,&b)?

The compiler accepts input when it encounters %*c but doesn't store it.

Its mostly used for taking inputs like dates. The user may enter the date as 22/7/2012, so the compiler, takes the integer '22' and stores it, then ignores the character '/' and stores the month '7' and so on... They you can automatically store the day, month, year without having to worry about separately asking for their input.

Hope it helps!

Community
  • 1
  • 1
Blackhat002
  • 598
  • 4
  • 12