0

I have a small program which reverses the words when inputted however for some reason the first input is being outputted as a blank without allowing the user to enter any input for it. This is happening for any amount of n, and I have been playing around with the for loops (were I think the error is) however I have not yet managed to eliminate the problem.

Thanks In Advance and Happy Holidays!

CODE:

int main()
{
    int n;

    printf("How many strings would you like to insert?\n");
    scanf("%d",&n);
    n=n+1;
    char s[n][100];


    printf("Insert %d strings\n", n-1);

    for(int i=0; i<n; i++) {
        gets(s[i]);
    }

    for (int i = 0; i <n ; i++) {
        reverseWords(s[i]);
        printf("The number %d Sentence is %s\n", i+1, s[i]);
    }

    return 0;
} 

Input/Output:

How many strings would you like to insert?
3

Insert 3 strings
hello whats up
bye dude

The number 1 Sentence is
The number 2 Sentence is up whats hello
The number 3 Sentence is dude bye

Process finished with exit code 0

EDIT:

I managed to find a fix myself. Thoughts on this fix?

int main()
{
    int n;

    printf("How many strings would you like to insert?\n");
    scanf("%d",&n);
    char s[n][100];

    printf("Insert %d strings\n", n);

    for(int i=0; i<n+1; i++) {
        gets(s[i]);
    }

    for (int i = 1; i <n+1 ; i++) {
        reverseWords(s[i]);
        printf("The number %d sentence in reverse is %s\n", i, s[i]);
    }

    return 0;
} 

New Output: "G:\UoM\Second Year\Programming Principles in C\Question 1\cmake-build-debug\CLion.exe" How many strings would you like to insert? 3

Insert 3 strings
Hello Carl whats up?
Dude Whats giong on?
wow nice!

The number 1 sentence is up? whats Carl Hello
The number 2 sentence is on? giong Whats Dude
The number 3 sentence is nice! wow
  • 2
    First of all, never ***ever*** use `gets`. [It's a dangerous function](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used), and has therefore been removed from the C specification. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Dec 26 '18 at 08:40
  • 3
    As for your problem, there are probably quite a few duplicates here on stackoverflow.com, but in short the problem is the `Enter` key you press for the input of the number. It's added to the input buffer as a newline, a newline that the `scanf` *doesn't* remove. – Some programmer dude Dec 26 '18 at 08:41

2 Answers2

0

Please try to modify your for loop as below. It will help you to get the expected output

for (int i = 1; i <n ; i++) 
{
  reverseWords(s[i]);
  printf("The number %d Sentence  %s\n", i, s[i]);
}
0

First of all don't use gets(). Instead, use fgets().

The problem is occurring because of the newline character remains on the input stream. When you give number of strings input, you enter number and hit the ENTER key. So, the input stream has 3\n in it [3 is number of strings input]. The scanf() will read the 3, but not the newline following it. That newline character will remain on the input stream. As soon as the gets() called, it read that newline character. The gets() reads until a newline character or the end-of-file is reached and if newline character found, it read but does not copy it to buffer and appends a terminating null character to the buffer. Hence, you are getting the first string as empty string.

A quick fix of your problem is to call getchar() immediately after the scanf() which will consume the stray newline character. But this is also not the perfect solution. You should not mix up the scanf() and gets() or other input functions. May you can consider using fgets() consistently in your program. For number of strings input you can use strtol() or sscanf() to convert the string to number.

NOTE: If you use fgets(), remember that it consider newline character as a valid character and include it in the string copied to buffer passed. Make sure to remove trailing newline character. Check this.

H.S.
  • 11,654
  • 2
  • 15
  • 32