problem-arrays

Pages: 12
Hi all,

First post here. teaching myself C++ and strugling a bit!
Quesion I cant figure out a solution too using arrays.

Question
list of first names (27 elements)
list of second names (27 elements)

The first element in list one matches first element in list 2 and so on down the list.

Write a C++ program that performs each of the following:

(a) Randomly displays one of the above second names and prompts the user to type in the first name that matches it.

(b) If the user types in a correct answer the program should display “Correct Answer!” and exit.

(c) If the user types in a wrong answer, the program should display the correct answer and repeat step (a).

(d) If the user types in the word exit, the program should exit.

Example Screen output:
What is Mr smiths first name?
John
Mr smiths first name is Peter
What is mr browns name?
John
Correct Answer!

Last edited on
Hi, and welcome:) The easiest thing would probably be to use a struct containing both names, and having a single array of such structs, but if you are relatively new to C/C++, I don't know how familiar you are with structs yet.

Another thing you could do is this:

d) Exit if the user enters "exit":
if (input == "exit") {return 0;}

a) Get a random entry in the last_names array:
last_names[rand()%length]; //where length= length of the array

b) Check if the user enters the correct name:
if (input == first_names[index]) {//display the "correct message"

c)
else {//display the "wrong message"
Last edited on
Thanks fafner, I am not familiar with structs yet(or most C++ for that matter)

Here is a rough attempt below at a similiar example. This one uses countries and their corrosponding capital. Any help to fill in the blanks would be much appreciated.

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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
	const int countrycount =27; //country is an array of 27 
	const int capitalcount =27:

        string Country  = new string[countrycount];   
        Country[0] = "Austria";
	Country[1] = "Belgium";
        Country[2] = "Bulgaria";
	Country[3] = "Cyprus";
	Country[4] = "Czech Republic";
	Country[5] = "Denmark";
	Country[6] = "Estonia";
	Country[7] = "Finland";
	Country[8] = "France";
	Country[9] = "Germany";
	Country[10] = "Greece";
	Country[11] = "Hungary";
	Country[12] = "Ireland";
	Country[13] = "Italy";
	Country[14] = "Latvia";
	Country[15] = "Lithuania";
	Country[16] = "Luxembourg";
	Country[17] = "Malta";
	Country[18] = "The Netherlands";
	Country[19] = "Poland";
	Country[20] = "Portugal";
	Country[21] = "Romania";
	Country[22] = "Slovakia";
	Country[23] = "Slovenia";
	Country[24] = "Spain";
	Country[25] = "Sweden";
	Country[26] = "United kingdom";

	string Capital = new string[capitalcount];
	Capital[0] = "Austria";
	Capital[1] = "Belgium";
        Capital[2] = "Bulgaria";
	Capital[3] = "Cyprus";
	Capital[4] = "Czech Republic";
	Capital[5] = "Denmark";
	Capital[6] = "Estonia";
	Capital[7] = "Finland";
	Capital[8] = "France";
	Capital[9] = "Germany";
	Capital[10] = "Greece";
	Capital[11] = "Hungary";
	Capital[12] = "Ireland";
	Capital[13] = "Italy";
	Capital[14] = "Latvia";
	Capital[15] = "Lithuania";
	Capital[16] = "Luxembourg";
	Capital[17] = "Malta";
	Capital[18] = "The Netherlands";
	Capital[19] = "Poland";
	Capital[20] = "Portugal";
	Capital[21] = "Romania";
	Capital[22] = "Slovakia";
	Capital[23] = "Slovenia";
	Capital[24] = "Spain";
	Capital[25] = "Sweden";
	Capital[26] = "United kingdom";
    
	if (input == "exit") {return 0;

        Capital[rand()%27]; 
        Cout << rand << "is the capital of which EU Country?"<< endl;
	Cin >>//not sure what to put here 

	if (input == Country[index])
		{
			Cout<< Correct Answer! <<endl;
	        }
	else
	       {   
			Cout<< rand << is the capital of <<//what do i put here to show correct Country name if input is wrong?
			//how do i make part (a) repeat itself?
	        }
Last edited on
First of all, create a string called input. Then do cin >> input. But you need to do this before you check if input=="exit". Secondly, this will only run once, if you want the program to ask several questions you need to put it inside a loop.

Also, it's easier to define arrays like this:

string Capital[]= {"Austria", "Belgium", "Bulgaria", "Cyprus", "Czech Republic"}

but put all the countries in it, of course:)
Thanks for the tip on defining arrays, much easier!

I created the string input but not sure what to do after this to make it work?

Program needs to:

(a)Randomly displays one of the above capital cities and prompts the user to type in the country of which it is a capital.

(b) If the user types in a correct answer the program should display “Correct Answer!” and exit.

(c) If the user types in a wrong answer, the program should display the correct answer and repeat step (a).

(d) If the user types in the word exit, the program should exit.


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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{

  String input = "exit";

  string Country[27]= {"Austria", "Belgium", "Bulgaria", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", 
	                   "Germany", "Greece", "Hungary", "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Malta", "The Netherlands",
					   "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Sweden", "United Kingdom"}
  
  String Capital[27]= {"Vienna", "Brussels", "Sofia", "Nicosia", "Prague", "Copenhagen", "Tallinn", "Helsinki", "Paris", "Berlin", "Athens", 
                       "Budapest", "Dublin", "Rome", "Riga", "Vilnius", "Luxembourg", "Valletta", "Amsterdam", "Warsaw", "Lisbon", "Bucharest", 
					   "Bratislava", "Ljubljana", "Madrid", "Stockholm", "London"}

  if (input == Country[index])
		{
			Cout<< Correct Answer! <<endl;
	    }
	else
	    {   
			Cout<< rand << is the capital of <<//what do i put here to show correct Country name if input is wrong?
			//how do i make part (a) repeat itself?
	    }
Last edited on
Ok, here's an idea:

1
2
3
4
5
6
7
8
9
10
11
12
//after the array-initializations
while(input != "exit") {
	int index = rand()%27;
	cout << "What is the capital of " << Country[index] << "?\n";
	cin >> input;
	if (input == Capital[index]) {
		cout << "Correct!\n";
	} else {
		cout << "Wrong!\n";
}
}
	
Last edited on
Thanks! I see how this will work now, thanks.

When i run the below code i get some errors. Not sure what to change?


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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{

  string input = "exit";

  string Country[27]= {"Austria", "Belgium", "Bulgaria", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", 
	                   "Germany", "Greece", "Hungary", "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Malta", "The Netherlands",
					   "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Sweden", "United Kingdom"}
  
  string Capital[27]= {"Vienna", "Brussels", "Sofia", "Nicosia", "Prague", "Copenhagen", "Tallinn", "Helsinki", "Paris", "Berlin", "Athens", 
                       "Budapest", "Dublin", "Rome", "Riga", "Vilnius", "Luxembourg", "Valletta", "Amsterdam", "Warsaw", "Lisbon", "Bucharest", 
					   "Bratislava", "Ljubljana", "Madrid", "Stockholm", "London"}

  while(input != "exit") {
	int index = rand()%27;
	cout << "What is the capital of " << Country[index] << "?\n";
	cin >> input;
	if (input == Capital[index]) {
		cout << "Correct!\n";
	} else {
		cout << Country[index] << "is the capital of" <<Capital[index];//will this line bring up the correct capital?
}
}




1> : error C2146: syntax error : missing ';' before identifier 'string'
1>: error C2146: syntax error : missing ';' before identifier 'Capital'
1> : error C2275: 'std::string' : illegal use of this type as an expression
1> : see declaration of 'std::string'
1> : error C2065: 'Capital' : undeclared identifier
1> : error C2059: syntax error : '{'
1> : error C2143: syntax error : missing ';' before '{'
1> : error C2143: syntax error : missing ';' before '}'
1> : error C2065: 'Capital' : undeclared identifier
1> : error C2065: 'Capital' : undeclared identifier
1> : error C2059: syntax error : '}'
1> : fatal error C1075: end of file found before the left brace '{' at 'c:\users\david\documents\visual studio 2008\projects\a1q4\a1q4\a1q41.cpp(9)' was matched
1>a1q4 - 11 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
1
2
3
					   "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Sweden", "United Kingdom"} //no semicolon here
//...
					   "Bratislava", "Ljubljana", "Madrid", "Stockholm", "London"} //no semicolon here 

When the compiler complains about semicolons, always look at the line before the one that triggered the error.
Ok, getting closer, thanks :)

how do i get visual studio 2008 to show the line numbers for each line of code?

just two errors now from below code:

1> error C2059: syntax error : '}'
1> fatal error C1075: end of file found before the left brace '{' at 'c:\users' was matched
1> - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{

  string input = "exit";

  string Country[27]= {"Austria", "Belgium", "Bulgaria", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", 
	                   "Germany", "Greece", "Hungary", "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Malta", "The Netherlands",
					   "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Sweden", "United Kingdom"};
  
  string Capital[27]= {"Vienna", "Brussels", "Sofia", "Nicosia", "Prague", "Copenhagen", "Tallinn", "Helsinki", "Paris", "Berlin", "Athens", 
                       "Budapest", "Dublin", "Rome", "Riga", "Vilnius", "Luxembourg", "Valletta", "Amsterdam", "Warsaw", "Lisbon", "Bucharest", 
					   "Bratislava", "Ljubljana", "Madrid", "Stockholm", "London"};

  while(input != "exit") 
    {
	int index = rand()%27;
	cout << "What is the capital of " << Country[index] << "?\n";
	cin >> input;

	if (input == Capital[index]) 
    {
		cout << "Correct!\n";
    } 
	else 
    {
		cout << Country[index] << "is the capital of" <<Capital[index]<<//will this line bring up the correct capital?
	}
}
Last edited on
How about another solution,

1. store the random array index that you display.
2. Store the input from user, and check it against the index of second array, using an
if () {
//correct action
}else{
//incorrect action
}


rough example:-

int index = rand()%27;

char * answer; // can also use [string answer];

cout << country [index] << "Some text message, asking some input" ;
cin >> answer;

if(answer==capital[index]){

cout << "Correct" << endl;

}else{

cout << "Sorry, not correct" << endl;

}


I think you would be able to add the rest of essential code. It only presents the idea for answer checking. Cant say for sure about its efficiency though as I am a beginner myself.
Thanks danishx83, i will give that soultion a go next.

I get the below to compile without errors but it just gives a "press any key to conyinue" output when i run it. what am i doing wrong?

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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{

  string input = "exit";

  string Country[27]= {"Austria", "Belgium", "Bulgaria", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", 
	                   "Germany", "Greece", "Hungary", "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Malta", "The Netherlands",
					   "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Sweden", "United Kingdom"};
  
  string Capital[27]= {"Vienna", "Brussels", "Sofia", "Nicosia", "Prague", "Copenhagen", "Tallinn", "Helsinki", "Paris", "Berlin", "Athens", 
                       "Budapest", "Dublin", "Rome", "Riga", "Vilnius", "Luxembourg", "Valletta", "Amsterdam", "Warsaw", "Lisbon", "Bucharest", 
					   "Bratislava", "Ljubljana", "Madrid", "Stockholm", "London"};

  while(input != "exit") 
    {
	int index = rand()%27;
	cout << "What is the capital of " << Country[index] << "?\n";
	cin >> input;

	if (input == Capital[index]) 
    {
		cout << "Correct!\n";
    } 
	else 
    {
		cout << Country[index] << "is the capital of" <<Capital[index];
	}
    }
}
Songok: You could easily see where the problem is if you formatted the braces properly.
cout << Country[index] << "is the capital of" <<Capital[index]<<//will this line bring up the correct capital?

yes it will you could also replace it by this

cout << Country[index] << "is the capital of" <<Capital[index]<< " , not " << input << endl;
cout << Country[index] << "is the capital of" <<Capital[index]<<//will this line bring up the correct capital?

yes it will you could also replace it by this

cout << Capital[index] << "is the capital of" <<Country[index]<< " , not " << input << endl;


I think you might want to exchange the Capital and Country array sequence during the display, Good luck.
Thanks Danishx83, appreciate the suggestion, have included it below.
Have tidied up the braces and formatting, thanks helios. any other suggestions?
Problem is I still get error below when running:

fatal error C1075: end of file found before the left brace '{' at


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
  string input = "exit";

  string Country[27]= {"Austria", "Belgium", "Bulgaria", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", 
	                   "Germany", "Greece", "Hungary", "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Malta", "The Netherlands",
					   "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Sweden", "United Kingdom"};
  
  string Capital[27]= {"Vienna", "Brussels", "Sofia", "Nicosia", "Prague", "Copenhagen", "Tallinn", "Helsinki", "Paris", "Berlin", "Athens", 
                       "Budapest", "Dublin", "Rome", "Riga", "Vilnius", "Luxembourg", "Valletta", "Amsterdam", "Warsaw", "Lisbon", "Bucharest", 
					   "Bratislava", "Ljubljana", "Madrid", "Stockholm", "London"};

  while(input != "exit") {
	int index = rand()%27;
	cout << "What is the capital of " << Country[index] << "?\n";
	cin >> input;
	if (input == Capital[index]) {
	cout << "Correct!\n";
        } else {
	cout << Capital[index] << "is the capital of" <<Country[index]<<" ,not" <<input <<endl;
    
}
}
Last edited on
It's still wrong. Your braces aren't aligned at all!
Helios, did i mention Im new to C++, thats why i posted in the beginner forum.

Thanks to anyone that can suggest how the braces should be formatted and maybe point out where i am going wrong in the above :)
It's not a problem in the code itself. It's how you're formatting it that doesn't let you see where your errors are. The fact that it's in C++ is incidental.

Get a copy of AStyle and run it through your file.
Last edited on
You're simply missing a closing brace, put one more at the end of the file. Also you should end your main-function with return 0;
Last edited on
Thanks guys. I will use AStyle in future, thanks helios.

Fafner, i added the missing brace and the return 0: but I still just get "press any key to continue when running??

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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
  string input = "exit";

  string Country[27]= {"Austria", "Belgium", "Bulgaria", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", 
	                   "Germany", "Greece", "Hungary", "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Malta", "The Netherlands",
					   "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Sweden", "United Kingdom"};
  
  string Capital[27]= {"Vienna", "Brussels", "Sofia", "Nicosia", "Prague", "Copenhagen", "Tallinn", "Helsinki", "Paris", "Berlin", "Athens", 
                       "Budapest", "Dublin", "Rome", "Riga", "Vilnius", "Luxembourg", "Valletta", "Amsterdam", "Warsaw", "Lisbon", "Bucharest", 
					   "Bratislava", "Ljubljana", "Madrid", "Stockholm", "London"};

    while(input != "exit") 
    {
	    int index = rand()%27;
	    cout << "What is the capital of " << Country[index] << "?\n";
	    cin >> input;
	    
        if (input == Capital[index]) 
        {
		cout << "Correct!\n";
        }     
              else 
              {
		      cout << Capital[index] << "is the capital of" <<Country[index] <<" ,not" << input << endl;
              }
    }
              return 0;        
}
Pages: 12