| | |
C++ help (string class)
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2007
Posts: 110
Reputation:
Solved Threads: 2
Hi again everyone. I'm having a problem with some code I am writing. I need to take an input file change what is on it, then print out to the screen. What I want to change in the file is re-formatting it so that there is Last name First name then 10 scores, if there are less than ten scores replace the empty ones with 0's. here is what I got so far...
this is just a start, but could someone advise me where to go with this, I am kind of at a standstill.
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <cctype> #include <cstdlib> #include <string> #include <fstream> #include <string> #include <sstream> using namespace std; string getLast(string line); bool moreToRead(istream& ins); int main() { string line; ifstream in; { in.open("in.text"); if (in.fail()) { cerr << "Failed to open in.text.\n"; exit(1); } } while(moreToRead(in)) { getline(in, line); getLast(line); cout << line << endl; } in.close(); return (0); } // Function writen by William Austad bool moreToRead(istream& ins) { char ch; ins.get(ch); while(!ins.eof()) { if (!isspace(ch)) { ins.putback(ch); return (true); } else { ins.get(ch); } } return (false); } string getLast(string line) { string temp = line; stringstream tempStream; string token; if (tempStream >> token) { return (token); } else { temp = "Error"; } return (temp); }
this is just a start, but could someone advise me where to go with this, I am kind of at a standstill.
Last edited by kylcrow; May 9th, 2007 at 7:56 pm.
you don't need function moreToRead() Change lines 27-31 to be something like this:
What format is the file? FirstName LastName <scores> ?
C++ Syntax (Toggle Plain Text)
while( getline(in,line) ) { // blabla }
What format is the file? FirstName LastName <scores> ?
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.
Wow -- it seems like you're way over-complicating the whole procedure. Just make a loop like AD mentioned, and then use a stringstream to split up the words:
The stringstream will automatically ignore the whitespace in your file.
C++ Syntax (Toggle Plain Text)
// loop... std::istringstream iss(line); iss >> lastName; iss >> firstName; // another loop to extract 10 scores, but you get the idea...
The stringstream will automatically ignore the whitespace in your file.
Last edited by John A; May 9th, 2007 at 9:29 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
•
•
ok, using those methods I got all the last names. Now I do a variation of that to get the first name, then the numbers.
FirstName LastName s1 s2 s3 ...
getline reads the entire line, and stores it in a string. Then it's put into a stringstream. The first >> will spit out the first name. The second time you use >>, it will spit out the last name. And after that it spits out the scores, until there's nothing left in the stringstream, in which case you're finished with the line, and you carry on to the next line.•
•
•
•
Then finally use member functions of the string class to concatinate them all together?
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Apr 2007
Posts: 110
Reputation:
Solved Threads: 2
Ok, so does this code look a lot better?
That code only gets the first code tho. So I tired to do a loop for scores and got weird answers. Why doesn't this work?
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <cctype> #include <cstdlib> #include <string> #include <fstream> #include <string> #include <sstream> using namespace std; string process_line(string, string, string, string); bool moreToRead(istream& ins); int main() { string line; ifstream in; { in.open("in.text"); if (in.fail()) { cerr << "Failed to open in.text.\n"; exit(1); } } string last; string first; string scores; while(getline(in, line)) { cout << process_line(line, last, first, scores) << endl; } in.close(); return (0); } // Function writen by William Austad bool moreToRead(istream& ins) { char ch; ins.get(ch); while(!ins.eof()) { if (!isspace(ch)) { ins.putback(ch); return (true); } else { ins.get(ch); } } return (false); } string process_line(string line, string last, string first, string scores) { string name; istringstream lineStream(line); lineStream >> last; lineStream >> first; lineStream >> scores; name = last + " " + first + " " + scores; return (name); }
That code only gets the first code tho. So I tired to do a loop for scores and got weird answers. Why doesn't this work?
c++ Syntax (Toggle Plain Text)
for (int i = 0 ; i < 10 ; i++) { scores = lineStream << scores; } cout << last << " " << first << " " << scores << endl;
>process_line(string line, string last, string first, string scores) {
Why do you pass
>Why doesn't this work?
Because you're doing something weird. Just do it something like this:
Why do you pass
last, first, and scores, when you just use them to fill up data from the stringstream?>Why doesn't this work?
Because you're doing something weird. Just do it something like this:
C++ Syntax (Toggle Plain Text)
// loop of course lineStream >> tempScore; name += " " + tempScore;
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
•
•
Now I just have to figure out how to deal with lines without 10 scores and replaces the missing scores with zero.
C++ Syntax (Toggle Plain Text)
if (iss >> score) { /* successful */ }
I'll let you figure out the rest.
Last edited by John A; May 9th, 2007 at 11:32 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- program depending on String class (Java)
- String class (C++)
- get token from string using string class (C++)
- error in user defined string class (C++)
- String Class Of Java (Java)
Other Threads in the C++ Forum
- Previous Thread: how to get files and store them...
- Next Thread: Help with a program
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






