Reading the names and ages

Hello, I'm doing a quick exercise from Chapter 9 of Programming Principles and Practice using C++ and I followed exercise 2 carefully but I keep running into the same problem. For some reason when I input the names needed and press enter it doesn't work. I eventually have to ctrl+z which just terminates the program.

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
 #include "std_lib_facilities.h"
#include <iostream>
#include <vector>
using namespace std;

class Name_pairs{
	vector<string> name=vector<string>{};
	vector<double> age=vector<double>{};

	public:
		void read_names();
		void read_age();
		void print();
};

void Name_pairs::read_names(){
	string temp; 
	while (std::cin >> temp){
		name.push_back(temp);
		break;
	}
		

	
}

void Name_pairs::read_age(){
	cout << "Enter the ages:" << '\n';
	int i ;
	while (std::cin >> i){
		age.push_back(i);
		break;
	}
		
	
	
}

void Name_pairs::print(){
	for (int i = 0; i < name.size(); i++){
		cout << '(' << name[i] << ',' << age[i] << ')';
	}
}



int main(){

	Name_pairs test;
	cout << "Enter the names:" << '\n';
	test.read_names();
	cout << '\n';
	test.read_age();
	

	
	

	test.print();
	
	keep_window_open();
	return 0;
}
Last edited on
In Name_pairs::read_names you have:

while (std::cin >> temp)

When will this loop end?
I'm relatively new to C++.
The loop is practically infinite because I have this notion that the input stops when you press the Enter key
The purpose of this program is to read in a sequence of names then a sequence of ages. I used a break but I seem to be getting an error if I input more than one value for names. The loop is suppose to end once the user hits the enter key
Last edited on
Ok I will point you to some things which I have seen...

First in the read_name() function,you have to terminate the loop after a while,or else the program WILL GET STUCK THERE.Yes ,I dont wanna provoke you,but thats true.
Heres a few lines which Ive written,
1
2
3
4
5
6
7
8
9
10
11
12
13
void Name_pairs::read_names(){
	string temp;
	char c='y'; 
	while (c=='y'||c=='Y')
	{
        cin>>temp;    
		name.push_back(temp);
		cout<<"Do you wanna add more?"<<endl;
        cin>>c;
        if(c=='n'||c=='N')
            break;
	}


Here the while loop will go on for one turn,and when the user gives a 'N' to char c,the loop checks for only 'y' and so the loop terminates.This will give a better result than asking for names forever,as the program will not do anything else but just that.
Hope you get it.And try to learn the basics of C++ first,like loops,if-else,switch.
After you gain a knowledge of that stuff,this type of things will be a cakewalk.
Hope you like it.(^_^)!!!!
If you want input to end on enter, you must use line oriented (or unformatted) input extraction. With string streams, you can then treat that line as a stream on which you can perform formatted extraction.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// ...
#include <sstream>

// ...

void Name_pairs::read_names()
{
    std::string line ;
    getline(std::cin, line);

    std::istringstream ls(line);

    std::string token;
    while (ls >> token)
        name.push_back(token);
}
Topic archived. No new replies allowed.