简体   繁体   中英

What is the best way to read a string?

So I'm really confused. I have to write a program in C which is basically an address book, thus I have to get multiple strings from the user (name, ID, phone etc.)

In the beginning I tried using scanf() only but it messed up sometimes with the newline character '\\n' . After some googling, I ended up using scanf() for getting single chars or ints (where user answers yes or no questions, or chooses an action from a menu) and fgets() to read the fields of the address book. However, I also had to use fflush(stdin) multiple times after using scanf() which is not recommended here as I have seen. This method worked so far as intended.

So what's the optimal way to read a string from the user? Does fflush(stdin) not offer portability? This is an assignment so I have to think for portability too, since I will execute my code on another computer.

Thank you in advance.

EDIT: So here is what I've got so far. Excuse some words that are written in another language (Albanian). I believe you can understand what's going on.

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

void regjistrim();
void kerkim();
void modifikim();
void fshirje();
void rradhitje();
void display();

#define EMRI 50
#define MBIEMRI 50
#define ID 20
#define TEL 20
#define EMAIL 25

typedef struct addressbook
{
    char emri[EMRI];
    char mbiemri[MBIEMRI];
    char id[ID];
    char tel[TEL];
    char email[EMAIL];
} addressbook;

FILE* Addressbook;

int main(void)
{
    char input[2];
    int choice;

    printf("----------------ADDRESS BOOK----------------");
    printf("\n\n\t1 - Regjistrimi i ri\n");
    printf("\n\t2 - Kerkim\n");
    printf("\n\t3 - Modifikim\n");
    printf("\n\t4 - Fshirje\n");
    printf("\n\t5 - Rradhitje\n");
    printf("\n\t6 - Afishim i address book\n");
    printf("\n\t0 - Exit\n");

    fgets(input, 2, stdin);
    sscanf(input, "%d", &choice);

    while (choice < 0 || choice > 6)
    {
        printf("\nShtypni nje numer nga 0 - 6: \n");
        fgets(input, 2, stdin);
        sscanf(input, "%d", &choice);
    }

    switch (choice)
    {
        case 1:
            regjistrim();
            break;
        case 2:
            kerkim();
            break;
        case 3:
            modifikim();
            break;
        case 4:
            fshirje();
            break;
        case 5:
            rradhitje();
            break;
        case 6:
            display();
            break;
        case 0:
            exit(0);
            break;
    }

    return 0;
}


//Regjistron nje qytetar ne addressbook
void regjistrim()
{
    char answer;

    addressbook entry;
    do
    {
        Addressbook = fopen("Addressbook.txt", "a+");

        printf("\nShtypni emrin: ");
        fgets(entry.emri, EMRI, stdin);

        printf("\nShtypni mbiemrin: ");
        fgets(entry.mbiemri, MBIEMRI, stdin);

        printf("\nShtypni ID-in: ");
        fgets(entry.id, ID, stdin);

        printf("\nShtypni nr. telefoni: ");
        fgets(entry.tel, TEL, stdin);

        printf("\nShtypni email-in: ");
        fgets(entry.email, EMAIL, stdin);

        fprintf(Addressbook, "Emri: %sMbiemri: %sID: %sNr. telefoni: %sEmail: %s\n", entry.emri, entry.mbiemri, entry.id, entry.tel,entry.email);

        fclose(Addressbook);

        printf("\nShtypni y/Y neqoftese doni te regjistroni person tjeter: ");
        fgets(answer, 1, stdin);
    }
    while(answer == 'y' || answer == 'Y');
}

With scanf , you can clear the newlines, like that. Also, I have included fgets too.

#include<stdio.h>

int main(void)
{
    int i, N = 5;
    char buffer[N];

    printf("Enter %d characters\n", N+1);

    scanf("%5s", buffer);   /* MUST check comments below on this page for this! */

    /* Clear trailing input */
    while(getchar() != '\n')
        /* discard */ ;


    for(i = 0 ; i < 5 ; ++i)
        printf("|%c|\n", buffer[i]);

    printf("End with scanf\n\n");

    /*****************************************************/

    printf("Enter %d characters\n", N+1);

    fgets(buffer, 5, stdin);

    for(i = 0 ; i < 5 ; ++i)
        printf("|%c|\n", buffer[i]);

    printf("End with fgets\n\n");

    return 0;
}

Also, this code demonstrates the limit you can put to every function for the input.

Source

Well "optimal" is maybe a bit subjective but I found doing a separate function to read the number makes things a bit easier and avoid scanf if you don't absolutely need it eg

int readNumber(int min, int max)
{
  char number[32];
  do
  {
    if ( fgets( number, sizeof(number), stdin ) != NULL )
    {
      // note that if 'number' is not a number atoi returns 0
      int n = atoi(number); 

      if ( n>= min && n <= max ) 
      { 
        return n;
      }
    }
    printf( "please enter a valid value between %d and %d\n", min, max );
  }
  while ( 1 );

  return -1; // never reached
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM