Question:
converting string to int using atoi()?
?
2009-09-27 11:31:20 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.
Four answers:
anonymous
2009-09-29 11:13:20 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;

}
hs
2009-09-27 11:45:39 UTC
Try declaring scoreVal outside the for loop. It may be throwing the error because you are declaring & initializing the scoreVal while converting using atoi().



Just give it a shot.
?
2009-09-27 11:46:41 UTC
Use c_str() to get the underlying C string data from a C++ string type, e.g. change:



--> int scoreVal = atoi(score);



to



--> int scoreVal = atoi(score.c_str());
penski
2016-12-16 22:12:45 UTC
You gotta keep in mind that for the duration of C, characters are examine as "numbers" too (their ASCII values). you basically could desire to subtract '0' from each and each characters to get the huge form (because of the fact it is going to calculate the version between ASCII fee of 0 and the ASCII fee of regardless of character it somewhat is you're attempting to transform).


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