Writing/Reading from a file!

A friend and I are trying to keep our minds fresh over break by creating a program that rates girls, excuse image that throws...

I am at a road block finding a way to store the information of each girl, such as a string for their name and ints for their scores. I cannot find a way to reference each of them so I can make a comparative statement! My first reaction was to write the data to a file. The program below only stores one number.

Arrays were another idea I looked into. If I understand correctly, data cannot be stored in an array like that.

Thanks in advance!

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
 #include <iostream>
#include <fstream>

using namespace std;

double girlscore1();

int main()
{
    int numgirls;
    
    cout << "How many girls would you like to rate?" << endl;
    cin >> numgirls;
    
    for(int girl = 1; girl <= numgirls;)
    {
        
        ofstream outputFile;
        outputFile.open("/Users/connorbaniak/Desktop/girlscore.rtf");
        
        int cute, crazy, smart;
        double total;
        
        cout << "How cute is girl " << girl << " 1-100?" << endl;
        cin >> cute;
        cout << "How crazy is girl " << girl << " 1-100?" << endl;
        cin >> crazy;
        cout << "How smart is girl " << girl << " 1-100?" << endl;
        cin >> smart;
        
        total = (cute + crazy + smart)/3;

        outputFile << total << endl;

        cout << "Girl " << girl << " has a total of " << total << "." << endl;
        
        outputFile.close();
        
        girl++;
    
    }
    
    ifstream outputFile;
    int one, two;
    outputFile.open("/Users/connorbaniak/Desktop/girlscore.rtf");
    
    outputFile >> one;
    outputFile >> two;
    
    outputFile.close();
    
    cout << one << endl;
    cout << two << endl;
    
    
return 0;
}
Last edited on
Well you could try to use an array but you would need to for-loop through the array before/while you put that data into your file.

However, I would be hard-pressed to think that there is any more efficient way of storing a series of data-points and with no iteration to place that data into a file.

I hope I've help,

~Hirokachi
Last edited on
Why don't you use a struct for the girl data and store them in a vector? You could easily do all sort of operations like sorting, filtering and searching.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct Girl
{
  Girl(int cute, int smart, int crazy)
  {
    this->crazy = crazy;
    this->smart = smart;
    this->cute = cute;
  }

  double getRating()
  {
    return double(cute + crazy + smart) / 3.0;
  }

  int cute;
  int smart;
  int crazy;
};
Thanks for the suggestions, from both of you. Thomas1965, I do not believe we went over structures in class yet (maybe we did). I will review my textbook and research into both suggestions. I may private message you moving forward. :)
Last edited on
Topic archived. No new replies allowed.