how to automatically declare a variable in a loop without having to manually do it

I am using a menu for the user that uses the numbers 1 and 0 to either enter a name or end the program. I am trying to store a person's name into the a string called Name. However every time a person enters a name, it overwrites the name string.

For example:
Input: Zachary
Output: Zachary

User presses 1 to enter a second name

Input: Lauren
Output: Lauren

The problem I am having is that every time the user enters a name, it overwrites the previous name. I realize I could make string called Name1, Name2, Name3, etc. But to me that is not effective. What if a user wants to enter 200 new names? I would have to make a new variable for each name, what would be an effective way to store several names into different variables without having to manually make a new variable every time a name was entered?

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

int main
{
int answer;
string name;

cout << "Please enter a 1 to enter your name, or a 0 to terminate the program";
cin >> answer;

if (answer == 1)
{
  cout << "Please enter your name: ";
  cin.ignore();
  getline (cin, name);

cout << "Please enter a 1 to enter your name, or a 0 to terminate the program";
cin >> answer;

}

if (answer == 0)
{
  cout << "Program terminating"

}

system("Pause");
return 0;
}
I have'nt understood what you're exactly trying to do.
can you give us the Input/Output you want & and the Input/Output you're having?
and please explain more about what you're trying to do.
That is what container are for. You could use an array, but a vector would be better.
@HBehbahani

I am trying to figure out how to declare a variable without having to manually do it. For example if a user wants to enter 10 names, I have to declare 10 separate variables called Name1, Name2, Name3, Name4, Name5.. etc. Is there a work around to this?
Last edited on
Ok see that's the confusing part, arrays only with work characters is that correct? And I am working with entire strings
Arrays and vectors work with any data type.
> Ok see that's the confusing part, arrays only with work characters is that correct?
> And I am working with entire strings

A program with a sequence of strings:

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

int main()
{
    // a container to hold a sequence of names.
    // std::vector is the default sequence container.
    // see: https://www.mochima.com/tutorials/vectors.html
    std::vector<std::string> all_names ;

    std::string this_name ;
    // quit the loop when an empty string is entered
    while( std::cout << "enter a name (or an empty string to end input): " &&
           std::getline( std::cin, this_name ) && !this_name.empty() )
    {
         all_names.push_back(this_name) ; // append this name to the sequence of names
    }

    if( !all_names.empty() ) // if there is at least one name in the sequence
    {
        std::cout << "\nthe names are:\n----------\n" ;
        for( std::string a_name : all_names ) // for each name in the sequence of names
                                              // http://www.stroustrup.com/C++11FAQ.html#for
        {
            std::cout << a_name << '\n' ;
        }
    }

    std::cout << "\nProgram terminating\n" ;
}
Topic archived. No new replies allowed.