Naming babies

I've come across a problem where I need an infinite amount of integers.
For example, when I need to create a program where you can name babies, the program needs to set all the names to different 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
32
33
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

int main(){
	string baby1;
	string baby2;
	int babynr;
	int amount;

	cout << "Enter the amount of babies." << endl;
	cin >> amount;
	if(amount >= 1){
	cout << "Enter the name of the first baby." << endl;
	cin >> baby1;
	}
	if(amount >= 2){
	cout << "Enter the name of the second baby." << endl;
	cin >> baby2;
	}
	cout << "Enter the number of the baby." << endl;
	cin >> babynr;
	if(babynr == 1){
	cout << baby1 << endl;
	}
	if(babynr == 2){
	cout << baby2 << endl;
	}

	_getch();
}


But what if the user has 92 babies? I have to create 92 strings?

Any help is much appreciated.
Last edited on
You can use a dynamically allocated array, or you can look up std::vector<> and use that instead (the latter being easier).
My vote is for using std::vector.

Additionally I would recommend you not use conio.h. You don't even need _getch() at the end there, you can replace it with
std::cin.get();
which all working C++ implementations will support (many will not support conio.h)
Better yet, you could use
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
which pauses execution until either the maximum number of characters has been entered or a newline is entered. If you use that, you need to add #include <limits>
Last edited on
You could use std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
I've been looking for that line for weeks! Thanks!


std::vector<>, std::vector. - Ookay, I'll use the vector thingy. Thanks again!
Topic archived. No new replies allowed.