help with loops

L

lsu420luv

'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
 
M

Marcus Kwok

'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.
[code snipped]

The first thing I noticed, is that your definition of PrintHigh() is
actually a function declaration. Double-check which curly braces match
each other, and get rid of the semicolon after the function prototype.

Once you fix that, notice that PrintHigh() is declared void, yet you are
returning a value.

After those errors are fixed, here are some warnings that my compiler
issued:

Unreferenced local variables:
scoreType_1
scoreType_2
scoreType_3
scoreType_4
next
totalScore

In Process(): not all control paths return a value

sum is used without being initialized


I haven't had time to look at the other parts of your code but this
should give you a start.
 
M

Mark P

'i have been working on this assignment for like 45 hours. t

Here is my code:

Some things that jumped out at me-- I haven't had the time to look very
carefully at the logic...
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;

You haven't initialized all of these variables and some of them even
appear to be used without initialization, notably "sum". Also I don't
see where others of them are ever used: next and the scoreType_i family.

In general you should declare variables as close to their point of use
as is reasonable, and within the smallest scope where they are needed.
Declaring a long list at the beginning as you've done is confusing to
the reader and error prone as well.
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;
}

I find this ugly and would recommend a const array of strings. For example:

const int numLevels = 4;
const char* const levels [numLevels] = {"Primary","Intermediate",...}

Then following suitable range checking you can just pick levels[level-1]
fin >> score;

while (score != -1)
{
judgeNumber ++;
fout << "Judge " << judgeNumber << " ";
fout << score << " ";
for (scoreType = 1; scoreType < numCategories; scoreType ++)

It seems likely your iteration bounds are off here. if numCatergories =
4, then this loop will execute 3 times. Is that what you want?
{
fin >> score;
fout << score << " ";
sum += score;
}
fout << "\n";
fin >> score;

}
return highScore;
}

void PrintHigh (ofstream& fout, double highScore);

Lose the semicolon.
{
fout << "\n\n\n";
fout << "Highest Score: ";
fout.width(5);
fout << highScore << endl;
return highScore;
} //end printhigh

It's nice to break things into functions but some of these very simple
functions seems like overkill. If a function is only called once, and
it only a few lines long, it's hard to see the merit in making it a
separate function.

Where did this brace come from?
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.

You're well within bounds asking for help on a problem you've attempted
on your own already. Look into some of these issues and see if that
helps; post again with your progress.

Mark
 
D

Daniel T.

'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.

Your code doesn't do much of anything on my box. I suspect you don't
really want "PrintHigh" *inside* the function "Process". Once I fix
that, the compiler complains because PrintHigh is returning a value when
it was declared 'void', so I fix that and notice where you put your
return in your process function... That is why your code doesn't go to
the second player.

Quite frankly, it looks like your problem is that you got confused as to
which '}' went with which '{' and I suspect that you got confused
because of the way you indented your code.

What is the assignment? It would help to know that.
 
J

Jim Langston

'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

I fixed 2 errors, one having to do with your PrintHigh function being
defined wrong, the other with sum not being initialized. I then get output,
but your input is in the wrong order. Comments inline.
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;

First you input numCategories. But, your first value in the file is: 6010
So now numCategories has 6010. I don't think this is what you wanted.
while (fin >> pianoPlayer)

pianoPlayer now has the next value in the input file, 1. I think you wanted
this to be the 6010. Did you not show us a value in the data file? Is
there a value that comes first before the pianoPlayer?
{
fin >> level;

Just like the other values being off, this now contains the next value in
the input file, 7.0 But I think you wanted this to be the 1. The rest of
the logic is off because of these. Either fix your input (what is
numCategories? I don't see that in the input file) or show us what the
input file is supposed to be. Next comment will be where the function was
defined wrong.
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;
}

You are missing a } here. Add a }
}
void PrintHigh (ofstream& fout, double highScore);

Get rid of the ; at the end of this function definition
{
fout << "\n\n\n";
fout << "Highest Score: ";
fout.width(5);
fout << highScore << endl;
return highScore;

PrintHigh is void, it doesn't return anything. change this to
return;
} //end printhigh

}

Get rid of this }, it was moved up before 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.

Fix the function PrintHigh and look at your input file and see if it works
then.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top