HELP WITH THE "gets()"

When I execute de program it doesn't ask me to give the string, ¿why?


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
#include <iostream>
using namespace std;
int CoutLetters(char* string, char letter);
#include <string.h>
#include <stdio.h>

void main()
{
	while(1)
	{
		int repeticiones =0;
		char letter;
		int longv  = 0;
		
		cout<<"\t ***CUENTA PALABRAS DE UNA FRASE ***\n";
		cout<<"Max(LONGITUD DE LA FRASE) = ";
		cin>>longv;
		cout<<endl;
		cout<<"FRASE: ";
		char *string = new char[longv];
		if (string == NULL ) {cout <<"Error,no hay memoria" ; exit(0);}
		gets(string);
		cout<<"\nLETRA: ";
		cin>>letter;
		cout<<endl;

		repeticiones = CoutLetters(string,letter);
		cout << "La letra '"<<letter<<"' se repite "<<repeticiones<<" veces."<<endl;

		bool opcion = false;
		cout<<"\t\t ***REPETIMOS?***\n";
		cout<<"NO ===> 0   /// SI ===> 1";
		cout<<"\n\t Su eleccion ===> ";
		cin >> opcion;
		system("cls");
		if (opcion == false){delete [] string; exit(0);}
	}
	cin.ignore();
}

int CoutLetters(char* string, char letter)
{
	int longitud = 0;
	longitud = strlen(string);
	int conta =0;
	for(int i = 0 ; i<longitud ; i++)
	{
		if( string[i] == letter ){conta += 1;}
	}
	return (conta);
}
Please answer me I've ask this question the 6 of April, and I NEED PLEASE !!!
Line 17: After this cin, '\n' is still in the input buffer. You need to add after line 17:
 
cin.ignore (1000, '\n');

You also have a memory leak in your program. Each time through the loop, you allocate a new string at line 20. At line 36, you release string only if opcion is false (exit). You need to release string each time through the loop.
Last edited on
One should also note that gets is one of the most dangerous functions in the C library since it doesn't allow you to specify a buffer size. Use cin.getline if you insist on using C strings, or std::getline if you come to your senses and decide to use std::string.
Thanks now it works, but WHY ???

WHY WE HAVE TO USE

cin.ignore ();

AND WHY THERE?

[/code]

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
#include <iostream>
using namespace std;
int CoutLetters(char* string,int longv, char letter);
#include <conio.h>
#include <string.h>
#include <stdio.h>

void main()
{
	while(1)
	{
		int repeticiones =0;
		char letter;
		int longv  = 0;

		cout<<"\t ***CUENTA LETRAS DE UNA FRASE ***\n";
		cout<<"Max(LONGITUD DE LA FRASE) = ";
			cin>>longv;
			cin.ignore();
		cout<<endl;
		cout<<"FRASE: ";
				char *string = new char[longv];
				if (string == NULL ) {cout <<"Error,no hay memoria" ; exit(0);}
		for (int i = 0; i<longv; i++) {string[i]=0;}
			gets(string);
		cout<<endl;
		cout<<"\nLETRA: ";
			cin>>letter;
		cout<<endl;

		//Llamar a la funcion para que cuente las letras.
		repeticiones = CoutLetters(string,longv,letter);
		cout << "La letra '"<<letter<<"' se repite "<<repeticiones<<" veces."<<endl;

		bool opcion = false;
		cout<<"\t\t ***REPETIMOS?***\n";
		cout<<"NO ===> 0   /// SI ===> 1";
		cout<<"\n\t Su eleccion ===> ";
		cin >> opcion;
		system("cls");
		if (opcion == false){delete [] string; exit(0);}
		delete [] string;
	}
	cin.ignore();
	
}

int CoutLetters(char* string,int longv, char letter)
{
	int conta =0;
	for(int i = 0 ; i<longv ; i++)
	{
		if( string[i] == letter ){conta += 1;}
	}
	return (conta);
}
Hi All,
cin.ignore() used to make input buffer clear(or empty).
after you get input from "cin" you will get your data but In addition, you will get a new line command too(because when you want to send data you will press enter(or Return) button and it will send a new line command(\n) to the cin buffer too, when you write cin.ignore(1000, '\n') or only cin.ignore() it will empty the buffer of cin(in first case it will search for '\n' and clear then but in 2nd one it will empty the buffer completely), if you did not do that you will have '\n' in your cin buffer and when you try to use gets() it will get '\n' and so it never wait for your input.
I think you should get answer of your question.
Have Nice Life.

EDIT:
as MiiNiaPaa said, it will ignoring them but in real its same as you did not enter any '\n' to the cin buffer , I am agree with MiNiPaa but i don't like make changes to the Main post so i said it in EDIT, Thank you MiiNiPaa , I should be careful about words :D
Last edited on
Some in-depth explanation of what is happening and how to avoid it: http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction

Note that using an ignore() is not clearing the buffer. It is ignoring of some input which might be or might be not in input buffer.
Topic archived. No new replies allowed.