Error with Infile.
Sep 12, 2019 at 2:45am UTC
So when reading the Grades.txt file I have the first number is 4. When the program runs it reads the file fine, but requires me to hit "Enter" 4 times before It displays the rest of the grades and the average. If I change that number to 12 for example, it then requires me to hit "Enter" 14 times before giving me the rest of the grades and the average. The first number of the file is just supposed to tell the program how many grades are in the file. How do I change this so once the input file is read it just does the calculations?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
//Driver.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "Scores.h"
using namespace std;
int main() {
// Define local variables
int score, num;
string inputFile;
// uses inData for input file stream
ifstream inData;
cout << "Enter the input file :" ;
// getline is used for if user enters a space for a file name
getline(cin, inputFile);
inData.open(inputFile.c_str());
// checks to see if the file is valid
if (inData.fail()) {
cout << "File Not Found" ;
return 1;
}
inData >> num;
if (num < 0) {
cout << "Invalid Score Entered: " << num<<endl;
return 0;
}
Scores scoreslist;
for (int i = 0; i < num; i++) {
inData >> score;
bool value = scoreslist.addScore(score);
if (!value)
cout << "Invalid score entered :" << num << endl;
}
cout<<setprecision(2)<<fixed;
scoreslist.sortScore();
cout << "Displaying Scores After Sorting :" << endl;
scoreslist.displayScores();
cout << "Average Score :" << scoreslist.averageScores() << endl;
// system("pause");
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
//Scores.cpp
#include "Scores.h"
Scores::Scores(int val) {
size = 0;
for (int i = 0; i < 100; i++) {
scoresList[i] = val;
}
}
Scores::Scores() {
size = 0;
for (int i = 0; i < 100; i++) {
scoresList[i] = 0;
}
}
bool Scores::addScore(int val) {
if (val < 0) {
return false ;
} else {
scoresList[size] = val;
size++;
return true ;
}
}
void Scores::sortScore() {
// This Logic will Sort the Array of elements in Ascending order
int temp;
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (scoresList[i] > scoresList[j]) {
temp = scoresList[i];
scoresList[i] = scoresList[j];
scoresList[j] = temp;
}
}
}
}
double Scores::averageScores() {
double total = 0;
for (int i = 0; i < size; i++) {
total = total + scoresList[i];
}
return ((double )total) / size;
}
void Scores::displayScores() {
for (int i = 0; i < size; i++) {
cout << scoresList[i] << endl;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
//Scores.h
#pragma once
#include<iostream>
using namespace std;
class Scores {
private :
int scoresList[100];
int size;
public :
Scores();
Scores(int list);
bool addScore(int val);
void sortScore();
double averageScores();
void displayScores();
};
Sep 12, 2019 at 4:27am UTC
First of all, indent your code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
//Driver.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
class Scores {
private :
int scoresList[100];
int size;
public :
Scores();
Scores(int list);
bool addScore(int val);
void sortScore();
double averageScores();
void displayScores();
};
Scores::Scores(int val)
{
size = 0;
for (int i = 0; i < 100; i++) {
scoresList[i] = val;
}
}
Scores::Scores()
{
size = 0;
for (int i = 0; i < 100; i++) {
scoresList[i] = 0;
}
}
bool Scores::addScore(int val)
{
if (val < 0) {
return false ;
} else {
scoresList[size] = val;
size++;
return true ;
}
}
void Scores::sortScore()
{
// This Logic will Sort the Array of elements in Ascending order
int temp;
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (scoresList[i] > scoresList[j]) {
temp = scoresList[i];
scoresList[i] = scoresList[j];
scoresList[j] = temp;
}
}
}
}
double Scores::averageScores()
{
double total = 0;
for (int i = 0; i < size; i++) {
total = total + scoresList[i];
}
return ((double ) total) / size;
}
void Scores::displayScores()
{
for (int i = 0; i < size; i++) {
cout << scoresList[i] << endl;
}
}
int main()
{
// Define local variables
int score, num;
string inputFile;
// uses inData for input file stream
ifstream inData;
cout << "Enter the input file :" ;
// getline is used for if user enters a space for a file name
getline(cin, inputFile);
inData.open(inputFile.c_str());
// checks to see if the file is valid
if (inData.fail()) {
cout << "File Not Found" ;
return 1;
}
inData >> num;
if (num < 0) {
cout << "Invalid Score Entered: " << num << endl;
return 0;
}
Scores scoreslist;
for (int i = 0; i < num; i++) {
inData >> score;
bool value = scoreslist.addScore(score);
if (!value)
cout << "Invalid score entered :" << num << endl;
}
cout << setprecision(2) << fixed;
scoreslist.sortScore();
cout << "Displaying Scores After Sorting :" << endl;
scoreslist.displayScores();
cout << "Average Score :" << scoreslist.averageScores() << endl;
return 0;
}
The wording and the variables in your error messages need a bit of work as well.
Plus, it works here without any extra newline pressing required.
$ cat foo.txt
4
22
44
33
11
$ g++ -std=c++11 foo.cpp
$ ./a.out
Enter the input file :foo.txt
Displaying Scores After Sorting :
11
22
33
44
Average Score :27.50
Sep 12, 2019 at 7:06pm UTC
Here is updated main and it works great. The only thing is I need to add error checking so the program stops if it reads in a negative number or a letter input. Right now if it reads either of those in it just ignores it and continues with the calculations normally. In my in line 103 to 107 would I just add a error display message and a system pause/ return 0?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "Scores.h"
using namespace std;
int main()
{
// Define local variables
int score, num;
string inputFile;
// uses inData for input file stream
ifstream inData;
cout << "Enter the input file :" ;
// getline is used for if user enters a space for a file name
getline(cin, inputFile);
inData.open(inputFile.c_str());
// checks to see if the file is valid
if (inData.fail())
{
cout << "File Not Found \n" ;
cout << "Enter correct input file :" ;
getline(cin, inputFile);
inData.open(inputFile.c_str());
}
Scores scoreslist;
inData >> num;
for (int i = 0; i < num; i++)
{
inData >> score;
bool value = scoreslist.addScore(score);
if (!value)
cout << "Invalid score entered: " << score << endl;
}
cout << setprecision(2) << fixed;
scoreslist.sortScore();
cout << "Displaying Scores After Sorting Correct Scores:" << endl;
scoreslist.displayScores();
cout << "Average Score :" << scoreslist.averageScores() << endl;
system("pause" );
return 0;
}
Last edited on Sep 12, 2019 at 7:07pm UTC
Topic archived. No new replies allowed.