943,692 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1733
  • C++ RSS
Feb 22nd, 2009
0

String/Integer C++ Question

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. bool isString = true;
  2. string Dogname = " ";
  3. int trash;
  4.  
  5. while (isString = true)
  6. {
  7. trash = " ";
  8.  
  9. strcat(Dogname, trash);
  10. }
  11.  
  12. if(atoi(trash(0)) >> 47 && atoi(trash(0)) < 58)
  13. {
  14. readin >> trash;
  15. //convert string to integer?
  16. }
  17. else
  18. {
  19. //convert string to int?
  20. // push back to next dog name and integer(number)
  21. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Yersey is offline Offline
1 posts
since Feb 2009
Feb 22nd, 2009
0

Re: String/Integer C++ Question

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.
Last edited by Ancient Dragon; Feb 22nd, 2009 at 10:17 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Feb 22nd, 2009
1

Re: String/Integer C++ Question

*mumbles something about find_last_of*
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Feb 22nd, 2009
0

Re: String/Integer C++ Question

Click to Expand / Collapse  Quote originally posted by Comatose ...
*mumbles something about find_last_of*
Of course perfect solution.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC