0

I wrote a c program that prompts the user enter their account information into the system and then print them out. I tried to use "gets()" to capture strings. However, it skipped the question for user's address and jumped into the question for state and city. I believed the problem is the "gets()"function. Could someone please help me with it?

#include <stdio.h>
#include <stdlib.h>
#define MAXLEN 40
struct account
{
    long unsigned number;
    char address[MAXLEN];
    char cityState[MAXLEN];
    int zip;
    double balances;
    double credit;
    char name[MAXLEN];
};

int main(void)
{
    struct account memebr;
    puts("Please enter the account number");
    scanf("%lu", &memebr.number);
    puts("Please enter the street address");
    while(getchar() != '\n')
        gets(memebr.address);
    puts("Please enter your city/state");
    while(getchar() != '\n')
        gets(memebr.cityState);
    puts("Please enter your zip code");
    scanf("%d", &memebr.zip);
    puts("Please enter your balances");
    scanf("%lf", &memebr.balances);
    puts("Please enter your credit limit");
    scanf("%lf", &memebr.credit);
    puts("Please enter your name");
    while(getchar() != '\n')
        gets(memebr.name);

    printf("Account number: %lu\n", memebr.number);
    printf("The street address is : %s\n", memebr.address);
    printf("The owner comes from: %s\n", memebr.cityState);
    printf("The zip code is: %d\n", memebr.zip);
    printf("The owner's balances: %.2f\n", memebr.balances);
    printf("The owner's credit limit: %.2f\n", memebr.credit);
    printf("The owener is: %s\n", memebr.name);
    return 0;

}
Wang Nick
  • 385
  • 1
  • 6
  • 17
  • What did you type in? – user253751 May 17 '16 at 00:55
  • 4
    Why are you using `gets()`. It's no longer a standard function, and it's dangerous. – Iharob Al Asimi May 17 '16 at 01:02
  • `while(getchar() != '\n') gets(memebr.address);` --> `while(getchar() != '\n'); gets(memebr.address);`, `while(getchar() != '\n') gets(memebr.cityState);` --> `gets(memebr.cityState);`, `while(getchar() != '\n') gets(memebr.name);` --> `while(getchar() != '\n'); gets(memebr.name);` Also `gets` has been abolished. – BLUEPIXY May 17 '16 at 01:10
  • What do you think code like this does: `while(getchar() != '\n') gets(memebr.name);`? – David Schwartz May 17 '16 at 05:05
  • the function: `gets()` has been depreciated for years and in the latest c standard it is completely removed. Your compiler should have told you that. The best replacement is the function: `fgets()` (which does have a different set of parameters from`gets()` Suggest read the man page for `fgets()` so you will know what to expect and how to modify your code. – user3629249 May 18 '16 at 03:45

2 Answers2

1

You are asking for input twice, and getchar() will only return when the buffer is flushed, i.e when Enter1 is pressed. Thus, the getchar() call consumes everything and then you will have to type the same again for gets() to take it.

Try like this

fgets(member.name, sizeof(member.name), stdin);

instead of

while (getchar() != '\n')
    gets(member.name);

Notice that I replaced gets() with fgets() which is safer and is still part of the standard.


1Actually, when '\n' is inserted into the buffer.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
0

Referring to the previous question "getchar does not stop when using scanf", the input is only sent to the program after you typed a newline, but scanf("%s", command );leaves the newline in the input buffer. And then the following getchar() would return the newline immediately. That's why the program does not wait for the further input.

As a result, you need to make sure to clean your std input buffer before reading follow-up input. Modify your code:

scanf("%lu", &memebr.number);
puts("Please enter the street address");
while(getchar() != '\n')
    gets(memebr.address);

to

scanf("%lu", &memebr.number);                                                         
do {
  c = getchar();
}while(c != '\n' && c != EOF);
puts("Please enter the street address");
gets(memebr.address);

should be ok to execute the program.

Community
  • 1
  • 1
CWLiu
  • 3,913
  • 1
  • 10
  • 14