String/Integer C++ Question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 1
Reputation: Yersey is an unknown quantity at this point 
Solved Threads: 0
Yersey Yersey is offline Offline
Newbie Poster

String/Integer C++ Question

 
0
  #1
Feb 22nd, 2009
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,652
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1499
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: String/Integer C++ Question

 
0
  #2
Feb 22nd, 2009
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: String/Integer C++ Question

 
1
  #3
Feb 22nd, 2009
*mumbles something about find_last_of*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,652
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1499
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: String/Integer C++ Question

 
0
  #4
Feb 22nd, 2009
Originally Posted by Comatose View Post
*mumbles something about find_last_of*
Of course perfect solution.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 961 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC