| | |
String to Int
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2007
Posts: 110
Reputation:
Solved Threads: 2
Hmm. I am converting a string into an int.
The error says that you cant convert an in to a const char*
what does that error mean?
c++ Syntax (Toggle Plain Text)
string sum(string line) { string first; string last; string scores; string output; istringstream lineStream(line); lineStream >> last; lineStream >> first; for (int i = 0; i < 10; i++) { string getScores = "0 "; lineStream >> getScores; scores += " " + getScores; } stringstream sumStream (stringstream::in | stringstream::out); sumStream >> scores; int num = 0; sumStream << num; return (num);
The error says that you cant convert an in to a const char*
what does that error mean?
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
convert a string to int this way.
C++ Syntax (Toggle Plain Text)
string test = "1234" ; istringstream stm(test); int num = 0; stm >> num ; // if using boost (boost lexical cast), it is easier num = boost::lexical_cast<int>(test) ;
•
•
Join Date: Apr 2007
Posts: 110
Reputation:
Solved Threads: 2
Yeah.
c++ Syntax (Toggle Plain Text)
string sum(string line) { string first; string last; string scores; string output; // declaring strings istringstream lineStream(line); lineStream >> last; lineStream >> first; // stores first 2 tokens in (line) for (int i = 0; i < 10; i++) { string getScores = "0 "; lineStream >> getScores; scores += " " + getScores; // creates string scores which has 10 numbers in it. } stringstream sumStream (stringstream::in | stringstream::out); sumStream >> scores; // stores ten numbers into stream int num = 0; sumStream << num; // converts string to int. return (num); // returns int.
Last edited by kylcrow; May 11th, 2007 at 2:19 pm.
•
•
Join Date: Apr 2007
Posts: 110
Reputation:
Solved Threads: 2
c++ Syntax (Toggle Plain Text)
int sum(string line) { string first; string last; string scores; string output; istringstream lineStream(line); int num = 0; int sum = 0; lineStream >> last; lineStream >> first; for (int i = 0; i < 10; i++) { string getScores = "0 "; lineStream >> getScores; scores += " " + getScores; istringstream numStream(scores); numStream >> num; sum += num; } return (sum); }
Last edited by kylcrow; May 11th, 2007 at 2:39 pm.
You should probably post a working code here along with the explanation of what it does. Making us guess what your code does is not a good idea. Post the complete code (along with main) along with the sample input data supplied and the expected output.
I don't accept change; I don't deserve to live.
•
•
Join Date: Apr 2007
Posts: 110
Reputation:
Solved Threads: 2
Ok sorry for the confusion, in not showing all the code. Here it is...
Ok. The sum isn't adding up correctly. And I have honestly no idea why. Any suggestions for what I need to think about changing?
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); int sum(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); } } while(getline(in, line)) { cout << process_line(line) << endl; cout << "The sum of scores is " << sum(line) << endl << endl; } in.close(); return (0); } // Function not needed for program, just ignore. 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 first; string last; string scores; string output; istringstream lineStream(line); lineStream >> last; lineStream >> first; for (int i = 0; i < 10; i++) { string getScores = "0 "; lineStream >> getScores; scores += " " + getScores; } output = last + string(16, ' ').substr(last.size()) + first + string(10, ' ').substr(first.size()) + scores; return (output); } int sum(string line) { string first; string last; string scores; string output; istringstream lineStream(line); int num = 0; int sum = 0; lineStream >> last; lineStream >> first; for (int i = 0; i < 10; i++) { string getScores = "0 "; lineStream >> getScores; scores += " " + getScores; istringstream numStream(scores); numStream >> num; sum += num; } return (sum); }
![]() |
Similar Threads
- string to int conversion (C++)
- Conver int Array into a String (Java)
Other Threads in the C++ Forum
- Previous Thread: dont use printf or puts functions
- Next Thread: Improving my skills in C and C++
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






