Hello, Below is my current code. It works as intended but I would like to prompt the user to input the files name and open it that way instead of it being declared in the code itself. I tried a few things myself but couldnt gete it to work. Any help would be appricated.    #include #include #include #include using namespace std; int main() {     //open file     ifstream file("StudentGrades.txt");     int number;     string x;     //declare array     int *array = new int;     int index = 0;     //declare variable to store number of student     int score24 = 0, score49 = 0, score74 = 0, score99 = 0, score124 = 0, score149 = 0, score174 = 0, score200 = 0;     //Ensure array is not already full     while (getline(file, x, ','))     {     //convert to int      number = stoi(x);      //store it in a array      array[index++] = number;   }   //check the every element and score based on score     for(int i=0;i=0 && array[i] <=24)       score24++;      else if(array[i]>=25 && array[i] <=49)       score49++;      else if(array[i]>=50 && array[i] <=74)       score74++;      else if(array[i]>=75 && array[i] <=99)       score99++;      else if(array[i]>=100 && array[i] <=124)       score124++;      else if(array[i]>=125 && array[i] <=149)       score149++;      else if(array[i]>=150 && array[i] <=174)       score174++;      else if(array[i]>=175 && array[i] <=200)       score200++;     }     //display score of the file     cout<<"\nStudent test score from the file is: "<

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter8: Arrays And Strings
Section: Chapter Questions
Problem 6PE
icon
Related questions
Question

Hello, Below is my current code. It works as intended but I would like to prompt the user to input the files name and open it that way instead of it being declared in the code itself. I tried a few things myself but couldnt gete it to work. Any help would be appricated. 

 

#include <fstream>
#include<iostream>
#include <ctype.h>
#include <string>
using namespace std;
int main()
{
    //open file
    ifstream file("StudentGrades.txt");
    int number;
    string x;
    //declare array
    int *array = new int;
    int index = 0;
    //declare variable to store number of student
    int score24 = 0, score49 = 0, score74 = 0, score99 = 0, score124 = 0, score149 = 0, score174 = 0, score200 = 0;
    //Ensure array is not already full
    while (getline(file, x, ','))
    {
    //convert to int
     number = stoi(x);
     //store it in a array
     array[index++] = number;

  }
  //check the every element and score based on score
    for(int i=0;i<index;i++)
    {
     if(array[i]>=0 && array[i] <=24)
      score24++;
     else if(array[i]>=25 && array[i] <=49)
      score49++;
     else if(array[i]>=50 && array[i] <=74)
      score74++;
     else if(array[i]>=75 && array[i] <=99)
      score99++;
     else if(array[i]>=100 && array[i] <=124)
      score124++;
     else if(array[i]>=125 && array[i] <=149)
      score149++;
     else if(array[i]>=150 && array[i] <=174)
      score174++;
     else if(array[i]>=175 && array[i] <=200)
      score200++;
    }
    //display score of the file
    cout<<"\nStudent test score from the file is: "<<endl;
    for(int i=0;i<index;i++)
     cout<<array[i]<<" ";
   
    cout<<endl;
    //display result
    cout<<"\nScore ranges \t Number of students"<<endl;
    cout<<"0-24 \t \t "<<score24<<endl;
    cout<<"25-49 \t \t "<<score49<<endl;
    cout<<"50-74 \t \t "<<score74<<endl;
    cout<<"75-99 \t \t "<<score99<<endl;
    cout<<"100-124 \t "<<score124<<endl;
    cout<<"125-149 \t "<<score149<<endl;
    cout<<"150-174 \t "<<score174<<endl;
    cout<<"175-200 \t "<<score200<<endl;

   cout<<endl;
   //close the file
   file.close();
}
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning