Function Call Problem

Function call has no syntax errors but does not produce desired output

Page 1 of 1

1 Replies - 662 Views - Last Post: 28 April 2008 - 01:58 PM Rate Topic: -----

#1 WELA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-April 08

Function Call Problem

Posted 28 April 2008 - 01:32 PM

Hello, I am having a slight problem with my C++ program. The purpose of the program is to input names and test scores from a text file into an array of structs. The progrom works fine when in main, however, when I insert the code into seperate functions I don't get any errors, but the results do not appear.

Here is my code:

//Preprocessor Directives
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <fstream>

using namespace std;

//Global Variables
ifstream fin;
struct studentType{
	   string name[2];
	   int scores[6];
	   };

//Function Prototypes
void headerfn();
void inputfn(studentType it210[40], int counter);
void outputfn(studentType it210[40],int counter);

//Function Definitions
int main()
{
	//Background Color Change
	system("color f0");
	
	//Function Call
	headerfn();
	
	//Declarations
	studentType it210[40];
	int counter = 0;	
	string filename;
	
	//Input File Command
	cout << "Please enter an input filename: ";
	cin >> filename;
	cout << endl;
	fin.open(filename.c_str());
	
	//Input File Error Check
	if(!fin){
			 cout << "Input Failure!" << endl << endl;
			 system("pause");
			 return 1;
			 }//end of error check
			 
	cout << "Filename: (" << filename << ") has been validated" << endl << endl;
	cout << "Press any key to perform calculations";
	getch();
	system("cls");
	
	inputfn(it210, counter);
	
	outputfn(it210, counter);

	cout << "Number of records = " << counter << endl;
	
	cout << "\n" << endl;
	system("pause");
}//End of Main

//*************************************************************************
//Header Function
//*************************************************************************
void headerfn(){
	 cout << "_________________________________________________"
		  << endl << endl;
	 cout << " C++ "
		  << endl;
	 cout << " Student Grades											"
		  << endl << endl;
	 cout << "_________________________________________________"
		  << endl << endl;
		  }//End of headerfn
//*************************************************************************
//Input Function
//*************************************************************************
void inputfn(studentType it210[40], int counter){
	 while(fin&&counter<40){
							for(int col=0; col<2; col++)
				fin >> it210[counter].name[col];
				for(int col = 0; col < 6; col++)
				fin >> it210[counter].scores[col];
				counter++;
				if(fin.peek()=='n')fin.ignore();
				}//end of while loop
	 }//end of input function
//*************************************************************************
//Output Function
//*************************************************************************
void outputfn(studentType it210[40],int counter){
	 cout << fixed << showpoint << left << setprecision(2);
						   
	for(int row = 0; row < counter; row++){
			cout << setw(25) << it210[row].name[0] + " " + it210[row].name[1];
			for(int col = 0; col < 6; col++)
			cout << setw(5) << it210[row].scores[col];
			cout << endl;
			}//end of outer for loop
	}//end of outputfn

This post has been edited by WELA: 28 April 2008 - 01:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Function Call Problem

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1383
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Function Call Problem

Posted 28 April 2008 - 01:58 PM

That's because you're opening the file in your main and trying to perform operations on it in other functions. So, you have two choices:
  • You could open/close the file from within the function itself]
  • You could open the file in main and send it as a parameter to your functions

Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1