adding letters to a string

I am writing a program that can add letters to a vector then display all elements of the vector. However, once I add letters, the vector doesn't print out anymore in the letterList function. Can anyone help me make this successful?

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


//Adds letters to the vector
void addLetters(string letter, vector<string> &letters, int i){
  letters.resize(i);
  letters[i]=letter;
  for (int i=0; i<letters.size(); i++){
    cout<<letters[i]<<endl;
  }
}

//Displays all letters
void letterList(vector<string> &letters){
  for (int i=0; i<letters.size(); i++){
    cout<<i+1<<". "<<letters[i]<<endl;
  }
}

int main() {
  char choice;
  int add;
  string letter;
  vector<string>letters{"a", "b", "c"};
  while(choice!='c'){
    cout<<endl<<"a. Enter additional letters"<<endl;
    cout<<"b. View full list of letters"<<endl;
    cout<<"c. Quit"<<endl;
    cin>>choice;
    //Options
    if (choice=='a'){
      cout<<"How many: ";
      cin>>add;
      for (int i=0; i<add; i++){
        cout<<"Enter additional letter: ";
        cin.ignore();
        getline(cin, letter);
        addLetters(letter, letters, i);
      }
    }
    if (choice=='b'){
      letterList(letters);
    }
  }
}
Last edited on
vectors work like arrays.
vector.resize(i);
vector[i] = x; // <-- still out of bounds, just like int x[10]; x[10] = 42;
that is lines 8 and 9. there could be other bugs, but I stopped reading there.

why not use push back?
vector<char> notastring;
for(int i = 0; i < 10; i++)
notastring.push_back(i+'a');
notastring.push_back(0);

cout << &notastring[0]; //like a c-string, using the 0 character I put into it, its one of many ways to do it (your loop works, but its a little brute-forceish).
Last edited on
You mention letters (which are type char) but use type std::string??

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
#include <iostream>
#include <vector>
#include <string>

//Adds letter to the vector
void addLetters(char letter, std::vector<char>& letters) {
	letters.push_back(letter);
}

//Displays all letters
void letterList(const std::vector<char>& letters) {
	for (size_t i = 0; i < letters.size(); ++i)
		std::cout << i + 1 << ". " << letters[i] << '\n';
}

int main() {
	char choice {};
	int add {};
	char letter {};
	std::vector<char>letters {'a', 'b', 'c'};

	while (choice != 'c') {
		std::cout << "\na. Enter additional letters\n";
		std::cout << "b. View full list of letters\n";
		std::cout << "c. Quit\n";
		std::cin >> choice;

		//Options
		if (choice == 'a') {
			std::cout << "How many: ";
			std::cin >> add;

			for (int i = 0; i < add; i++) {
				std::cout << "Enter additional letter: ";
				std::cin >> letter;
				addLetters(letter, letters);
			}
		}

		else if (choice == 'b')
			letterList(letters);
		else if (choice != 'c')
			std::cout << "Invalid option\n";
	}
}

Topic archived. No new replies allowed.