Assistance needed on my code.

So we have to write a code that uses vectors and the topic is asking for amendment between 1 and 27, then when user enters 1 it should output amendment 1 with a little summary. Once I got those down it and when i entered 1 it would give me the description on amendment 2.


Notice that we use "std_lib.." folder which has the std, vectors etc in it.
Also I haven't entered all of my amendment for the 3-27. Since I want to fix this issue first.
THanks!

Here's my 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
  
#include "std_lib_facilities_4.h"


int main()
{

	vector<string> answers;
	answers.push_back("Amendment 1 guarantees freedom of religion, speech, press, assembly, and petition.\n");
	answers.push_back("Amendment 2 guarantees the people's right to own and bear arms for their defense.\n");
	int i;
	cout << "Please enter number between 1 and 27: ";
	cin >> i;
	
	while(1){
		
	while(i < 1 || i > 27)
	{
		cout << "That is not a valid number. Please enter a number between 1 and 27. \n"; 
		cout << "Please enter number between 1 and 27: ";
		cin >> i;
		
	} 
	  	cout <<answers[i];
		cout << "Please enter number between 1 and 27: ";
		cin >> i;
		
	}
	
	
	return 0;

}
All arrays/vectors/containers start at 0. So 0 - 26.
Sorry I am new to vectors. How do I input that into my code? Do i add the 0 and 26 to while loop?
Yep, you could just change the while loop and ask the user for 0-26.
People seem to like starting at 1 though so you could just subtract 1 when you access the vector and be done. Up to you.

cout <<answers[i - 1];

Oh ok, thank you!
Topic archived. No new replies allowed.