1

In my program I'm just calculating the costs of things. However, at the end I want a little break at the program asking for the user to just press the Enter button. I supposed getchar() would work here but it doesn't even stop, it just continues to keep printing. I even tried to put a space after the scant formats like scanf("%s ").

So two things how do I stop the program to ask for input at getchar() and how do I make it recognize just a enter button.

#include <stdio.h>
#include <stdlib.h>

int main()
{

    char hotels_houses[5];
    int houses, hotels, cost;


    printf("Enter amount of houses on board: \n");
    scanf("%s", hotels_houses);
    houses = atoi(hotels_houses);

    printf("Enter amount of hotels on board: \n");
    scanf("%s", hotels_houses);
    hotels = atoi(hotels_houses);


    printf("Cost of houses: %d\n", houses);
    printf("Cost of hotels: %d\n", hotels);


    cost = (houses *40) + (hotels * 115);


    puts("Click enter to calculate total cash ");
    getchar();                  /* just a filler */
    printf("Total cost: %d\n", cost); 

    return(0);
}
  • You can't tell whether it is working because you don't test the result. It is working; it is returning the newline after the number of hotels on the board (`scanf()` leaves characters that are not read by the conversion specifications in the input). You might find it easier to read whole lines and then scan them with `sscanf()`. You should consider using `if (scanf("%d", &hotels) != 1) { ...report error... }` using `scanf()` to do the conversion and checking that there is no error. – Jonathan Leffler Feb 27 '14 at 23:23
  • possible duplicate of [getchar does not stop when using scanf](http://stackoverflow.com/questions/12653884/getchar-does-not-stop-when-using-scanf) – Jonathan Leffler Feb 27 '14 at 23:27
  • `getchar();` --> `getchar();getchar();` – BLUEPIXY Feb 27 '14 at 23:27
  • 1
    @BLUEPIXY: as long as the user didn't add a space after the number by accident... :D For the expected input, that would work. – Jonathan Leffler Feb 27 '14 at 23:47

2 Answers2

0

My best guess is that it is retrieving the remaining newline after the user has entered their input. You can print out the return value to verify. If I'm correct, it'll be "10" or "13" depending on your OS.

You might want to change your program to use getline. There are other examples on how to write a get line at How to read a line from the console in C?

Community
  • 1
  • 1
Daniel
  • 4,481
  • 14
  • 34
  • 1
    If `getchar()` reads a newline character, it will return `'\n'`, which is usually equal to 10. In text mode, each line ending is converted to a single newline character on input, and each newline character is converted to an end-of-line sequence on output. (On UNIX-like systems, the conversion is trivial.) – Keith Thompson Feb 27 '14 at 23:30
0

When code calls scanf("%s", ... the program waits for input.

You type "123" and nothing happens yet as stdin is buffered input and waits for a \n thus the system has not given any data to scanf().

Then you type "\n" and "123\n" is given to stdin.

scanf("%s",...) reads stdin and scans optional leading white-space then the non-white space "123". Finally it sees "\n" and puts it back in stdin and completes.

Code calls scanf("%s", ... again. scanf() scans the "\n" as part of its scanning optional leading white-space. Then it waits for more input.

You type "456" and nothing happens yet as stdin is buffered input and waits for a \n thus the system has not given any data to scanf().

Then you type "\n" and "456\n" is given to stdin.

scanf("%s",...) reads stdin and scans optional leading white-space then the non-white space "456". Finally it sees "\n" and puts it back in stdin and completes.

Finally you call getchar() and puff, it reads the previous line's \n from stdin.


So how to do I stop the program to ask for input at getchar() and how do I make it recognize just a enter button.

Best approach: use fgets()

char hotels_houses[5+1];

// scanf("%s", hotels_houses);
fgets(hotels_houses, sizeof hotels_houses, stdin);
houses = atoi(hotels_houses);
...
// scanf("%s", hotels_houses);
fgets(hotels_houses, sizeof hotels_houses, stdin);
hotels = atoi(hotels_houses);
...
puts("Click enter to calculate total cash ");
fgets(bhotels_houses, sizeof hotels_houses, stdin); // do nothing w/hotels_houses
printf("Total cost: %d\n", cost); 

Checking for a NULL return value from fgets() is useful to test for a closed stdin.
Using strtol() has error checking advantages over atoi().

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256