help with function "gets"

Well hello everyone Im just a few newbie at this part so someone can help me? (CODE ON C)

My problem is that if I Using the function "gets(<var>)" to read a string, in a While or maybe I repeat the function more than 1 time and it doesn't work.. Just the first time of running it.

Why Gets can't run another time on the program?
there is another function that I would work instead of this one?

because as I know i can use scanf("%s", &<var>)
but if i leave a blank between the string it won't work at printing...
Any ideas?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main(){

    char c [100];

    while ( true ){

        gets(c);
        if( (c[0]) == 'x' )
            break;
        std::cout << c << std::endl;

    }

}


this piece of code seems to work, is this what you wanted to do? if you want more help your gonna have to paste here the code that is causing you trouble.
Last edited on
man wrote:
Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.


@TinyTeeTree: ¿why didn't you simply do char c[100]; ?
dunno, its an "on the spot" small test. seems to me like gets() is a preaty easy function to use, wonder how BatCat managed to screw it up? anyway ill edit my overhead
I'll bet there's a call to scanf somewhere in the program which leaves an endline on the input steam which causes gets to return immediately.
scanf can leave an endline?

can you demonstrate this in a code pls?
Can? It's what it does. e.g.
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
int main() 
{
    printf("enter a number: ");
    int d;
    if( scanf("%d",  &d)==1 ) 
    {
        printf("scanf got %d,  the next char is 0x%x\n", d,  getchar()) ;
   } 
} 
Okay look guys this is my code (Is in spanish though)

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//Funciones por doquier :(

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

void captura (void);
int valida (int);
float promedio(int,int,int);
void salida(char[30],float);
char* obs(float);

main()

	{
		

		captura();


		return 0;
	}



void captura(void)

	{
		char name[30];
		int n1,n2,n3;
		float average;
		
		
		printf("\n Ingrese el nombre del estudiante : ");
		gets(name);                                        // Here it's the problem
		printf("\n Ingrese la nota 1 : ");
		scanf("%i", &n1);
		
		n1= valida(n1);

		printf("\n Ingrese la nota 2 : ");
		scanf("%d", &n2);
		n2=valida(n2);

		printf("\n Ingrese la nota 3 : ");
		scanf("%d", &n3);
		n3=valida(n3);
		
		average=promedio(n1,n2,n3);
		
		salida(name,average); 

	}



	int valida(int x)
		{

			while ( x<0 || x>100)
				{
				
					printf("\n Ingrese una nota en el rango 0-100 :");
					scanf("%d", &x);
				}

			return(x);
		}
						

	float promedio (int j, int k, int l)

		{

			return (float)((j+k+l)/3);

		}

	
	

		
	void salida(char nombre[30], float prom)

		{
			char resp;
			printf("\n Nombre del alumno: %s", nombre);
			printf("\n Promedio : %.2f", prom);
			printf("\n El alumno esta : %s", obs(prom));
			printf("\n **************************************");
			do
			{
				printf("\n Desea continuar con otro estudiante? [S/N]:");
				scanf("%s", &resp);
				resp=toupper(resp);
				if (resp !='N' && resp != 'S')
					printf("ERROR INGRESE S o N");	
							
			}	
			while(resp !='N' && resp != 'S');
			
			if(resp == 'S')
				captura();
			
			

		}
		
		char* obs(float v)
		{
			if(v>69)
				return("aprobado");
			else
				return("Reprobado");	
		}



Okay the problem is that I run the program withouth problems, I can set a name with any name using gets.. The problem is that after processing all the vars, The program ask for "Do you want to continue" .. The if I press 'S' it continues but It won't ask me do insert Again the name var.. Tried using scanf but it doesn't work If i like a blank space (E. Robert gonzales). So it only works this way Robert_gonzales... So that's why Im using gets, but anyways teh second time gets doesn't work :/.
yep, there's a scanf that fills in resp, so gets (which you should never use anyway) reads off the endline and continues. Try scanf(" %29[^\n]", name)
Also, line 93 is not correct. resp is not a string so the format specifier should be "%c" and not "%s".
Thanks Cubbi It worked idk why but it worked lol... can someone explain me why this scanf(" %29[^\n]", name worked instead of gets() (:
... Just to use it in further codes...
Last edited on
You might jump down to the scanf section at:
http://wpollock.com/CPlus/PrintfRef.htm
ok then thanks to all. Solved n.n
Topic archived. No new replies allowed.