| | |
String/Integer C++ Question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2009
Posts: 1
Reputation:
Solved Threads: 0
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.
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)
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) }
If the string looks something like this:
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.
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.
![]() |
Similar Threads
- Copy argv to string in C(newbie question) (C)
- extracting an arbitrary number of numbers from a string (C)
- Lookup tables - how to perform a switch using a string (C++)
- How to convert strSQLStmt result to integer? (VB.NET)
- Converting a String into an integer array (C)
- String to integer conversion (C)
Other Threads in the C++ Forum
- Previous Thread: Homework due tonight, can't figure it out! "error C2143: syntax error : missing ';'
- Next Thread: two dimensional matrix array problem
Views: 961 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets







perfect solution.