Using getline to read until a number

I have a file that has several lines of text with each line having a person's name followed by his/her shoe number. The length of the name varies from one to several words in the file. I need to read the name and the number into individual variables.

I've tried to use getline to read a line in the file until it reaches a number. After this the program would read the number into a different variable, jump to the next line and repeat. The problem is the first part. At the moment my code looks like this:

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
const int amount_of_people = 10;

struct person {
       string name;
       int shoenumber;
};

int main () {
    person people[amount_of_people];
    
    ifstream file("input.txt");
    if (!file) {
       cerr << "Couldn't open the file!" << endl;
    } else {
           int i = 0;
           while ( file.peek() != EOF ) {
                 if ( !(getline(file, people[i].name, 'this stops getline'))) {
                      cerr << "An error occurred on line " << i+1 << endl;
                      break;
                 } else if ( !(file >> people[i].shoenumber) ) {
                      cerr << "An error occurred on line " << i+1 << endl;
                      break;
                 }
                 file.ignore(1000, '\n');
                 ++i;
           }
    }  
    
    system("PAUSE");                    
}

Any advice? Is it even possible to do such a thing with getline?
The easy solution would be to change the input file so the number is read first. Then you can use getline to get the rest of the line.

Another way would be to mark the end of the string with a ':' then use
getline(ifstream, string, ':'); to input the string.

I don't know of a way getline could be used to read up to a number, but that dosn't mean there isn't one.
Last edited on
But the idea is to solve the problem of having the text in that format so I can't really change it.
Maybe you could input the whole line as a string. Then minipulate the string to get what you wan't.

Here is an example
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
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
	string str;
	string num;
	double stringDouble;
	int indexOfDigit = -1;

	cout << "PLease enter a string and a number ";
	getline(cin, str);

	// Finds the index of the first digit
	for(int i = 0; i < str.length(); i++)
	{
		if(isdigit(str.at(i))) 
		{
			indexOfDigit = i;
			break;
		}
	}/*end of for loop*/

	//saves number as string
	num = str.substr(indexOfDigit, str.length() - indexOfDigit);
	
	//removes the number part of the string
	str.resize(indexOfDigit);

	//converts num to a double
	stringDouble = stod(num);

	cout << "The string is " << str << endl;
	cout << "The double is " << stringDouble << endl;
	
	
	cin.ignore();
	return 0;
}/*end of main*/
Last edited on
Variant for "Finds the index":
1
2
auto it = std::find_if( str.begin(), str.end(), std::isdigit );
if ( str.end() != it ) ...
Topic archived. No new replies allowed.