std::lenght_error - How to fix/get around?

Hello!

I'm a c++ beginner, so to learn the language, I watch tutorials online or make up some program and try to advance it as much as possible.

So, I wanted to create a program that will ask the user for how many people is in his group, and then asks him to input the names of the people in the group.

It's far from finished, but I ran into a problem.
I have an if statement (ln. 24), to make sure the input from the user is valid and to make sure he can't enter more than 1000 people.
This works all fine, unless the user enters a letter.
eg. the user inputs the letter "h", the program crashes and a an error box says: "Unhandled exception at 0x7786DAD8 in Vectors.exe: Microsoft C++ exception: std::length_error at memory location 0x0018FBAC."

Any help on this, would be much appreciated!

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
// Vectors.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <limits>

int main()
{
	//Define variables needed for program.
	unsigned int numOfPeople;
	std::string name;
	double long l;


	//Asks the user for input
	std::cout << "Please enter the number of people in your group: ";
	std::cin >> numOfPeople;

	//Tests if user input is valid.
	if (std::cin.fail() || numOfPeople > 1000  ) {
		std::cout << "Please enter the number of people in your group: ";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		std::cin >> numOfPeople;
	}
	

	//Creates the vector
	std::vector<std::string> peopleInGroup;

	//Sets the size of the vector from numOfPeople
	peopleInGroup.reserve(numOfPeople);

	//Asks the user to input the name(s)
	std::cout << "Enter a name, then hit enter, to enter the next name: ";

	for (int x = 0; x <= numOfPeople; x++) {
		getline(std::cin, name);
		peopleInGroup.push_back(name);
		std::cin.clear();
	}

	std::cout << "The people in your group are: ";
	for (int x = 0; x <= numOfPeople; x++) {
		std::cout << peopleInGroup[x] << "\n";
	}
		
	system("PAUSE");
    return 0;
}


Last edited on
An int can not be assigned to a letter. If you want that variable to be able to hold a letter you will need to use a char or std::string.
Oh, sorry I didn't clarify it good enough - What I mean is, I want to make sure the user only enters a valid int and nothing else.
So if the user enters a letter, the program rejects the input, and aks him to enter a valid int
Last edited on
I'm sorry I misunderstood. You have two options. I see you already have an if statement with std::cin.fail() try changing that if statement to a while loop, like so while (std::cin.fail() || numOfPeople > 1000) this way the user must enter a valid int. If this doesn't work you could try changing int numOfPeople to char numOfPeople. Hope this can help you.
Changing it to while (std::cin.fail() || numOfPeople > 1000) worked!
So simple, ahaha =)
Thanks a lot.
A few other comments.

The first getline() at line 42 will not work correctly because the previous std::cin >> numOfPeople; has left a trailing newline character in the input buffer. You can remove that by adding at line 30, another
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

The two for loops.
Line 41,
 
for (int x = 0; x <= numOfPeople; x++)
the <= should just be <

At line 47 you have the same error. However you might instead use
x < peopleInGroup.size() to guarantee that no out-of-bounds element will be accessed.
Last edited on
Hi!

Thanks for the extra comments. I Have corrected the mistakes in the for loops, and added std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); at line 30. I also added some comments, to explain why, so I won't forget when i'm looking back.

Thanks a lot, I really appreciate it.
Last edited on
Topic archived. No new replies allowed.