#include <iostream>
#include <fstream>
usingnamespace std;
void main (void)
{
float price , total =0;
int count = 0 ;
char filename [] = "C:\\testfileiol.txt";
ifstream inputable;
inputfile.open (filename , ios::in);
if (inputfile.fail())
{cout<<"Opening file "<<filename<<"for reading";
cout<<"--------------------------------------------";
cout<<"The file could not be opened!";
cout<<"Possible errors:\n";
cout<<"1. The file does not exist.";
cout<<"2. The path was not found.";
return 0;
exit(1);
}
else
{
cout<<"The "<<filename<<" File was opened sucessfully!";
cout<<"Reading data and do some calculation";
inputfile>>price;
while(!inputfile.eof())
{
total +=price;
count++;
cout<"Item price # "<<count<<"is "<<price<<endl;
inputfile>>price;
}
cout<<"The total price for "<<count<<" items is: "<<total<<endl;
cout<<"\n--------Done--------\n"<<endl;
inputfile.close();
if(inputfile.fail())
{
cout<<"The "<<filename<<" file could not be closed!";
exit(1);
}
else
cout<<"The "<<filename<<" file was closed successfully!";
}
}
The errors are::
5 16 G:\Assignment 3 programming\testing.cpp [Error] '::main' must return 'int'
G:\Assignment 3 programming\testing.cpp In function 'int main()':
12 2 G:\Assignment 3 programming\testing.cpp [Error] 'inputfile' was not declared in this scope
21 9 G:\Assignment 3 programming\testing.cpp [Error] 'exit' was not declared in this scope
32 28 G:\Assignment 3 programming\testing.cpp [Error] invalid operands of types 'const char [14]' and 'int' to binary 'operator<<'
42 15 G:\Assignment 3 programming\testing.cpp [Error] 'exit' was not declared in this scope
5 16 G:\Assignment 3 programming\testing.cpp [Error] '::main' must return 'int'
This is line 5. Let's look at line 5. void main (void)
Aha, this is wrong. In C++, main returns an int, and in C++ when a function accepts no parameters, we do not mark it void - we just leave it empty. I am surprised that you have got this far in C++ and did not know the correct form of the main function. int main()
12 2 G:\Assignment 3 programming\testing.cpp [Error] 'inputfile' was not declared in this scope
This is line 12. Let's look at line 12. inputfile.open (filename , ios::in);
The error message is complaining about inputfile not being declared. Where was it declared? Nowhere. It does not exist. inputable is not the same as inputfile
The rest require similar examination of the lines it tells you and thinking about them.