May 6, 2012 at 5:31pm UTC
I am trying to use atof to convert from a string to a double. I get this error message:
3 IntelliSense: no suitable conversion function from "std::string" to "const char *" exists
I'm not quite understanding the error since i'm trying to convert from a string to a double. The line i'm concerned with is near the bottom. It is:
income[count].bonus = atof(bonus[1]); and is in bold font.
Here is the code:
#include<string>
#include<iostream>
#include<iomanip>
#include<fstream>
#include<cstdlib>
using namespace std;
struct incomeInfo {
char id[12];
char name[30];
double pay;
double bonus;
double totPay;
};
//Function Prototypes
void getIncome(incomeInfo[],int&);
void Compute(incomeInfo[],int);
//void Display(incomeInfo[],int);
int main()
{
incomeInfo income[20];
int count = 0;
getIncome(income,count);
Compute(income,count);
//Display(income,count);
return 0;
}
void getIncome(incomeInfo income[], int &count)
{
//ifstream inputFile;
char line[50];
fstream inputFile("Income.txt", ios::in);
//inputFile.open("Income.txt");
if (inputFile.fail())
{
cout << "\n\n\tError opening file:" << "\n\n\t";
system("pause");
exit(1);
}
else
{
while(!inputFile.eof())
{
inputFile.getline(line,50,',');
strcpy(income[count].id, line);
inputFile.getline(line,50,',');
strcpy(income[count].name, line);
inputFile.getline(line,50,',');
income[count].pay = atof(line);
income[count].bonus = 0;
income[count].totPay = 0;
cout << income[count].id << endl;
cout << income[count].name << endl;
cout << income[count].pay << endl;
cout << income[count].bonus << endl;
cout << income[count].totPay << endl;
cout << "\n";
count++;
}
}
inputFile.close();
return;
}
void Compute(incomeInfo income[], int count)
{
//string input; // holds file input
fstream inputFile("Bonus.txt", ios::in);
string bonus[20];
count = 0;
if (inputFile)
{
//Read an item using , as a delimiter.
getline(inputFile, bonus[count], ',');
//While last read operation was successful
//continue.
while (inputFile)
{
count ++;
//Read an item using , as delimeter
getline(inputFile, bonus[count], ',');
}
inputFile.close();
count = 0;
while (count < 20)
{
if (bonus[0] == income[count].id)
cout << income[count].id << endl;
income[count].bonus = atof(bonus[1]);
//count ++;
if (bonus[2] == income[count].id)
cout << income[count].id << endl;
if (bonus[4] == income[count].id)
cout << income[count].id << endl;
count ++;
}
} system("pause");
}
Any clues would be greatly appreciated.. thanks!