Question

I have 2 issues with the C++ code below. The biggest concern is that the wrong...

I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it?

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

int readTestScores (ifstream &infile, int testScores[]);
void findRanges (int [], int, int[]);
void printResults (int[]);

const int maxStudents = 50;
const int numberOfRanges = 8;
const int rangeSize = 25;

int main()
{
ifstream infile;
string filename;
int count;

int testScores[maxStudents];
int range[numberOfRanges];

cout << "Enter the file name: ";
cin >> filename;
cout << "You entered the file name: " << filename << endl;
cout << endl << endl;

infile.open("testScores.txt");

if (!infile)
{
  cout << filename << "file cannot be opened!!" << endl;
  return 1;
}

count = readTestScores(infile, testScores);

findRanges(testScores, count, range);

printResults(range);

infile.close();

system("pause");

return(0);
}

int readTestScores(ifstream &infile, int testScores[])
{
int score;
int count = 0;

infile >> score;

while (infile && count < maxStudents)
{
  testScores[count] = score;
  
  count++;
  
  infile >> score;
  
}

return count;
}

void findRanges(int testScores[], int count, int range[])
{
int score;

for (int i = 0 ; i < numberOfRanges; i++)
{
  range[i] = 0;
}

for (int i = 0; i < count; i++)
{
  score = testScores[i];
  
  if (score > 0 && score <= 24)
   range[0]++;
  else if (score >= 25 && score <= 49)
   range[1]++;
  else if (score >= 50 && score <= 74)
   range[2]++;
  else if (score >= 75 && score <= 99)
   range[3]++;
  else if (score >= 100 && score <= 124)
   range[4]++;
  else if (score >= 125 && score <= 149)
   range[5]++;
  else if (score >= 150 && score <= 174)
   range[6]++;
  else if (score >= 175 && score <= 200)
   range[7]++;
  else
  {
   cout << endl << "All test scores should be in the 0 to 200 range." << endl;
   exit(1);
  }

}
}

void printResults(int range[])
{
int rangeStart = 0;
int rangeEnd = 24;

cout << "Scores       \tNumber_of_Students" << endl;
for (int i = 0; i < numberOfRanges; i++)
{
  cout << rangeStart << "-" << rangeEnd
    << "\t\t\t" << range[i] << endl;
   
    rangeStart += rangeSize;
    rangeEnd += rangeSize;
   
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello, here is the completed code you wanted. Every important statement is explained using comments. Please check and let me know if you have any doubts. Thanks.

modifications in code:

Opening file (line 35) in screen shot

Function printResults (line 144 - 146)

modifications are mentioned in code as comment

CODE

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

//function prototypes
int readTestScores (ifstream &infile, int testScores[]);
void findRanges (int [], int, int[]);
void printResults (int[]);

//global variables
const int maxStudents = 50;
const int numberOfRanges = 8;
const int rangeSize = 25;

int main()
{
   //variables
   ifstream infile;
   string filename;
   int count;
   int testScores[maxStudents];
   int range[numberOfRanges];

   //prompt for file name
   cout << "Enter the file name (with extension): ";
   cin >> filename;
  
   //printing file name
   cout << "You entered the file name: " << filename << endl;
   cout << endl << endl;
  
   //opening file (code modified)
   infile.open(filename);
  
   //if file not exists
   if (!infile)
   {
       //prints file can't be opened
       cout << filename << "file cannot be opened!!" << endl;
       return 1;
   }
  
   //calling readTestScores
   count = readTestScores(infile, testScores);
  
   //calling findRanges
   findRanges(testScores, count, range);
  
   //calling printResults
   printResults(range);
  
   //closing file
   infile.close();
  
   system("pause");
   return(0);
}

int readTestScores(ifstream &infile, int testScores[])
{
   int score;
   int count = 0;
  
   //reading score from file
   infile >> score;
  
   while (infile && count < maxStudents)
   {
       //storing score into array
       testScores[count] = score;
      
       //incrementing count
       count++;
      
       //reading score from file
       infile >> score;
   }
   //returns count
   return count;
}

void findRanges(int testScores[], int count, int range[])
{
   int score;
  
   //initializing elements in range array as 0
   for (int i = 0 ; i < numberOfRanges; i++)
   {
       range[i] = 0;
   }
  
   //counting ranges
   for (int i = 0; i < count; i++)
   {
       //score from testScores array
       score = testScores[i];
  
       //score between 1 - 24
       if (score > 0 && score <= 24)
           range[0]++;
       //score between 25 - 49
       else if (score >= 25 && score <= 49)
           range[1]++;
       //score between 50 -74
       else if (score >= 50 && score <= 74)
           range[2]++;
       //score between 75 - 99
       else if (score >= 75 && score <= 99)
           range[3]++;
       //score between 100 - 124
       else if (score >= 100 && score <= 124)
           range[4]++;
       //score between 125 - 149
       else if (score >= 125 && score <= 149)
           range[5]++;
       //score between 150 - 174
       else if (score >= 150 && score <= 174)
           range[6]++;
       //score between 175 - 200
       else if (score >= 175 && score <= 200)
           range[7]++;
       //score is out of range
       else
       {
           cout << endl << "All test scores should be in the 0 to 200 range." << endl;
           exit(1);
       }
   }
}

//function modified
void printResults(int range[])
{
   int rangeStart = 0;
   int rangeEnd = 24;
  
   //printing outputs
   cout << "Scores       \tNumber_of_Students" << endl;
   for (int i = 0; i < numberOfRanges; i++)
   {
       //if i is last index (code modified)
       if(i == numberOfRanges - 1)
           rangeEnd++;
      
       //printing counts of range
       cout << rangeStart << "-" << rangeEnd << "\t\t\t" << range[i] << endl;
      
       //incrementing rangeStart and rangeEnd by rangeSize
       rangeStart += rangeSize;
       rangeEnd += rangeSize;
   }
}

OUTPUT

CODE SCREEN SHOT

Add a comment
Know the answer?
Add Answer to:
I have 2 issues with the C++ code below. The biggest concern is that the wrong...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

  • I need to update this C++ code according to these instructions. The team name should be...

    I need to update this C++ code according to these instructions. The team name should be "Scooterbacks". I appreciate any help! Here is my code: #include <iostream> #include <string> #include <fstream> using namespace std; void menu(); int loadFile(string file,string names[],int jNo[],string pos[],int scores[]); string lowestScorer(string names[],int scores[],int size); string highestScorer(string names[],int scores[],int size); void searchByName(string names[],int jNo[],string pos[],int scores[],int size); int totalPoints(int scores[],int size); void sortByName(string names[],int jNo[],string pos[],int scores[],int size); void displayToScreen(string names[],int jNo[],string pos[],int scores[],int size); void writeToFile(string...

  • Hi there! I need to fix the errors that this code is giving me and also...

    Hi there! I need to fix the errors that this code is giving me and also I neet to make it look better. thank you! #include <iostream> #include <windows.h> #include <ctime> #include <cstdio> #include <fstream> // file stream #include <string> #include <cstring> using namespace std; const int N=10000; int *freq =new int [N]; int *duration=new int [N]; char const*filename="filename.txt"; int songLength(140); char MENU(); // SHOWS USER CHOICE void execute(const char command); void About(); void Save( int freq[],int duration[],int songLength); void...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • Can some help me with my code I'm not sure why its not working. Thanks In...

    Can some help me with my code I'm not sure why its not working. Thanks In this exercise, you are to modify the Classify Numbers programming example in this chapter. As written, the program inputs the data from the standard input device (keyboard) and outputs the results on the standard output device (screen). The program can process only 20 numbers. Rewrite the program to incorporate the following requirements: a. Data to the program is input from a file of an...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

  • I need to add something to this C++ program.Additionally I want it to remove 10 words...

    I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words);    int main() { // Declaring variables std::map<std::string,int> words;       //defines an input stream for the data file ifstream dataIn;    string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...

  • Am I getting this error because i declared 'n' as an int, and then asking it...

    Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() {    int n;    double avg, sum = 0;;    string in_file, out_file;    cout << "Please enter the name of the input file: ";    cin >> in_file;   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT