- #1
FallArk
- 127
- 0
I ran into some trouble with a C++ question. I need to make a program that will run like this:
This is what I came up with :
I was trying to use a for loop to ask for inputs and then assign them to the vector. Then i can use the vectr.pop_back() command to eliminate values. But the program just end itself after displaying Enter name #1:. I'm not sure whether the getline is not used correctly or i messed up setting the vectors.
Thanks for the great help so far!
Example run 1:
How many names do you want (max 99)? 5
Enter name #1: Adam Apple
Enter name #2: Betty Boop
Enter name #3: Charles Chaplin
Enter name #4: Debbie Dali
Enter name #5: Elaine Eggbert
What name do you want to eliminate? Adam Apple
Here is the list in reverse order, skipping Adam Apple...
Elaine Eggbert
Debbie Dali
Charles Chaplin
Betty Boop
ER]
How many names do you want (max 99)? 5
Enter name #1: Adam Apple
Enter name #2: Betty Boop
Enter name #3: Charles Chaplin
Enter name #4: Debbie Dali
Enter name #5: Elaine Eggbert
What name do you want to eliminate? Adam Apple
Here is the list in reverse order, skipping Adam Apple...
Elaine Eggbert
Debbie Dali
Charles Chaplin
Betty Boop
ER]
This is what I came up with :
Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int x = 0;
int i = 0;
vector<string> userName(x);
cout << "How many names do you want (max 99) ? ";
cin >> x;
if (x > 99) {
cout << "Out of memory!";
return 0;
}
for (i = 0; i < x; ++i) {
cout << "Enter name #" << i + 1 << ": ";
cin.ignore();
getline(cin,userName.at(i));
}
}
Thanks for the great help so far!