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?

Recommended Answers

All 12 Replies

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

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.

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

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.

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

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

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.

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?

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

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

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 ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.