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 = newint[num];
The second part I am unsure of how to make sure the user input is the correct type.
#include <iostream>
#include <string>
usingnamespace std;
constint 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.
@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.
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.