cin.getline with vector struct

Hello there,
I have been working on this assignment for class where we have to convert to use an STL vector instead of an array. We also have to complete this my using iterators, no square brackets or use of push_back or at().

My problem that I am dealing with is at my readData function. I am trying to get it to accept a line from the use and store it under name but cin.getline is not working for me. Cant seem to figure it out or maybe I am missing something simple? Do you know a way to get it to accept a line of characters?

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

struct Highscore {
    char name;
    int score;
};


void getVectorSize(int& size);
void readData(vector<Highscore>& scores);


int main()
{
    int size;

    getVectorSize(size);

    vector<Highscore> scores(size);

    readData(scores);

    system("pause");
}



void getVectorSize(int& size) {
    cout << "How many scores will you enter?: ";
    cin >> size;
    cin.ignore();
}

void readData(vector<Highscore>& scores)
{
    for (vector<Highscore>::iterator i = scores.begin(); i != scores.end(); i++)
    {
        cout << "Enter the name for score #" << ": ";
        cin.getline(cin,(i->name));
        cout << "/n";

        cout << "Enter the score for score #" << ": ";
        cin >> i->score;
        cin.ignore();
    }
    cout << endl;
}
a line of characters?
You aren't using a line of characters. char name; is a single character.

Use std::string.

Yeah, it was my intension to have a line of characters. I believe that I am supposed to keep that variable type. This is good though, I look into that. Its been a long while since the last time I tried to program.

Edit: Yes that name field must remain as a c-string
Last edited on
Perhaps:

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

constexpr size_t MAXNAM {50};

struct Highscore {
	char name[MAXNAM] {};
	int score {};
};

size_t getVectorSize();
void readData(vector<Highscore>& scores);

int main()
{
	const auto size {getVectorSize()};
	vector<Highscore> scores(size);

	readData(scores);
}


size_t getVectorSize() {
	size_t size {};

	cout << "How many scores will you enter?: ";
	cin >> size;
	cin.ignore();

	return size;
}

void readData(vector<Highscore>& scores)
{
	for (auto i = scores.begin(); i != scores.end(); ++i) {
		cout << "Enter the name for score #"  << i - scores.begin() + 1 << ": ";
		cin.getline(i->name, MAXNAM);

		cout << "Enter the score for score #" << i - scores.begin() + 1 << ": ";
		cin >> i->score;
		cin.ignore();
	}

	cout << '\n';
}

Yes that name field must remain as a c-string

The name member in your struct is NOT a C string. It is a single character.

A C string is an array or [sic] chars. str[], for example.
https://www.learncpp.com/cpp-tutorial/c-style-strings/
Last edited on
I see what you did there seeplus and it looks good but it my fault I didnt include more information. In the directions its stated that a size parameter should not be used being that a vector knows its own size and can increase in its own. Thats where I have to figure out how to get it to read a line of characters and store them into char name.
@Siguardo, You have now been told explicitly twice and implicitly once that
char name;

is NOT a line of characters. It is a SINGLE CHARACTER; that's JUST ONE LETTER.
OK. Using vector:

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

struct Highscore {
	vector <char> name;
	int score {};
};

size_t getVectorSize();
void readData(vector<Highscore>& scores);

int main()
{
	const auto size {getVectorSize()};
	vector<Highscore> scores(size);

	readData(scores);

	for (const auto& [name, score] : scores) {
		for (const auto& ch : name)
			std::cout << ch;

		std::cout << "  " << score << '\n';
	}
}

size_t getVectorSize() {
	size_t size {};

	cout << "How many scores will you enter?: ";
	cin >> size;
	cin.ignore();

	return size;
}

void readData(vector<Highscore>& scores)
{
	for (auto i = scores.begin(); i != scores.end(); ++i) {
		cout << "Enter the name for score #" << i - scores.begin() + 1 << ": ";

		for (char ch {}; (ch = cin.get()) != '\n'; )
			i->name.emplace_back(ch);

		cout << "Enter the score for score #" << i - scores.begin() + 1 << ": ";
		cin >> i->score;
		cin.ignore();
	}

	cout << '\n';
}



How many scores will you enter?: 3
Enter the name for score #1: qwe rty
Enter the score for score #1: 1
Enter the name for score #2: asd fgh
Enter the score for score #2: 2
Enter the name for score #3: zxc vbn
Enter the score for score #3: 3

qwe rty  1
asd fgh  2
zxc vbn  3

Topic archived. No new replies allowed.