My program cant read gets()

For example

char text[100]
printf("Enter some text: \n\n");
gets(text);

It works perfectly fine , but when i copied the code to a new task, full of voids.
The program just ignore the gets(text);
Why is this happening.Help appreciated. Thanks
==The code example==
INPUT

char text[100];

int count=0, count2=0;
printf("Enter some text: \n\n\a");
gets (text);
printf("\nText after removing space:\n\n%s\n\n",Space);

OUTPUT
Enter some text:

Text after removing space:

Press any key to continue....
Last edited on
What "Space" is? Looks like you didn't give us all code.
im sorry here's the full code

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

int main()
{
char text[100],space[100];
int count=0, count2=0;
printf("Enter some text: \n\n");
gets(text);
while(text[count]!='\0')
{
if (!(text[count]==' '&&text[count+1] == ' '))
{
space[count2]=text[count];
count2++;
}
count++;
}
space[count2]='\0';

printf("\nText after removing space:\n\n%s\n\n",space);
system("pause");
return 0;
}

Program works fine for me and I don't see any errors...
change
1
2
#include <stdio.h>
#include<stdlib.h> 

to
1
2
#include <cstdio>
#include <cstdlib> 

.h versions of headers are deprecated and are there only for compatibility.

Take a look at this code. Go into choice number 2.

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


void menu();
void Spacein();

//Main
int main()
{
menu();
system("pause");
return 0;
}
void menu()

{
int choice=0; //declare choice
printf("Select Choice:\n\n ");
printf("1.Reverse Number:\n\n");
printf(" 2.Remove Extra Spaces:\n\n ");
printf("3.Insert Array Element:\n\n ");
printf("Enter Choice Number :\n\n\a ");
scanf("%d",&choice); //input choice

if (choice==2)
{
system("cls");
Spacein();
}

else
{
system("cls");
printf("\a\a\a\a\a\a\a\a\a\a\a\a\a\a\aCLOSING PROGRAM\n");
_sleep(3000);


}
}

//No Spacing
void Spacein()
{
char text[100];
char Space[100];
int count=0, count2=0;
printf("Enter some text: \n\n\a");
gets (text);

while(text[count]!='\0')
{
if (!(text[count]==' '&&text[count+1] == ' '))
{
Space[count2]=text[count];
count2++;
}
count++;
}
Space[count2]='\0';

printf("\nText after removing space:\n\n%s\n\n",Space);

}

MiiNiPaa
Sorry for taking up your time and thanks :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <stdlib.h>

char * removeSpace(char * input, char * output);

int main()
{
    char text[100],space[100];
    int count=0, count2=0;
    printf("Enter some text: \n\n");
    gets(text);

    while(text[count]!='\0')
    {
        if (!(text[count]==' '&&text[count+1] == ' '))
        {
            space[count2]=text[count];
            count2++;
        }
        count++;
    }
    space[count2]='\0';


    printf("\nText before removing space:\n\"%s\"",text);
    printf("\n\nText after removing space:\n\"%s\"",space);
    printf("\n\nFunction removeSpace:\n\"%s\"\n", removeSpace(text, space));

    return 0;
}

char * removeSpace(char * input, char * output)
{
    int ix = 0;
    int ox = 0;
    const char spc = ' ';

    // Ignore leading spaces
    while (input[ix] == spc)
        ix++;

    // copy text ignoring multiple spaces
    while (input[ix])
    {
        if (!(input[ix] == spc && input[ix+1] == spc))
            output[ox++] = input[ix];
        ix++;
    }

    // Add null terminator
    output[ox] = '\0';

    // Ignore trailing space
    if (ox > 0 && output[ox-1] == spc)
        output[ox-1] = '\0';

    return output;
}

Output:
Enter some text:

   The cat   sat on  the   mat.

Text before removing space:
"   The cat   sat on  the   mat.      "

Text after removing space:
" The cat sat on the mat. "

Function removeSpace:
"The cat sat on the mat."
I see now, the problem is that after getting an integer using scanf(), the newline character remains in the input buffer.

An alternative, instead of using scanf()
1
2
3
    char buf[100];
    fgets(buf,100,stdin); //input choice
    choice = atoi(buf);
Great codes you have there
What does "char * removeSpace(char * input, char * output)"
do?

Im still confuse about why wont my gets(); work in the code i shown above.
Thanks
What does "char * removeSpace(char * input, char * output)"
do?

If you mean line 4, it's the declaration of the prototype of the function. It is there so that when the compiler sees the function call at line 27, it knows enough about the function to be able to generate code.

In some larger programs, the full definition of the function might be in a completely separate file. In this case it's just a few lines lower down, but the principle is the same.

Im still confuse about why wont my gets(); work in the code i shown above

Well, when you use scanf to enter a number, what do you actually type on the keyboard? You type some digits, and then press enter. All of those keystrokes are stored as characters in the input buffer (an area of memory set aside for that purpose). So the buffer contains something like "2\n".

Next the scanf routine examines what it finds in the buffer and tries to get an integer. As soon as the newline character '\n' is encountered, it isn't part of the number, so scanf stops at that point, and the newline remains in the buffer.

Later, when you try to do gets(), the routine finds the newline character in the buffer, and considers that to be the end of the string.

You can demonstrate this for yourself. At the menu screen, instead of entering "2", try entering some text as well, like this, "2 hello". You should find that the gets() function will receive " hello".
Last edited on
@Chervil
Thanks for clarifying this problem with me.
I find all this explanation very useful :)

You can demonstrate this for yourself. At the menu screen, instead of entering "2", try entering some text as well, like this, "2 hello". You should find that the gets() function will receive " hello".

I tried it for myself and indeed it prints out with i typed at the menu screen!
Is there anyway to clear the input buffer? Using simple codes, not complex ones . http://www.cplusplus.com/forum/beginner/33349/ <dont understand

If you mean line 4, it's the declaration of the prototype of the function. It is there so that when the compiler sees the function call at line 27, it knows enough about the function to be able to generate code.


I understand how this declarations works as whole now thanks.
Last edited on
1
2
3
   char buf[100];
    fgets(buf,100,stdin); //input choice
    choice = atoi(buf);


How does this code works ?
fgets() is the same as gets() in this case, but a bit safer, as it avoids the input being larger than the buffer, which can cause corruption of other data.

atoi() is one of several functions to convert a character string to a number.

http://www.cplusplus.com/reference/cstdio/fgets/
http://www.cplusplus.com/reference/cstdlib/atoi/

The reason for this approach is that the gets() or fgets() will read an entire line from the input, and doesn't normally leave anything remaining in the buffer afterwards. (If you used this with a disk file, there would usually be more data in the buffer, but we are considering keyboard input here).

These are functions from the C library. If we were to use the C++ equivalents, then there can still be problems with characters remaining in the buffer. Normally that is cleared using cin.ignore().

C++ example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int choice;
    string line;

    cin >> choice;         // get integer
    cin.ignore(1000,'\n'); // ignore up to 1000 characters or until newline is found
    getline(cin,line);     // get a line of text as a string

    cout << "choice: " << choice << endl;
    cout << "line:   " << line << endl;

    return 0;
}
Topic archived. No new replies allowed.