Problems with 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);
}
just read into a std::string mate, dont use char arrays like that.

also:
1
2
#include <string.h>
#include <stdio.h> 


should be
1
2
#include <string>
#include <iostream> 
Thaks, but:

1st) I've include those librarys, but in the new version of c++ we have to write like that @mutexe

2nd) I know that whe can do like that, but I want to do like in the first code that I've coment, and no this one:

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
#include <iostream>
using  namespace std;
#include <stdio.h>
#include <string.h>

void main()
{
	char frase[200];
	int longitud;
	char letra;
	int i,ii ;
	int conta;
	int option;

	while(1)
	{
		conta = 0;
		frase[200]=0;
		cout<<"1) Introduzca la frase: ";
		gets(frase);
		cout<<endl;
		longitud = strlen(frase);//optimiza el programa.
		cout<<"2) Introduzca la letra a comprobar: ";
		cin>>letra;
		for ( i = 0;i <longitud;i++)
		{
			if (frase[i] == letra) {conta += 1;}
		}
		cout<<"La letra sale "<<conta<<" veces en la frase.";
		cout<<endl;
		cout<<"¿Ahora que quiere hacer?";
		cout<<endl;
		cout<<"1) Repetir el programa"<<endl;
		cout<<"0) Salir"<<endl;
		cout<<"--> ";
		cin>>option;
		cout<<endl;
		if (option == 0){exit(0);}else{system("cls"); }
		cin.ignore();///////////////////////////////////////////////////////////////

	}
}
Topic archived. No new replies allowed.