A very beginners question

Hello,

I am trying to read two variables, a string and an integer, respectively. Those two variables are read in a loop, until following string is typed. "No more". I have the following code, however and as you might expect it is not working.

if I type i.e Mario 23, it does not anything afterwards, and if I type "No more", it just keeps repeating in a loop "Digite el nombre y edad"

Would anyone suggest a better approach? Furthermore, a beginners approach, not advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace std;


int main ()
{
	//Declara e inicializa variables
	string Nombre_Persona;
	int    Edad_Persona = 0;


	while (Nombre_Persona != "No mas")
	{
		cout << "Digite el nombre y edad: " << endl;
		getline(cin, Nombre_Persona);
		cin >> Edad_Persona;

	}

}
Can you describe the differences of the following two inputs?
1
2
3
std::string text;
std::cin >> text;
std::getline( std::cin, text );

The code runs as you want for me.

Colombiano1976 wrote:
if I type i.e Mario 23, it does not anything afterwards, and if I type "No more", it just keeps repeating in a loop "Digite el nombre y edad"

Doesn't happen for me.
Hola Colombiano1976,

Try adding this after the getline:
if (Nombre_Persona == "No mas") continue;.

if I type i.e Mario 23, it does not anything afterwards.

Well based on the code you have posted you do nothing with your inputs.

A suggestion is to make:

1
2
string Nombre_Persona;
int    Edad_Persona = 0;


an array or a vector or even a map to store your inputs for later processing. Because your inputs are in a while loop it gives the perception that you will have more than one set of answers, but you can only store one set of inputs and anything after that just overwrites the previous inputs.

Hope that helps,

Andy

P.S. Add this after the cin statement:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Last edited on
Handy Andy wrote:
P.S. Add this after the cin statement:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

That is not needed in this code. Also, you should tell the OP to include a library if your code uses it. In this case, the <limits> library.

Anyways, for strings std::getline() (defined in the <string> library) should be used. Unless taking only the characters before the first whitespace for input is desired.
Last edited on
Hi everyone,

I should have been more clear with my code, my apologies. I am trying to capture a name and age, (that is why I use getline and cin), and yes I was also trying -as some of you have suggested- to save them in a string vector and int vector types ,respectively.

I did not add those two vectors because I am just testing that:

1) I can type a name and an age in two different variables. I know I can do it in one go with getline but I will not be able to use "age" as it is a string type.

2) I can terminate the loop by just typing "No mas".

so...I did not care too much about saving the values at the moment, as I thought I have to be able to capture them in two different variables first

I did try to add:

if (Nombre_Persona == "No mas") continue; after getline, however it did not work.
The reason I did ask about >> and getline was that first extracts one word, but the second (by default) takes everything up to next newline.

If the user types:
Mario B 30
No mas

then the getline extracts "Mario B 30" as Nombre_Persona and
the cin >> Edad_Persona; cannot make a number from No mas.

Input failure sets failbit on the cin and all future read attempts automatically fail.

If the user types:
Mario B
30
No mas

then the getline extracts "Mario B" as Nombre_Persona and
the cin >> Edad_Persona; stores 30 in Edad_Persona.
The next iterations getline receives "No mas" and the loop ends gracefully.
Topic archived. No new replies allowed.