Thread: Inputting from a text file

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Inputting from a text file

    Hey guys, any help would be greatly appreciated. I'm trying to input data from a text file into my code but it doesnt seem to be working out. here is what I have so far.
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <iomanip>
    
    using namespace std; 
    
    struct studentInfo
    {
    	int id;
    	int score;
    	char grade;
    };
    
    const int MAX_NUM = 50;
    studentInfo data[MAX_NUM];
    bool successful = false;
    bool readStuData(ifstream&, bool&, studentInfo data[]);
    float getAverage(studentInfo data[], float& mean);
    void assignGrades(studentInfo data[], float);
    void reportResults(studentInfo data[]);
    
    int main(int argc, char *argv[], float average)
    {
    	ifstream inData("C:\Users\Owner\Desktop\scores.txt", ios::in);
    	readStuData(inData, successful, data);
    	getAverage(data, average);
    	cout<<"Student "<<data[0].id<<"'s test score is "<<data[0].score<<endl;
    	assignGrades(data, average);
    	reportResults(data);
    	cin.ignore();
    	cin.get();
    	return 0;
    }
    
    bool readStuData(ifstream& inData, bool& successful, studentInfo data[])
    {
    
    	int number;
    
    
    	if(inData.fail())
    {
    	cerr << "Error: Program cannot be run." << inData << endl;
    	successful = false;
    	return EXIT_FAILURE;
    }
    	else
    {
    	successful = true;
    	return successful;
    	inData >> data[0].id >> data[0].score;
    	number = 0;
    	while (!inData.eof())
    {
    	if (number=0, number<MAX_NUM, number++)
    { 
    	inData >> data[number].id >> data[number].score;
    	number++;
    }
    }
    }
    	inData.close();
    	return successful;
    }
    	float getAverage(studentInfo data[], float& mean)
    {
    	float sum = 0.0;
    	int i;
    
    	for (i=0; i<MAX_NUM; i++)
    {
    	sum = data[i].score + sum;
    }
    	mean = sum / MAX_NUM;
    
    	return (mean);
    }
    	void assignGrades(studentInfo data[], float average)
    {
    	int i;
    	for (i=0; i<MAX_NUM; i++)
    {
    	if ((data[i].score <= average + 10) || (data[i].score >= average - 10))
    {
    	data[i].grade = 'S';
    }
    	if (data[i].score > average + 10)
    {
    	data[i].grade = 'O';
    }
    	if (data[i].score < average - 10)
    {
    	data[i].grade = 'U';
    }
    }
    }
    	void reportResults(studentInfo data[])
    {
    	ofstream out("C:\Users\Owner\Desktop\report.txt");
    	out << "STUDENT SCORES"<< setw(4) <<"Student ID #"<< setw(10)<<"Score"<< setw(14)<<"Grade"<< endl;
    	for (int i=0;i<MAX_NUM; i++)
    	out << setw(8) << data[i].id << setw(14)<< data[i].score << setw(14)<< data[i].grade << endl;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And what could "not working out for you" possibly mean in this case?

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Looks like it's also over here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM