Program error?

My program keeps giving me the same error each time....

Page 1 of 1

9 Replies - 869 Views - Last Post: 03 March 2010 - 01:02 PM Rate Topic: -----

#1 creative_dream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 17-February 10

Program error?

Posted 03 March 2010 - 10:32 AM

I am supposed to generate a program, but it keeps on telling me " '{' : missing function header (old-style formal list?) ":


(1) Generate a data file to save students' grade. Allow the user to enter id, name and grade of each student as well as the total number of students. There should be a header in the first row. Here is an example of the data file.

ID NAME GRADE

01 Mary 100.00
02 Tom 98.35
03 John 88.99
......

(2) If the grade is <0 or >100, ask the user to re-enter the correct grade.

(3) After the data file is generated, read the file and print the content to the screen.

The tasks describe above should be completed in one program.


I have created a program:

#include<fstream>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;


int main()

{
	int ID, amountofstudents;
	double Grade;
	string Name;

	ofstream OOO;
	OOO.open ("code.dat");
	cout<<"Please enter amount of students"<<endl;
	cin>>amountofstudents;
//header

for( int i=1; i<=ID ; ++i ) 
cout << left << setw(5) << i ;
cout<<endl<<"--"; 

for( int i=1; i<=amountofstudents ; ++i ) 
{
	cout<<"Please enter ID of student."<<endl;
	cin>>ID;
	cout<<"Please enter Name of student"<<endl;
	cin>>Name;
	cout<<"Please enter grade of student"<<endl;
	cin>>Grade;

	while (Grade<0 || Grade>100)
	{
		cout<<"Unable to calculate, please enter grade again"<<endl;
		cin>>Grade;
	}

	
	OOO<<setw(10)<<ID<<setw(10)<<Name<<setw(10)<<Grade<<endl;
}

system("pause"); 
return 0;
}

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

//int main(void)
{
	int ID, amountofstudents;
	double Grade;
	string Name;

//ifstream III.;
//string filename;
//III.open("datafile.dat");
//III>>S1>>S2
//int main()

   ifstream III; 
   III.open("datafile.dat"); 
   if(!III) 
   { 
      cerr << "Error: file could not be opened" << endl;
      exit(1);
   }
  III >> Grade;
   while ( !III.eof() ) 
   { 
      cout<< "The result is" << Grade << endl;
      III>> amountofstudents>>ID; 
   }
   
   III.close();
   cout << "End-of-file reached.." << endl;
   return 0;
}

This post has been edited by JackOfAllTrades: 03 March 2010 - 10:50 AM
Reason for edit:: Added code tags. PLEASE!!! [code]...PUT YOUR CODE IN HERE!!!...[/code]


Is This A Good Question/Topic? 0
  • +

Replies To: Program error?

#2 I3AnThRaX69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-April 08

Re: Program error?

Posted 03 March 2010 - 10:41 AM

I can't tell if midway down the text thats a different function you're trying to call but this here...

//int main(void) << you're commenting out the name of the function. 
{
int ID, amountofstudents;
double Grade;
string Name;



the compiler is looking and just seeing {, it's complaining that you have to name it such as...
int main(){ 
return 0;
}


this is saying that main will return int, which you follow through with return 0 at the end. you have to have a some form of return value or a void of some sort.
Was This Post Helpful? 0
  • +
  • -

#3 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Program error?

Posted 03 March 2010 - 10:43 AM

I am not sure, but you have some strange code following the end of main. Was that supposed to be a function?

#include<fstream>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;


int main()

{
    int ID, amountofstudents;
    double Grade;
    string Name;

    ofstream OOO;
    OOO.open ("code.dat");
    cout<<"Please enter amount of students"<<endl;
    cin>>amountofstudents;
    //header

    for( int i=1; i<=ID ; ++i ) 
    cout << left << setw(5) << i ;
    cout<<endl<<"--"; 

    for( int i=1; i<=amountofstudents ; ++i ) 
    {
        cout<<"Please enter ID of student."<<endl;
        cin>>ID;
        cout<<"Please enter Name of student"<<endl;
        cin>>Name;
        cout<<"Please enter grade of student"<<endl;
        cin>>Grade;

        while (Grade<0 || Grade>100)
        {
            cout<<"Unable to calculate, please enter grade again"<<endl;
            cin>>Grade;
        }


        OOO<<setw(10)<<ID<<setw(10)<<Name<<setw(10)<<Grade<<endl;
    }

    system("pause"); 
    return 0;
}

/*

// what is this below supposed to be?

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

//int main(void)
{
int ID, amountofstudents;
double Grade;
string Name;

//ifstream III.;
//string filename;
//III.open("datafile.dat");
//III>>S1>>S2
//int main()

ifstream III; 
III.open("datafile.dat"); 
if(!III) 
{ 
cerr << "Error: file could not be opened" << endl;
exit(1);
}
III >> Grade;
while ( !III.eof() ) 
{ 
cout<< "The result is" << Grade << endl;
III>> amountofstudents>>ID; 
}

III.close();
cout << "End-of-file reached.." << endl;
return 0;
} 
*/


Was This Post Helpful? 1
  • +
  • -

#4 creative_dream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 17-February 10

Re: Program error?

Posted 03 March 2010 - 10:53 AM

I am trying to read the 000 file in that section, but it kept telling me i had too many int main() ---or that I had already defined int main() in the beginning....


View PostMartyn.Rae, on 03 March 2010 - 09:43 AM, said:

I am not sure, but you have some strange code following the end of main. Was that supposed to be a function?

#include<fstream>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;


int main()

{
    int ID, amountofstudents;
    double Grade;
    string Name;

    ofstream OOO;
    OOO.open ("code.dat");
    cout<<"Please enter amount of students"<<endl;
    cin>>amountofstudents;
    //header

    for( int i=1; i<=ID ; ++i ) 
    cout << left << setw(5) << i ;
    cout<<endl<<"--"; 

    for( int i=1; i<=amountofstudents ; ++i ) 
    {
        cout<<"Please enter ID of student."<<endl;
        cin>>ID;
        cout<<"Please enter Name of student"<<endl;
        cin>>Name;
        cout<<"Please enter grade of student"<<endl;
        cin>>Grade;

        while (Grade<0 || Grade>100)
        {
            cout<<"Unable to calculate, please enter grade again"<<endl;
            cin>>Grade;
        }


        OOO<<setw(10)<<ID<<setw(10)<<Name<<setw(10)<<Grade<<endl;
    }

    system("pause"); 
    return 0;
}

/*

// what is this below supposed to be?

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

//int main(void)
{
int ID, amountofstudents;
double Grade;
string Name;

//ifstream III.;
//string filename;
//III.open("datafile.dat");
//III>>S1>>S2
//int main()

ifstream III; 
III.open("datafile.dat"); 
if(!III) 
{ 
cerr << "Error: file could not be opened" << endl;
exit(1);
}
III >> Grade;
while ( !III.eof() ) 
{ 
cout<< "The result is" << Grade << endl;
III>> amountofstudents>>ID; 
}

//not sure why i had to do this could this be an error as well??

III.close();
cout << "End-of-file reached.." << endl;
return 0;
} 
*/



It kept telling me I had defined int main() in the beginning of the file, so I commented it out hoping the error would stop and it did...did i need it??

View PostI3AnThRaX69, on 03 March 2010 - 09:41 AM, said:

I can't tell if midway down the text thats a different function you're trying to call but this here...

//int main(void) << you're commenting out the name of the function. 
{
int ID, amountofstudents;
double Grade;
string Name;



the compiler is looking and just seeing {, it's complaining that you have to name it such as...
int main(){ 
return 0;
}


this is saying that main will return int, which you follow through with return 0 at the end. you have to have a some form of return value or a void of some sort.

Was This Post Helpful? 0
  • +
  • -

#5 I3AnThRaX69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-April 08

Re: Program error?

Posted 03 March 2010 - 10:59 AM

Don't call it main, you can only have one main. (as far as I know on that).

Call it something else like
void readFile(){}



Call it from your main as you need it.
Like if I wanted to send off a value or run a function for output like hello world..
#include <iostream>
using namespace std;

void printHello();  //declaration of the function

int main(){

	printHello();
	return 0;
}

void printHello() //implementation of it
{
	cout << "Hello world" << endl;
}



Hope this helps and makes sense.
Was This Post Helpful? 0
  • +
  • -

#6 creative_dream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 17-February 10

Re: Program error?

Posted 03 March 2010 - 10:59 AM

thanks I will try this!!! and post back.....

This post has been edited by creative_dream: 03 March 2010 - 11:01 AM

Was This Post Helpful? 0
  • +
  • -

#7 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Program error?

Posted 03 March 2010 - 11:03 AM

for( int i=1; i<=ID ; ++i )


Not a good practice! Although it may work, arrays in C++ should be initialized starting from 0 -> N - 1. So:
for (int i = 0; i < ID; ++i)


I think it would be better. Also, you should learn the BASIC syntax of a C++ program, before trying to manipulate files.
PS: A header file should be included ONLY once in a source/header file. Not to mention the compiling time increase, but you will probably get stuck in one thousand errors +, just because of a header included twice.

This post has been edited by sarmanu: 03 March 2010 - 11:05 AM

Was This Post Helpful? 0
  • +
  • -

#8 creative_dream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 17-February 10

Re: Program error?

Posted 03 March 2010 - 11:07 AM

Thanks I will try that as well, the thing is my teacher tells us to learn through tough problems-----I am trying to figure this all out...

View Postsarmanu, on 03 March 2010 - 10:03 AM, said:

for( int i=1; i<=ID ; ++i )


Not a good practice! Although it may work, arrays in C++ should be initialized starting from 0 -> N - 1. So:
for (int i = 0; i < ID; ++i)


I think it would be better. Also, you should learn the BASIC syntax of a C++ program, before trying to manipulate files.
PS: A header file should be included ONLY once in a source/header file. Not to mention the compiling time increase, but you will probably get stuck in one thousand errors +, just because of a header included twice.

Was This Post Helpful? 0
  • +
  • -

#9 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Program error?

Posted 03 March 2010 - 11:16 AM

the thing is my teacher tells us to learn through tough problems
Lol, learning C++ syntax by yourself could be a pain. I remember when I first looked at a C++ program. It was the classic "Hello, World!" program. When I saw the '#' from the #include, I pretty much started to get scared. The "cout << " thing was a pain to learn. I don't know why. I was always confusing << with >>. I was also forgetting about "int main()", not to mention the header file names, which took me days to learn. Ehh ... my point is: the teacher should learn you the syntax first, then, you will develop a good logic through problems. Without knowing the syntax, you could be the best mathematician in the world. You won't even know how to compute the sum between two numbers, in C++ :)

This post has been edited by sarmanu: 03 March 2010 - 11:17 AM

Was This Post Helpful? 0
  • +
  • -

#10 creative_dream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 17-February 10

Re: Program error?

Posted 03 March 2010 - 01:02 PM

Thanks so much for the help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1