Question:
converting string to int using atoi()?
chris
2009-09-27 11:31:13 UTC
I'm working on a program that uses a input file that looks like this:

004566577
Prescott Schultz
86

003455675
Allison Dold
88

003455676
Brian Cantrell
87

003455467
Carrie Sutton
78

003455678
Zoe Ruedemann
89

003455678
Steven Allard
23

Each block of data is in order: Student ID, Student Name and Student's Test Score. I need to sum up the student's test scores so I can make an average of them. The problem I am running into is when using the atoi() function. The score line is being read into the main function as a string. When using atoi() to convert the score from string to int, I get this error:

error C2664: 'atoi' : cannot convert parameter 1 from 'std::string' to 'const char *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

I have no issues getting the string values and am able to parse the data into appropriate functions, but in order to get the average of the scores I must be able to convert the score from type string to type int. Here is where the problem is in my code (the arrows marks the spot):

while( !records.eof() )
{
int i=0;
for (i=0; i<1; i++){
std::getline (records, line);
std::getline (records, line2);
std::getline (records, line3);
std::getline (records, line4);
string id = line;
string name = line2;
string score = line3;
--> int scoreVal = atoi(score);
--> total = scoreVal + total;
cout << "id: " << id << endl;
cout << "name: " << name << endl;
cout << "the score: " << score << endl;
cout << line4 << endl;
}
loop++;
}

All I'm trying to do is sum up the score values read in from the file so that I can divide them by the sample size to get the average.

Any help is appreciated thanks.
Three answers:
anonymous
2009-09-29 11:16:37 UTC
Hm....



















I dont know if is wrong, but u r data file have 5 field:

ID

1st name

2nd name

score

cariage return (i dont like to say "Enter")



But u just read 4 data field.

I d like to use the data struct:

class numeric{public:int text;};

class name{public:char text[30];};

class member{public: name id, firstname, lastname; name score;};



opens data file with stream File I/O

ifstream myfile;

myfile.open("data.txt");

reads until last record with stream (i think, do not read buffer of stream, but if we do: add getline to read blank line):

member records;

while (!myfile.eof()){

myfile>>records.id.text;

myfile>>records.firstname.text;

myfile>>records.lastname.text;

myfile>>records.score.text;

char dumm; //include last cariage return

myfile>>dumm;

}
Mark aka jack573
2009-10-01 00:58:57 UTC
It is simple. The string class has a function called c_str



So you would do this:

int scoreVal = atoi( score.c_str() );



Good luck with it.
southern
2016-12-11 22:49:51 UTC
attempt asserting scoreVal exterior the for loop. it would desire to be throwing the blunders in view which you're asserting & initializing the scoreVal at the same time as changing making use of atoi(). in simple terms supply it a shot.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...