I wrote the following program toread in a data file with golf scores for 9 holes
ID: 3617954 • Letter: I
Question
I wrote the following program toread in a data file with golf scores for 9 holes in one column, andthe par for each hole in the second column. My problem is that,when it displays the results, it calls every hole "Hole 10",instead of "Hole 1", "Hole 2", etc.*Note: to actually run program you need to save the following as adata file named "scorecard" in notepad:
2 4
3 4
3 5
5 3
5 4
2 4
3 4
1 3
4 5
------------------------------------------------------------------------------------
#include <iostream> #include <fstream> #include <cstdlib> #include <cmath> using namespace std; int compare(int, int); int main () { int k,under_over, score, par; ifstreamgolfround; golfround.open("scorecard.dat"); if(!golfround) { cout<<"Unable to open file"; system("pause"); exit (0); } else { cout<<"Successfully read data:"<<" "<<endl; while(golfround>>score>>par) { for (k=1; k<=9; k++) under_over=compare(score,par); if (under_over < 0) cout<<" Hole"<<k<<": "<<fabs(under_over)<<" strokesunder par"<<endl; if (under_over > 0) cout<<" Hole"<<k<<": "<<under_over<<" strokes overpar"<<endl; } } system("pause"); return 0; } /*----------------------------------------------------------------------------*/ int compare(int myscore, intparforcourse) { intdifference; difference=myscore-parforcourse; returndifference; } I wrote the following program toread in a data file with golf scores for 9 holes in one column, andthe par for each hole in the second column. My problem is that,when it displays the results, it calls every hole "Hole 10",instead of "Hole 1", "Hole 2", etc.
*Note: to actually run program you need to save the following as adata file named "scorecard" in notepad:
2 4
3 4
3 5
5 3
5 4
2 4
3 4
1 3
4 5
------------------------------------------------------------------------------------
#include <iostream> #include <fstream> #include <cstdlib> #include <cmath> using namespace std; int compare(int, int); int main () { int k,under_over, score, par; ifstreamgolfround; golfround.open("scorecard.dat"); if(!golfround) { cout<<"Unable to open file"; system("pause"); exit (0); } else { cout<<"Successfully read data:"<<" "<<endl; while(golfround>>score>>par) { for (k=1; k<=9; k++) under_over=compare(score,par); if (under_over < 0) cout<<" Hole"<<k<<": "<<fabs(under_over)<<" strokesunder par"<<endl; if (under_over > 0) cout<<" Hole"<<k<<": "<<under_over<<" strokes overpar"<<endl; } } system("pause"); return 0; } /*----------------------------------------------------------------------------*/ int compare(int myscore, intparforcourse) { intdifference; difference=myscore-parforcourse; returndifference; } #include <iostream> #include <fstream> #include <cstdlib> #include <cmath> using namespace std; int compare(int, int); int main () { int k,under_over, score, par; ifstreamgolfround; golfround.open("scorecard.dat"); if(!golfround) { cout<<"Unable to open file"; system("pause"); exit (0); } else { cout<<"Successfully read data:"<<" "<<endl; while(golfround>>score>>par) { for (k=1; k<=9; k++) under_over=compare(score,par); if (under_over < 0) cout<<" Hole"<<k<<": "<<fabs(under_over)<<" strokesunder par"<<endl; if (under_over > 0) cout<<" Hole"<<k<<": "<<under_over<<" strokes overpar"<<endl; } } system("pause"); return 0; } /*----------------------------------------------------------------------------*/ int compare(int myscore, intparforcourse) { intdifference; difference=myscore-parforcourse; returndifference; }
Explanation / Answer
please rate - thanks I've highlighted the area I changed--I added an equal to par #include #include #include #include using namespace std; int compare(int, int); int main () { int k, under_over, score, par; ifstream golfround; golfround.open("scorecard.dat"); if (!golfround) { coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.