Can't figure out how to terminate my program.

Here is my program:

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
#include<iostream>
#include<stdlib.h>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;

int main()
{
cout<<"Enter names and scores.\n";
vector<string> names;
vector<int> scores;
int score;
string name;

do{
cin >> name >> score;
if(name.compare("NoName") && (score != 0)){
names.push_back(name);
scores.push_back(score);
}
}while(name.compare("NoName") && (score != 0));

string currentName;
for(int i=0; i < names.size(); ++i)
{
currentName = names[i];
for(int j = 0; j < names.size(); j++)
{
if(currentName == names[j] && j != i)
{
cout << "You entered repeat names.\n";
system("PAUSE");
return 0;
}
}

cout << names[i] << ", " << scores[i] << "\n";
}
system("PAUSE");
}


The assignment says to:
Write a program where you enter name and value pairs (like Joe 33). For each pair, add names to a vector and scores to another vector. Terminate input with NoName 0. Check that each name is unique and terminate with an error message if a name is entered twice. Write out all (name, score) pairs, one per line.

I've got everything except "terminating" the program.

This is the test input:
Barbara 22
Janna 11
Kendall 10
Ginnie 9
Henry 18
Barbie 31
Winston 45
Joe 17
Winston 32

So once Winston repeats, it should terminate with an error message.
I have no idea how to do that. Heck, this is in the chapter before errors in my C++ textbook.

How do I terminate with an error message AS SOON AS the repeat name is entered?
Last edited on
Since you are in main(), you can simply return 0; if you want to exit early.
As it is, you could check the vector before inserting a name to see if it already is there. If so, print an error and terminate.
Topic archived. No new replies allowed.