File Input & Output: List Error

This program reads a few lines from a list located in a text file. Each line contains data about a person and his or her score. The program should then output the person with the highest score and his or her score. But for some reason, the program seemingly only takes the first three lines into consideration. Can someone tell me why?

(Sorry for the code being so long, I don't know how I would shorten it down. And shortening it down probably wouldn't be a good idea yet, because I haven't got it working yet.)

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <locale>
#include <cmath>
using namespace std;

void creatingName(vector<string> *rows){
    for(int x=0;x<rows->size();x++){
        for(int y=0;y<(*rows)[x].size();y++){
            if((*rows)[x][y]==':'){
                (*rows)[x].resize(y);
                break;
            }
        }
    }
}

void isSorted(bool *sorted, vector<int> *assembledNumber){
    for(int x=0;x<assembledNumber->size()-1;x++){
        if((*assembledNumber)[x]<(*assembledNumber)[x+1])
            *sorted = false;
        else
            *sorted = true;
    }
}

void creatingNumber(vector<int> *number, vector<int> *assembledNumber){
    int n = number->size(), sum = 0;
    for(int x=0;x<n;x++){
        if(n == 0)
            sum += (*number)[x];
        else
            sum += (*number)[x]*pow(10, n-1);
        n--;
    }
    cout << sum << endl;
    assembledNumber->push_back(sum);
}

void checkIfInputIsCorrect(bool *input, vector<string> *rows){
    string line;
    fstream file;
    file.open("exam.txt");
    while(getline(file, line)){
        if (line == "")
            break;
        rows->push_back(line);
    }
    *input = true;
    for(int x=0;x<rows->size();x++){
        int signs1 = 0, signs2 = 0;
        for(int y=0;y<(*rows)[x].size();y++){
            if((*rows)[x][y]==':')
                signs1++;
            else if((*rows)[x][y]>='0' && (*rows)[x][y]<= '9')
                signs2++;
        }
        if(signs1 == 0 || signs2 == 0)
            *input = false;
    }
}

int main(){
    bool inputCorrect, sorted = false;
    vector<string> rows;
    vector<int> number, assembledNumber;
    fstream file;
    file.open("exam.txt");
    int replacingNumber;
    string replacingString;
    checkIfInputIsCorrect(&inputCorrect, &rows);
    if(rows.empty()){
        cout << "No data has been input!" << endl;
    }else{
        if(inputCorrect){
            for(int x=0;x<rows.size();x++){
                int n = rows[x].size();
                for(int y=0;y<n;y++){
                    char c = rows[x][y];
                    if(isdigit(c)){
                        int a = c-48;
                        number.push_back(a);
                    }
                }
                creatingNumber(&number, &assembledNumber);
                number.clear();
            }
            while(!sorted){
                isSorted(&sorted,&assembledNumber);
                for(int x=0;x<assembledNumber.size()-1;x++){
                    if(assembledNumber[x]<assembledNumber[x+1]){
                        replacingNumber = assembledNumber[x+1];
                        assembledNumber[x+1] = assembledNumber[x];
                        assembledNumber[x] = replacingNumber;
                        replacingString = rows[x+1];
                        rows[x+1] = rows[x];
                        rows[x] = replacingString;
                    }
                }
            }
            creatingName(&rows);
            cout << "\n" << rows[0] << " had the highest score with " << assembledNumber[0] << "!" << endl;
        }else
            cout << "Your input was incorrect!\nTry again!" << endl;
    }
}
Last edited on
Line 47-48 might be your problem, do you have blank lines in your text file?

You might try while(!InputFile.eof())
closed account (28poGNh0)
Can you give us the contents of your file.txt
You might try while(!InputFile.eof())

That's not a good idea at all. In fact I'd recommend avoiding the use of eof() .
closed account (28poGNh0)
Please ,can you tell us why ?
Thanks for answering, I was afraid that no one would! :) Here are the contents of my text file:

Johan Johansson: 10
Robert Plant: 20
Randomname Random: 30
Bla Blabla: 40
Hello There: 50

As you can see, I don't have any blank lines in there. And about line 47 to 48 maybe being the problem, I have tested outputting the data of the "rows" vector, and there doesn't seem to be a problem, at least with storing the data.

The program says that Randomname Random has the highest score with 30 points...
Last edited on
BTW. Do any of you know how to clear a text file?
closed account (28poGNh0)
This line ofstream file("exam.txt"); will clear the text file for you

about you program above is so complexe I'am still working on it
Last edited on
Thanks!

I'm sorry! I am sure that it is possible to make the code shorter, it's just that I'm not very experienced with coding.
Last edited on
closed account (28poGNh0)
No It is not about short or tall, It is about complexity You have strong relationship with it.

You should know that your program gave a headack ...whatever !

Your mistake is in isSorted function because it checks four time the condition while you need to increase checking from 4 to 3 to 2.. so that's why you need(OMG) another parameter I called It sadNumber
this is your code little bit changed if you want we can simplify it

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# include <iostream>
# include <vector>
# include <fstream>
# include <cmath>
using namespace std;

void creatingName(vector<string> *rows){
    for(size_t x=0;x<rows->size();x++)
    {
        // cout << (*rows)[x] << endl;
        for(size_t y=0;y<(*rows)[x].size();y++){
            if((*rows)[x][y]==':'){
                (*rows)[x].resize(y);
                break;
            }
        }
    }
}

void isSorted(bool *sorted, vector<int> *assembledNumber,int number){
    for(size_t x=0;x<assembledNumber->size()-number;x++)
    {
        if((*assembledNumber)[x]<(*assembledNumber)[x+1])
            *sorted = false;
        else
            *sorted = true;
    }
}

void creatingNumber(vector<int> *number, vector<int> *assembledNumber)
{
    int n = number->size(), sum = 0;
    for(int x=0;x<n;x++){
        if(n == 0)
            sum += (*number)[x];
        else
            sum += (*number)[x]*pow(10, n-1);
        n--;
    }
    cout << sum << endl;
    assembledNumber->push_back(sum);
}

void checkIfInputIsCorrect(bool *input, vector<string> *rows)
{
    string line;
    ifstream file;
    file.open("exam.txt");
    while(getline(file, line)){
        if (line == "")
            break;
        rows->push_back(line);
    }
    *input = true;

    for(size_t x=0;x<rows->size();x++)
    {
        int signs1 = 0, signs2 = 0;
        for(size_t y=0;y<(*rows)[x].size();y++)
        {
            if((*rows)[x][y]==':')
                signs1++;
            else if((*rows)[x][y]>='0' && (*rows)[x][y]<= '9')
                signs2++;
        }
        if(signs1 == 0 || signs2 == 0)
            *input = false;
    }
    file.close();
}

int main()
{
    bool inputCorrect, sorted = false;
    vector<string> rows;
    vector<int> number, assembledNumber;

    /*fstream file("exam.txt");
    file.open("exam.txt");*/// No need to declare those here

    int replacingNumber,sadNumbuer = 1;
    string replacingString;
    checkIfInputIsCorrect(&inputCorrect, &rows);
    if(rows.empty())
    {
        cout << "No data has been input!" << endl;
    }else
    {
        if(inputCorrect)
        {
            for(size_t x=0;x<rows.size();x++)
            {
                int n = rows[x].size();
                for(int y=0;y<n;y++)
                {
                    char c = rows[x][y];
                    if(isdigit(c))
                    {
                        int a = c-48;
                        number.push_back(a);
                    }
                }
                creatingNumber(&number, &assembledNumber);
                number.clear();
            }

            while(!sorted)
            {
                isSorted(&sorted,&assembledNumber,sadNumbuer);
                for(size_t x=0;x<assembledNumber.size()-1;x++)
                {
                    if(assembledNumber[x]<assembledNumber[x+1])
                    {
                        replacingNumber = assembledNumber[x+1];
                        assembledNumber[x+1] = assembledNumber[x];
                        assembledNumber[x] = replacingNumber;
                        replacingString = rows[x+1];
                        rows[x+1] = rows[x];
                        rows[x] = replacingString;
                    }
                }if(sadNumbuer<4)sadNumbuer++;
            }
            creatingName(&rows);
            cout << "\n" << rows[0] << " had the highest score with " << assembledNumber[0] << "!" << endl;
        }else
            cout << "Your input was incorrect!\nTry again!" << endl;
    }
}


hope that helps
Thanks for the help with finding the issue! :)

No It is not about short or tall, It is about complexity You have strong relationship with it.

Is that a good or a bad thing? :P
closed account (28poGNh0)
It's both I think! and I am not kidding.

good because It gives you some kind of strength to go further(for exp I spent so much time to understand the code of yours) and bad because it kills your time(to do such a code you need more time .In the future you may have aproblem( the most of programers have it) it's when you want to understand what did you did ,and what was your logic you'll find out how much it's important to simplify your codes.

all the best
Topic archived. No new replies allowed.