Outputting text from Array/Vector

I am having quite a bit of trouble just getting user input/ or even storing strings, etc. so let me lay out my problem.

Personal Project, so no limits, I do like to know how stuff works though.

Array or Vector
C_Strings, Strings, Char Array;

I understand those are what I have to work with.
I want to make a list of choices for the user, and store them so that I can easily print them out by calling their number.
Also the user will only enter in a number, then the number will correspond to the choice, so therefore I only really need to output all of the options in a timely manner.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
#include <string>
vector<string> user_choices;
void initializeChoices(vector<string>& user_choices);
using namespace std;
int main(){
	initializeChoices(user_choices);
	cout << "Hello, which number would you like to pick?" << endl;
	for (int i = 0; i < 1; i++){
		cout << i << ": " << user_choices[i] << endl;
	}
	return 0;
}
void initializeChoices(vector<string>& user_choices){
	user_choices.push_back("Choice 1");
}


My code so far. I know it is wrong, the compiler went crazy with errors.
Well, code is actually fine. You just has a slight misunderstanding about how using namespace std; works (actually it is better to not use it, but that is another topic)

Here is an example how it could look (uses C++11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
#include <string>


int main()
{
    std::vector<std::string> user_choices = {"Repeat", "Exit"};

	bool repeat;
    do {
        repeat = false;
        int choice = 0;
        std::cout << "Hello, which number would you like to pick?\n";
        for (const auto& s: user_choices)
		    std::cout << ++choice << ": " << s << '\n';
        std::cin >> choice;
        switch(choice) {
          case 1: repeat = true; break;
          case 2: repeat = false; break;
        }
    } while (repeat);
}


Last edited on
Move using namespace std; to just after the #lincludes

Andy
Topic archived. No new replies allowed.