String to Int

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2007
Posts: 110
Reputation: kylcrow is an unknown quantity at this point 
Solved Threads: 2
kylcrow kylcrow is offline Offline
Junior Poster

String to Int

 
0
  #1
May 11th, 2007
Hmm. I am converting a string into an int.

  1. string sum(string line) {
  2. string first;
  3. string last;
  4. string scores;
  5. string output;
  6. istringstream lineStream(line);
  7. lineStream >> last;
  8. lineStream >> first;
  9. for (int i = 0; i < 10; i++) {
  10. string getScores = "0 ";
  11. lineStream >> getScores;
  12. scores += " " + getScores;
  13. }
  14. stringstream sumStream (stringstream::in | stringstream::out);
  15. sumStream >> scores;
  16. int num = 0;
  17. sumStream << num;
  18. return (num);

The error says that you cant convert an in to a const char*
what does that error mean?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: String to Int

 
0
  #2
May 11th, 2007
convert a string to int this way.
  1. string test = "1234" ;
  2. istringstream stm(test);
  3. int num = 0;
  4. stm >> num ;
  5.  
  6. // if using boost (boost lexical cast), it is easier
  7. num = boost::lexical_cast<int>(test) ;
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 110
Reputation: kylcrow is an unknown quantity at this point 
Solved Threads: 2
kylcrow kylcrow is offline Offline
Junior Poster

Re: String to Int

 
0
  #3
May 11th, 2007
That gave the same error as before. Does it have something to do with how I got scores? And I would rather not use boost.
Last edited by kylcrow; May 11th, 2007 at 2:09 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,734
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 738
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: String to Int

 
0
  #4
May 11th, 2007
>Does it have something to do with how I got scores?
It probably has something to do with returning an int from a function that's defined to return a string... Also, your function probably doesn't do what you want. Can you describe what you think it should be doing?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: String to Int

 
0
  #5
May 11th, 2007
Originally Posted by kylcrow View Post
That gave the same error as before. Does it have something to do with how I got scores? And I would rather not use boost.
i just tested the code (which i posted). compiles in both vc++ 8.0 and gcc 3.4.6. also works correctly in both.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 110
Reputation: kylcrow is an unknown quantity at this point 
Solved Threads: 2
kylcrow kylcrow is offline Offline
Junior Poster

Re: String to Int

 
0
  #6
May 11th, 2007
Yeah.
  1. string sum(string line) {
  2. string first;
  3. string last;
  4. string scores;
  5. string output; // declaring strings
  6. istringstream lineStream(line);
  7. lineStream >> last;
  8. lineStream >> first; // stores first 2 tokens in (line)
  9. for (int i = 0; i < 10; i++) {
  10. string getScores = "0 ";
  11. lineStream >> getScores;
  12. scores += " " + getScores; // creates string scores which has 10 numbers in it.
  13. }
  14. stringstream sumStream (stringstream::in | stringstream::out);
  15. sumStream >> scores; // stores ten numbers into stream
  16. int num = 0;
  17. sumStream << num; // converts string to int.
  18. return (num); // returns int.
Last edited by kylcrow; May 11th, 2007 at 2:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 110
Reputation: kylcrow is an unknown quantity at this point 
Solved Threads: 2
kylcrow kylcrow is offline Offline
Junior Poster

Re: String to Int

 
0
  #7
May 11th, 2007
  1. int sum(string line) {
  2. string first;
  3. string last;
  4. string scores;
  5. string output;
  6. istringstream lineStream(line);
  7. int num = 0;
  8. int sum = 0;
  9. lineStream >> last;
  10. lineStream >> first;
  11. for (int i = 0; i < 10; i++) {
  12. string getScores = "0 ";
  13. lineStream >> getScores;
  14. scores += " " + getScores;
  15. istringstream numStream(scores);
  16. numStream >> num;
  17. sum += num;
  18. }
  19. return (sum);
  20. }
There that compiles, but I am getting the wrong sum. Any suggestions?
Last edited by kylcrow; May 11th, 2007 at 2:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 110
Reputation: kylcrow is an unknown quantity at this point 
Solved Threads: 2
kylcrow kylcrow is offline Offline
Junior Poster

Re: String to Int

 
0
  #8
May 11th, 2007
Is this sum messing up because it needs to be in its own loop?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,621
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: String to Int

 
0
  #9
May 11th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 110
Reputation: kylcrow is an unknown quantity at this point 
Solved Threads: 2
kylcrow kylcrow is offline Offline
Junior Poster

Re: String to Int

 
0
  #10
May 11th, 2007
Ok sorry for the confusion, in not showing all the code. Here it is...

  1. #include <iostream>
  2. #include <cctype>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <fstream>
  6. #include <string>
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. string process_line(string);
  13. int sum(string);
  14. bool moreToRead(istream& ins);
  15.  
  16. int main()
  17. {
  18. string line;
  19. ifstream in;
  20.  
  21. { in.open("in.text");
  22. if (in.fail()) {
  23. cerr << "Failed to open in.text.\n";
  24. exit(1);
  25. }
  26. }
  27.  
  28. while(getline(in, line)) {
  29. cout << process_line(line) << endl;
  30. cout << "The sum of scores is " << sum(line) << endl << endl;
  31. }
  32.  
  33. in.close();
  34.  
  35. return (0);
  36. }
  37.  
  38.  
  39. // Function not needed for program, just ignore.
  40. bool moreToRead(istream& ins) {
  41. char ch;
  42. ins.get(ch);
  43. while(!ins.eof()) {
  44. if (!isspace(ch)) {
  45. ins.putback(ch);
  46. return (true);
  47. } else {
  48. ins.get(ch);
  49. }
  50. }
  51. return (false);
  52.  
  53. }
  54.  
  55. string process_line(string line) {
  56. string first;
  57. string last;
  58. string scores;
  59. string output;
  60. istringstream lineStream(line);
  61.  
  62. lineStream >> last;
  63. lineStream >> first;
  64.  
  65. for (int i = 0; i < 10; i++) {
  66. string getScores = "0 ";
  67. lineStream >> getScores;
  68. scores += " " + getScores;
  69. }
  70.  
  71. output = last + string(16, ' ').substr(last.size()) + first + string(10, ' ').substr(first.size()) + scores;
  72. return (output);
  73. }
  74.  
  75. int sum(string line) {
  76. string first;
  77. string last;
  78. string scores;
  79. string output;
  80. istringstream lineStream(line);
  81. int num = 0;
  82. int sum = 0;
  83. lineStream >> last;
  84. lineStream >> first;
  85.  
  86. for (int i = 0; i < 10; i++) {
  87. string getScores = "0 ";
  88. lineStream >> getScores;
  89. scores += " " + getScores;
  90. istringstream numStream(scores);
  91. numStream >> num;
  92. sum += num;
  93. }
  94.  
  95. return (sum);
  96. }
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?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC