HW Code Help

a. Create a txt file named “x” that contains students’ test scores in the range 0~100. Assume
test scores are integers. You can use the following sample data:
(76 84 75 62 58 75 93 54 36 87 81 88 68 90 73 80 74 84 96 100 61 50 84 47 92 87 76 87 82
64)
b. Write a program that reads the scores from file “x”, find the total number of scores, and determine the number of scores in each of the following ranges:
1) 0‐50 (0 <= score < 50)
2) 50‐55 (50 <= score < 55)
3) 55‐60
4) 60‐65
5) 65‐70
6) 70‐75
7) 75‐80
8) 80‐85
9) 85‐90
10) 90‐100 (90 <= score <=100 Notice: 100 is included in this range)
Output the score ranges and the number of students in each range. Your program should be able to read
any number of test scores.



Here is what I have so far:




1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
  ofstream myfile;
  myfile.open ("x");
  myfile << "C:\\Users\\Me\\Desktop\\x.txt";
  myfile.close();
  system("pause");
  return 0;
}



I honestly have NO idea how to go about doing this next part.
Last edited on
Actually I think this is a correction:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
  ofstream myfile;
  myfile.open ("x");
  myfile << "C:\\Users\\Me\\Desktop\\x.txt";
  system("pause");
  return 0;
}


since I don't want to close the file yet
The 'o' in 'ofstream' means it's an output file. This means your program is creating a file and will be writing to it.

1
2
3
myfile.open ("x");  // <- opens the file named "x" for writing

myfile << "C:\\Users\\Me\\Desktop\\x.txt";  // <- writes the path of the file to the file 


So you're basically creating a text file and then writing the name of the file within the file.


This isn't really what you want to do.


The instructions suggest to me that you should manually create the file (ie: use notepad). The program you write should not be trying to modify that file, but instead should just be opening it to read the data from it. (Use an ifstream instead of an ofstream)
Yes, that's what I was intending to do with that code. I already have the text file.

Here's the updated code:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
  ifstream myfile;
  myfile.open ("hwdata2.txt");
  myfile << "C:\\Users\\Me\\Desktop\\hwdata2.txt";  
  system("pause");
  return 0;
}


Note that this is just what I know what's needed in the code to start it off, but now i'm having trouble with step b and i'm running out of time to work on this.
Any help is appreciated :)
Last edited on
I'm at a loss here. If anybody can help me. :/
Could somebody first of all just help me in making the program read the scores, and find the total number of scores?
1
2
3
4
5
6
std::ifstream input("x");
int count=0, score;
while( input>>score ){
   //...
   ++count;
}
So would it be something like:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
  ifstream myfile;
  myfile.open ("hwdata2.txt");
  myfile >> "C:\\Users\\Mike\\Desktop\\hwdata2.txt";  
  
std::ifstream input("hwdata2.txt");
int count=0, score;
while( input>>score ){
   //...
   ++count;
}
  system("pause");
  return 0;
}



I've run out of time!

I'm begging somebody to please finish this code for me.
What is this:

myfile >> "C:\\Users\\Mike\\Desktop\\hwdata2.txt";

supposed to be doing? Your compiler should be giving you all sorts of errors about that.

Why are you creating two different ifstream objects based on the same file?

I suggest you take a look at:

http://www.cplusplus.com/doc/tutorial/files/
Topic archived. No new replies allowed.