Can't Identify Errors. Input/Output files

Guys , I need help again. My lecturer gave me this coding and want it to work. I don't know what the errors are.

First I created a text file named testfileio1.txt and save it on drive C.
Then I added a couple of float digits into the text file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <fstream>
using namespace 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.
Last edited on
Topic archived. No new replies allowed.