Hello, I am fairly new to C++ and am having a bit of trouble reading in a text file to my program which contains numbers(integers) and names(strings) and checking for spaces in the names(strings).I basically am reading in a .txt file which contains numbers and letters. In this file, the numbers correspond to a dogID number and the letters correspond to the name of the dog.Now the problem....

The string containing the dog names needs to be able to have spaces. The code needs to check for spaces and then for integers. If the name(string) is complete, the code needs to proceed to the dogID number(integer). So basically, I need to load a string, then load an integer, load a second string, then load a second integer and so on...

The code below is something that I came up with but am having trouble implementing it. It checks for ASCII characters 47-58(which are numbers 0 through 9 in the ASCII table) and then a conversion from string to integer is needed. Maybe string1 and string2(dog names) should be appended? Will the built in atoi function be useful here? Thanks in advance.

bool isString = true;
string Dogname = " ";
int trash;

while (isString = true)
{
	trash = " ";

	strcat(Dogname, trash);
}

if(atoi(trash(0)) >> 47 && atoi(trash(0)) < 58)
{
		readin >> trash;
		//convert string to integer?
}
else
{
	//convert string to int?
	// push back to next dog name and integer(number)
}

Recommended Answers

All 3 Replies

If the string looks something like this: Dani Web 100 Then just use getline() to read the entire line, and start at the end of the string and back up until you find the first space, split the string at that point.

*mumbles something about find_last_of*

commented: perfect solution :) +36

*mumbles something about find_last_of*

Of course :) perfect solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.