Trying to dynamically change the size of my array

Good afternoon/morning (depending on your time zone) after piecing together code I have found with some of my own, I have the program below. I am trying to make it so the user can change how many Speakers there are and also I would like to make sure that the input is in the correct format. Any help would be appreciated.
I think I need something like

1
2
3
4
5
6
7
8
9
10
11
12
        cout<<"Please enter how many Speaker's will be attending:\n";
	cin>>num;

	while(num<0)
	{
		cout<<"Please enter how many Speaker's will be attending:\n";
		cin>>num;
	}



	speakerInfo = new int[num];

The second part I am unsure of how to make sure the user input is the correct type.
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

 #include <iostream>
#include <string>


using namespace std;

const int SIZE =10;

struct speakerInfo
{
string name;
string phone;    
string topic;  
int fee ;
};

void get(speakerInfo *);
void print(speakerInfo *);
void edit(speakerInfo *);

int main()
{                         
        speakerInfo info[SIZE];   
        int menu;

        do
		{
			cout << "Please select a choice from the following menu:\n";
			cout << "===============================================\n";
            cout<< "\t1) Enter speaker Info.\n";
            cout<< "\t2) Change speaker Info.\n";
            cout<< "\t3) Print speaker Info.\n";
            cout<< "\t4) Quit.\n";
			cin >> menu;
        
				switch (menu)
				{
               case 1:
					{
                    get(info);
                    }
                    break;
               case 2:
					{
                    edit(info);
                    }
                    break;
               case 3:
					{
					print(info);
                    }
                    break;
				}
		}
		while (menu != 4);
   system("pause");
   return 0;
}
void get(speakerInfo *program)
{ 
     int fee=0 ;
	 system("cls");
			cout<<"There will be 10 speakers in this data base numbered 0-9\n";
     for (int i=0; i<SIZE; i++)
		{
			cout <<"Please enter the following information of speaker "<<i<< " : \n";
			cout <<"Speaker Name:";
			cin.ignore();  
			getline (cin, program[i].name);
			cout<<"\n";

			cout<<"Speaker telephone number area code included\n";
			cout<<"using the phone number format example (555)555-5555 :";
			cin.ignore();  
			getline (cin, program[i].phone);
			cout<<"\n";

			cout<<"Speaker topic:";
			cin.ignore();
			getline (cin, program[i].topic);
			cout<<"\n";

			cout<<"Fee required: $";
			cin>>program[i].fee;
			cout<<"\n";
		}	
}

void print(speakerInfo *program)
{
     
     system("cls");
     for (int i=0; i<SIZE; i++)
	 {
     cout<<"The information entered for each speaker is: \n";
     cout<<"Speaker number:"<<i<<endl;
     cout<<"Speaker Name: "<<program[i].name<<endl;
     cout<<"Speaker Telephone Number: "<<program[i].phone<<endl;
     cout<<"Speaker Topic: "<<program[i].topic<<endl;
     cout<<"Speaker Fee Required:$"<<program[i].fee<<endl;
	 cout<<"\n";
     }
}

     
void edit(speakerInfo *program)
{
     int num;
	 system("cls");
     cout <<"Please enter the number of the speaker (0-9)\n";
	 cout<<"for who's information you would like to change:\n";
     cin >> num;
	 cout<<"\n";
	 while (num<0 || num >9)
	
			{
				cout<<"That selection is an invalid.\n";
				cout<<"The speakers range from 0-9.\n";
				cout<<"Please enter the number of the speaker (0-9) you would like to change.\n";
				cin >> num;
			}
     
      
			cout << endl;
			cout <<"Please enter the updated information of the speaker: \n";
			cout <<"Speaker Name:";
			cin.ignore();  
			getline (cin, program[num].name);
			cout<<"\n";

			cout<<"Speaker Telephone Number:";
			getline (cin, program[num].phone);
			cout<<"\n";

			cout<<"Speaker Topic:";
			getline (cin, program[num].topic);
			cout<<"\n";

			cout<<"Fee Required:$";
			cin>>program[num].fee;
			cout<<"\n";

}
Well since C++ doesn't allow Variable Length Arrays the first snippet shouldn't work, I suggest you think about using std::vector instead of the array.


The second part I am unsure of how to make sure the user input is the correct type.

What do you mean by "the correct type"?
@jib - The problem with the first snippet is not the use of variable length arrays. The OP is trying to allocate an array of ints and assign it to the type speakerInfo.

@OP - What you want at line 12 of the first snippet is:
1
2
  speakerInfo * info;  // replaces line 24 in second snippet
  info = new speakerInfo[num];


Your for loops at line 65 and 94 are going to need to change since you will have only num valid entries.

As jib suggested, the use a std::vector will simplify dealing with a variable number of entries. std:vector eliminates the need to dynamically allocate the array, automatically keeps track of the number of entries and eliminates the possibility of a memory leak. It also makes the program a little more user friendly. Instead of asking exactly how many entries, you can simply keep asking for entries until the user specifies no more.

Last edited on
Thanks for the suggestions. The part about the correct input type, I am trying to force the user to not put a Char where an int is supposed to be and vise versa.
The part about the correct input type, I am trying to force the user to not put a Char where an int is supposed to be and vise versa.

Well the easiest way to insure the user is inputting the "correct" type of data is to always get the data from the user into a string. Verify the string has the "correct" data then convert this string to the proper type using a stringstream. If the string has the wrong type of data ask the user to retry inputting the correct data or abort the program. This will avoid most of the causes of failing streams since almost any entry the user can make is a character.

Topic archived. No new replies allowed.