Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8164329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:17:37+00:00 2026-06-06T19:17:37+00:00

I have a homework problem here I’ve been working on; here is the description:

  • 0

I have a homework problem here I’ve been working on; here is the description:

Write a program that reads a file consisting of students’ test scores in the
range 0–200. It should then determine the number of students having
scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99,
100–124, 125–149, 150–174, and 175–200. Output the score ranges
and the number of students. (Run your program with the following input
data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200,
175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189.)

The following is the program I have made:

#include <iostream>
#include <fstream>
using namespace std;

int main() 
{
//declaring variables
ifstream myfile("data.txt");
double grades[26];
int i, studentNumber=0;
int score0_24=0, score25_49=0, score50_74=0,
    score75_99=0, score100_124=0, score125_149=0,
    score150_174=0, score175_200=0;

cout << score150_174 << endl;

//initializing grades array
for(i=0; i<26; i++)
{
    grades[i] = 0;
    cout << grades[i] << " ";
}

//getting data from text file
    for(i=0; i<26; i++)
        {
            myfile >> grades[i];
            cin.ignore(2);
            studentNumber++;
        }

    myfile.close();

//finding number of people for each score range
for(i=0; i<26; i++)
{
    if(grades[i] <= 24)
        score0_24++;
    if(grades[i] >= 25 && grades[i] <= 49)
        score25_49++;
    if(grades[i] >= 50 && grades[i] <= 74)
        score50_74++;
    if(grades[i] >= 75 && grades[i] <= 99)
        score75_99++;
    if(grades[i] >= 100 && grades[i] <= 124)
        score100_124++;
    if(grades[i] >= 125 && grades[i] <= 149)
        score125_149++;
    if(grades[i] >= 150 && grades[i] <= 174)
        score150_174++;
    if(grades[i] >= 175 && grades[i] <= 200)
        score175_200++;
}

//outputing results
cout << "Number of students: " << studentNumber << endl;
cout << "0-24: " << score0_24 << endl;
cout << "25-49: " << score25_49 << endl;
cout << "50-74: " << score50_74 << endl;
cout << "75-99: " << score75_99 << endl;
cout << "100-124: " << score100_124 << endl;
cout << "125-149: " << score125_149 << endl;
cout << "150-174: " << score150_174 << endl;
cout << "175-200: " << score175_200 << endl;

return 0;
}

And the file I have, which is in the same folder as this project, is called:

data.txt

And contains the following:

76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189

When I run the program, it suspends during the for loop where I read data from the file. I’ve tried doing some cout statements right before and after, and it seems that is the problem.

Help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:17:38+00:00Added an answer on June 6, 2026 at 7:17 pm

    I have a few recommendations for you, though you didn’t ask :)…

    1) Instead of collecting the grades first and then processing them, combine your loops into 1 so as soon as you read the value you do something with it. This removes the need to have your grades array and about half your code.

    2) Instead of hard-coding 26 grades, read from the file until you’re at the end:

    for(int grade = 0; myFile >> grade; )
    {
        if(myFile.peek() == ',')
             myFile.ignore(); // <--- this is why your version wasn't working (you were calling cin.ignore instead)
    
        // do something with grade here
    }
    

    3) Instead of creating 8 counters for 0-24, 25-49, etc, create an array of 8 and use integer math to figure out what index to access.

    With those changes you should be able to get this down to 15-20 lines of clean readable code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a homework problem that I really don't know how to start. Here
I have a homework problem where I have to make an inheritance program that
I have done a homework assignment, here is the problem statement: Your program should
This is a homework problem that I have. I have been doing some research
I have a question about my homework problem. Here is the problem: Write a
I have a homework question that says: Problem 1: Given the array [ 22
This is my homework, but please read my problem description first. I have to
I have a program that solves the weighted interval scheduling problem using dynamic programming
For homework we have been given the bathroom synchronization problem. I have been struggling
Hey all, got one mountain of a problem here. I have completed a program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.