954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String/Integer C++ Question

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)
}
Yersey
Newbie Poster
1 post since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

*mumbles something about find_last_of*

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 
*mumbles something about find_last_of*

Of course :) perfect solution.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You