Using scanf with a while loop to input two separate strings

Elis Jones

I'm trying to make a program which reads in a name and number which is separated with a space and allocates the input to a structure.

typedef struct {

    char name [20];
    char number [12];
} Entry;

The scanf function is in a while loop, and I'd like it to break from the while loop if the input is "."

while(strcmp(e.name,".")!=0) {
    scanf("%s %s", e.name, e.number );
}

But using the above code means that the user has to input two periods. I was wondering if anybody had any advice on how I could make it break from the loop after the first ".".

Spikatrix

Use two conditions:

while(1) {  //infinite loop

    scanf("%19s", e.name);
    getchar(); //remove the \n from the stdin
    if(strcmp(e.name,".")==0)
        break;

    scanf("%11s", e.number);
    getchar(); //remove the \n from the stdin
    if(strcmp(e.number,".")==0)
        break;
}

The 19 and 11 tells scanf to scan a maximum of that much characters and then,append a \0 at the end.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to read strings from stdin into a two-dimensional array in c programming using scanf() and while loop?

From Dev

Checking input types with scanf() in a while loop

From Dev

Using scanf for character input, but the do-while loop wont stop at the null character

From Dev

C while loop using scanf prints twice when entering more than 1 char of input

From Dev

Separate input in scanf()?

From Dev

Stopping an infinite while loop using scanf in c?

From Dev

How to read from two input files using while loop

From Java

Printing two Strings simultaneously in loop but on separate "paragraphs"

From Dev

For loop and scanf in a while loop

From Dev

While loop using strings and if statements

From Dev

c - while loop keeps ignoring scanf after bad input

From Dev

While loop printing and scanf

From Dev

Validate the input using a while loop

From Dev

Double Ctrl+D input when using scanf for strings on Windows

From Dev

Why the previous input buffer is not stored in case of scanning of strings using scanf?

From Dev

Unable to take strings as input for structures properly using scanf() in C

From Dev

Parse two files input in for/while loop

From Dev

Using "continue" in while loop, skips over scanf("%d", var)

From Dev

Using scanf and while loop with vector push_back

From Dev

How to separate a string user input into two different strings?

From Dev

How to separate input strings?

From Dev

Printing multiple input strings in reverse using a loop

From Dev

Basic while loop to scanf an integer

From Dev

while loop does not execute scanf()

From Dev

Escape scanf within a while loop

From Dev

Ignoring scanf value in while loop

From Dev

using printf and then scanf in loop

From Dev

Using scanf in for loop

From Dev

Scanf takes two values in loop for standard input but needs to take only one to end loop

Related Related

  1. 1

    How to read strings from stdin into a two-dimensional array in c programming using scanf() and while loop?

  2. 2

    Checking input types with scanf() in a while loop

  3. 3

    Using scanf for character input, but the do-while loop wont stop at the null character

  4. 4

    C while loop using scanf prints twice when entering more than 1 char of input

  5. 5

    Separate input in scanf()?

  6. 6

    Stopping an infinite while loop using scanf in c?

  7. 7

    How to read from two input files using while loop

  8. 8

    Printing two Strings simultaneously in loop but on separate "paragraphs"

  9. 9

    For loop and scanf in a while loop

  10. 10

    While loop using strings and if statements

  11. 11

    c - while loop keeps ignoring scanf after bad input

  12. 12

    While loop printing and scanf

  13. 13

    Validate the input using a while loop

  14. 14

    Double Ctrl+D input when using scanf for strings on Windows

  15. 15

    Why the previous input buffer is not stored in case of scanning of strings using scanf?

  16. 16

    Unable to take strings as input for structures properly using scanf() in C

  17. 17

    Parse two files input in for/while loop

  18. 18

    Using "continue" in while loop, skips over scanf("%d", var)

  19. 19

    Using scanf and while loop with vector push_back

  20. 20

    How to separate a string user input into two different strings?

  21. 21

    How to separate input strings?

  22. 22

    Printing multiple input strings in reverse using a loop

  23. 23

    Basic while loop to scanf an integer

  24. 24

    while loop does not execute scanf()

  25. 25

    Escape scanf within a while loop

  26. 26

    Ignoring scanf value in while loop

  27. 27

    using printf and then scanf in loop

  28. 28

    Using scanf in for loop

  29. 29

    Scanf takes two values in loop for standard input but needs to take only one to end loop

HotTag

Archive