0

I just tried to print the String which I have received through scanf function and I have set the loop to process further until I press the 0 to terminate.

First I have entered the single string, it was normally printed, but another time I have entered three strings => Name1 Name2 Name3 --> three strings were printed automatically like I mentioned below after the code

Program:

#include <stdio.h>

int main()
{   
    int i; 

    do {
        char str[20];
    
        printf("\nEnter the String: ");   
    
        scanf("%s", str);              // String input
    
        printf("The String is %s", str);     // Print the String
    
        printf("\n\nEnter the Number 1 to restart and 0 to terminate: ");  //Input for loop
    
        scanf("%d", &i);
    
    } while (i != 0);     // while loop
    
    return 0;
}

Output console Screen:

Enter the String: Name

The String is Name

Enter the Number 1 to restart and 0 to terminate: 1     // Normally printed

// I don't what is happening by Entering three strings with space (Below Mentioned)

Enter the String: Name1 Name2 Name3

The String is Name1

Enter the Number 1 to restart and 0 to terminate: 

Enter the String: 

The String is Name2

Enter the Number 1 to restart and 0 to terminate: 

Enter the String: 

The String is Name3

Enter the Number 1 to restart and 0 to terminate: 0

...Program finished with exit code 0 Press ENTER to exit console.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Venkat V
  • 1
  • 1
  • You're using `scanf`, so you aren't reading a line, you're reading (in effect) a word, i.e. whitespace characters terminate the string. Look at the man page for `scanf`, under the description of the `%s` format specifier. You might be better off using `fgets` instead of `scanf` when reading the string. – Tom Karzes Mar 12 '23 at 18:10
  • gets( ) worked fine. Another issue Now i am facing is, After printing the string, the problem in the loop, if i enter the 1 to repeat the process it is printing like this again and again until i terminate the process instead of asking the string value again--> Enter the Number 1 to restart and 0 to terminate: 1 Enter the String: The String is Enter the Number 1 to restart and 0 to terminate: 1 Enter the String: The String is – Venkat V Mar 12 '23 at 18:26
  • Make sure you use `fgets`, and not `gets`. The `gets` function is deprecated. – Tom Karzes Mar 12 '23 at 18:40

1 Answers1

0

If you want to enter a string that contains several words separated by spaces then instead of this call of scanf

scanf("%s", str);

write

scanf(" %19[^\n]", str);

Pay attention to the leading space in the format string. It allows to skip the new line character '\n' stored in the input buffer due to pressing the Enter key. Otherwise neither string can be read.

As for your code then due to the input buffer contains three entered strings then this call

scanf("%d", &i);

of inputting of an integer fails between reading the strings because the input buffer contains letters instead of an integer.

After all three strings will be read then only then the call of scanf will, wait until you will enter a number.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335