Program always stops working after the first set of inputs

Hello, everyone! I am supposed to write codes for a little program that records names of students and their test scores. If I typed the same name twice, the program would prompt an error. If I type in "NO MORE", then the program stops recording and displays all the information. Here is the code:

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
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <complex>
#include <ctime>

using namespace std;

int main()
{
    cout << "Please type a person's name and his/her score." << endl;
    string name;
    int score;
    int x = 0;
    vector<string>names;
    vector<int>scores;

    while(cin >> name >> score)
    {
        if (name == "NO MORE")
        break;

        for(int i = 0; i <=x; i++)
        {
            if(name == names[i])
            {
                cout << "Sorry. The name: " << name << " is entered twice." << endl;
                cout << "Please type a person's name and his/her score." << endl;
            }
            else{
            names.push_back(name);
        scores.push_back(score);
        cout << "Please type a person's name and his/her score." << endl;
        x++;}

        }

    }

    cout << "Program terminated by user." << endl;
    for(int y = 0; y <= x; y++)
    {
        cout << names[y] << " --- " << scores[y] << endl;
    }


}

// The program always stops working after I entered the first set of inputs like "Joe 87". I am using Code::Blocks with the GNU GCC Compiler. 


Could someone help me investigate what is wrong with my code? Thank you very much!

The last statement in this code snip is already invalid

1
2
3
        for(int i = 0; i <=x; i++)
        {
            if(name == names[i])

because the value names[0] is undefined. The vector has no yet an element.
Thank you vlad! I revised my code, but there is a new problem coming up: I typed a name only for once, but it will prompt me saying that I already entered the name twice. Could you help me investigate what is going wrong with the logic of my code? Thank you very much!

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
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <complex>
#include <ctime>

using namespace std;

int main()
{
    cout << "Please type a person's name and his/her score." << endl;
    string name;
    int score;
    int x = 1;
    vector<string>names;
    vector<int>scores;
    cin >> name >> score;
    names.push_back(name);
    scores.push_back(score);

    while(cin >> name >> score)
    {
        if (name == "NO MORE")
        break;

        for(int i = 0; i < x; i++)
        {
            if(name != names[i])
            {
                names.push_back(name);
                scores.push_back(score);
                x++;

            }
            else if(name == names[i]){

           cout << "Sorry. The name: " << name << " is entered twice." << endl;
           cout << "Please type a person's name and his/her score." << endl;
            }
        }

        }



    cout << "Program terminated by user." << endl;

    for(int y = 0; y < x; y++)
    {
        cout << names[y] << " --- " << scores[y] << endl;
    }


}
I solved the problem on my own :)
The problem is that for the code
1
2
3
4
5
6
7
 if(name != names[i])
            {
                names.push_back(name);
                scores.push_back(score);
                x++;

            }

, whenever the "name" variable is not equal to one particular element of the "names" vector, the if statement will add the name to the end of the vector for multiple times except for the time when name equals to one particular element of the vector. So what I need to do the to get the push_back commands out of the for loop so no repetitive tasks will be done, since the names only need to be added ONCE.

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
 #include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <complex>
#include <ctime>

using namespace std;

int main()
{
    cout << "Please type a person's name and his/her score." << endl;
    string name;
    int score;
    int x = 1;
    vector<string>names;
    vector<int>scores;
    cin >> name >> score;
    names.push_back(name);
    scores.push_back(score);

    while(cin >> name >> score)
    {
        if (name == "NO MORE")
        break;

        for(int i = 0; i < x; i++)
        {
            if(name == names[i]){

           cout << "Sorry. The name: " << name << " is entered twice." << endl;
           cout << "Please type a person's name and his/her score." << endl;
           name = names[x-1];
           score = scores[x-1];
           names.pop_back();
           scores.pop_back();
           x--;
            }
        }


        names.push_back(name);
        scores.push_back(score);
        x++;

        }



    cout << "Program terminated by user." << endl;

    for(int y = 0; y < x; y++)
    {
        cout << names[y] << " --- " << scores[y] << endl;
    }


}
Topic archived. No new replies allowed.