help with loops

This is a discussion on help with loops within the C++ Programming forums, part of the General Programming Boards category; 'i have been working on this assignment for like 45 hours. the file reads from Piano.data and outputs into report.out. ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    1

    help with loops

    'i have been working on this assignment for like 45 hours. the file reads from Piano.data and outputs into report.out. I have everything working, except for the fact that it will not repeat until the end of file is read. It just reads one display. Also the totalScore for each player seems to come one tenth off every time i run the program. When i try to call the PrintHigh function it is giving me a undefined reference. If someone could tell me where I went wrong it would be great. In the example my teacher gave the while(fin >> pianoPlayer) read until end of file but in mine wont. Anyways here is an example of piano.data. this is two players there are 16

    6010 1
    7.0 8.5 7.0 8.5
    7.0 7.5 8.0 7.5
    7.5 8.0 7.5 8.0
    -1
    6012 1
    7.5 7.0 9.5 9.0
    7.0 6.0 10.0 10.0
    7.5 7.5 10.0 9.5
    -1

    Here is what report.out looks like



    Federation of Music Teachers
    Mardi Gras Piano Invitational Competition
    Baton Rouge, Louisiana

    Piano Player: 6010
    Level: Primary

    Type 1 Type 2 Type 3 Type 4 Overall
    Judge 1 7.0 8.5 7.0 8.5 31.0
    Judge 2 7.0 7.5 8.0 7.5 23.0
    Judge 3 7.5 8.0 7.5 8.0 23.5
    Total: 77.3


    Here is my code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    
    using namespace std;
    
    void SetFormat	(ofstream& fout);
    void Header	(ofstream& fout);
    double Process	(ifstream& fin, ofstream& fout);
    void PrintHigh  (ofstream& fout, double highScore);
    
    int main()
    {
    	ifstream fin;
    	ofstream fout;
    	double highScore;
    	
    	fin.open("Piano.data");
    	if (fin.fail())
    	{
    		cout << "ERROR: Input File\n";
    		exit(1);
    	}
    	
    	fout.open("Report.out");
    	if (fout.fail())
    	{
    		cout << "ERROR: Output File\n";
    		exit(1);
    	}
    	
    	SetFormat(fout);
    	Header(fout);
    	highScore = Process(fin, fout);
    	PrintHigh  (fout, highScore);
    	fin.close();
    	fout.close();
    	return 0;
    }
    
    void SetFormat(ofstream& fout)
    {
    	fout.setf(ios::fixed);
    	fout.setf(ios:: showpoint);
    	fout.precision(1);
    	return;
    }  // end SetFormat
    
    void Header(ofstream& fout)
    {
    	fout << "     \n\n      Federation of Music Teachers\n";
    	fout << " Mardi Gras Piano Invitational Competition\n";
    	fout << "	Baton Rouge, Louisiana\n";
    	return;
    }  // end header
    
    double Process(ifstream& fin, ofstream& fout)
    {
    	int numCategories;
    	int pianoPlayer;
    	int level;
    	int judgeNumber;
    	double scoreType_1, scoreType_2, scoreType_3, scoreType_4;
    	double highScore = -999999.99;
    	double score;
    	double totalScore;
    	int scoreType;
    	double sum;
    	char next;
    	fin >> numCategories;
    	while (fin >> pianoPlayer)
    	{
    		fin >> level;
    		judgeNumber = 0;
    		fout << "Piano Player: ";
    		fout.width(1);
    		fout << pianoPlayer << "\n";
    		fout << "Level: ";
    		switch(level)
    		{
    			case 1:		
    				fout << " Primary\n";
    				break;
    			case 2:
    				fout << " Intermediate\n";
    				break;
    			case 3:
    				fout << " Advanced Intermediate\n";
    				break;
    			case 4: 
    				fout << " Senior\n";
    				break;
    			default: 
    				cout << " Invalid Code\n";
    				
    				break;
    		}
    		fin >> score;
    			
    		while (score != -1)
    		{
    			judgeNumber ++;
    			fout << "Judge " << judgeNumber << "  ";
    			fout << score << "  ";
    			for (scoreType = 1; scoreType < numCategories; scoreType ++)
    			{
    				fin >> score;
    				fout << score << "  ";
    				sum += score;
    			}
    			fout << "\n";
    			fin >> score;
    			
    		}
    		return highScore;
    	}
    		
    void PrintHigh (ofstream& fout, double highScore);
    	{
    		fout << "\n\n\n";
    		fout << "Highest Score:  ";
    		fout.width(5);
    		fout << highScore << endl;
    		return highScore;
    	} //end printhigh	
    	
    }
    I hope i am not asking too much, but I keep on trying to fix it and nothing seems to work. I have been working on this for days. I hope I am not violating the board policies. I do not wish for anyone to do things for me, but my teacher is not that good and doesnt really help us and I need this assignment to be perfect to maintain my grade. I have a 100 average on my assignments and just cannot seem to get this one.

    Thanks in advance
    Jeff

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > return highScore;
    This is inside your while (fin >> pianoPlayer)
    So you only get to read it once.

    Also, there are some seriously mis-matched braces in that code.
    For example, PrintHigh seems to be part of Process.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 12:18 AM
  3. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM
  4. for loops in C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-15-2001, 05:09 PM
  5. exiting loops with ease?
    By KingRuss in forum Game Programming
    Replies: 3
    Last Post: 09-24-2001, 08:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21