0

Hmm. I am converting a string into an int.

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?

4
Contributors
12
Replies
13
Views
9 Years
Discussion Span
Last Post by vijayan121
0

convert a string to int this way.

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) ;
0

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.

0

>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?

0

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.

0

Yeah.

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.
0
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);
}

There that compiles, but I am getting the wrong sum. Any suggestions?

0

Is this sum messing up because it needs to be in its own loop?

0

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.

0

Ok sorry for the confusion, in not showing all the code. Here it is...

#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);
}

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?

0

And here is in.text...

Glamonski Melvin 10 10 10 3 5 10 4
Pidwell Xavier 10 10 9 2 10 7 2      4 9 1
Beat Dead
Austad William 10       10      10 10 10 10 10 10 10 10
0

Hey, I figured it out on my own. Thank you to all who helped me get through it. Thanks again

0

if you just want the sum, this would be simpler:

int sum2(string line) 
{
  istringstream stm(line) ;
  string  x ; stm >> x >> x ;
  int sum=0 ;  int n = 0 ;
  while( stm >> n )  sum += n ;
  return sum ;
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.