Editing Data in Structure Array

Hello!

I am working on a program for my class, and the problem requests that the user be able to change data in a structure array:
Write a program that keeps track of a speakers' bureau. The program should use a structure to store the following data about a speaker:
Name
Telephone Number
Speaking Topic
Fee Required

The program should use an array of at least 10 structures. It should let the user enter data into the array, change the contents of any element, and display all the data stored in the array. The program should have menu-driven user interface.


Inputting the data and printing the data are fine - I can do that all day long, but changing the data is giving me a headache. My code is as follows:

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

using namespace std;

// Structure lists
    struct speakerInfo
    {
           string name;
           string bangoo;    //Japanese for phone
           string speakTopic;  
           int feeReq;
           };

//Function prototypes
void getSpeaker(speakerInfo *);
void printSpeaker(speakerInfo *);
void editSpeaker(speakerInfo *);

int main()
{
        int NUM_SPEAKERS = 10; //The number of speakers
        int index;             //Loop counter...        
        speakerInfo infos[10];    //Array to hold the stats for each speaker...
        //Choice for submenu
        int submenu;
        const int enter = 11,
                  change = 22,
                  print = 33,
                  leave = 44;
    
        //Menu display with a do-while loop
        do{
        cout << "Please select a choice from this submenu.\n"
             << "11) Enter Speaker Information.\n"
             << "22) Change Speaker Information.\n"
             << "33) Print Speaker Information.\n"
             << "44) Leave this menu.\n"
             << "Selection: ";
        cin >> submenu;
        
        switch (submenu){
               case enter:{
                    //enter speaker information
                    getSpeaker(infos);
                    }
                    break;
               case change:{
                    //change speaker information
                    editSpeaker(infos);
                    }
                    break;
               case print:{
                    //print speaker information
                    printSpeaker(infos);
                    }
                    break;
                    }}
                    while (submenu != leave);

   system("pause");
   return 0;
}
void getSpeaker(speakerInfo *p){
     int index=0;
     int size=10; // Array size
     for (index=0; index <= size; index++){
     cout <<"Please enter the following information of the speaker: \n";
     cout <<"Speaker Name:";
     cin.ignore();  //To skip remaining '\n' character
     getline (cin, p->name);
     cout<<"\nSpeaker Telephone Number:";
     cin.ignore();  //To skip remaining '\n' character
     getline (cin, p->bangoo);
     cout<<"\nSpeaker Topic:";
     cin.ignore(); //To skip remaining '\n' character
     getline (cin, p->speakTopic);
     cout<<"\nFee Required:";
     cin>>p->feeReq;
     }}

void printSpeaker(speakerInfo *p){
     int index=0;
     int size=10; // Array size
     for (index=0; index <= size; index++){
     cout <<"The information entered for each speaker is: \n";
     cout << "SPEAKER "<<index<<endl;
     cout << "Speaker Name: "<<p->name<<endl;
     cout << "Speaker Telephone Number: "<<p->bangoo<<endl;
     cout << "Speaker Topic: "<<p->speakTopic<<endl;
     cout << "Speaker Fee Required: "<<p->feeReq<<endl;
     }}
     
void editSpeaker(speakerInfo *){
     int i; //placeholder for the number wanted to be edited.
     cout << "Please enter the number of the speaker you would like to edit."
          << "Example: 5";
     cin >> i;
     cout << endl;
     cout <<"Please enter the updated information of the speaker: \n";
     cout <<"Speaker Name:";
     cin.ignore();  //To skip remaining '\n' character
     getline (cin, i->name);
     cout<<"\nSpeaker Telephone Number:";
     cin.ignore();  //To skip remaining '\n' character
     getline (cin, i->bangoo);
     cout<<"\nSpeaker Topic:";
     cin.ignore(); //To skip remaining '\n' character
     getline (cin, i->speakTopic);
     cout<<"\nFee Required:";
     cin>>i->feeReq;
     }


My error codes are:
In function `void editSpeaker(speakerInfo*)': base operand of `->' is not a pointer
x4

I have tried every method of directing to the array element's structure, but it either yells at me or tells me that 'name', 'bangoo', 'speakTopic' and 'feeReq' are not assigned variables. I have also been told 'infos' doesn't exist when I try adding that to the location.

If anyone could help me debug this it would be GREATLY appreciated. I'm about to throw my netbook out the window.
What does the "->" operator typically do? What behavior are you expecting from "i->name"? Id recommend you take some time to review pointers and dereferncing.
"->" dereferences structure pointers, and takes the place of a "." operator in statements using pointers to structures. The "->" operator automatically dereferences the structure pointer on it's left.

Basically strptr->member and (*strptr).member are the same thing, just -> is easier to read.

Yay for reading my book, however I don't exactly understand that. What I do understand is I was able to use "p.name" in a program that did not have a structured array, the only way for me to have "getSpeaker" and "printSpeaker" to work correctly was by using "p->name".

So, when I carried on to write "editSpeaker" I wanted "i->name" to:
-Take the inputted integer to find the associated array location
-Allow said array location to be rewritten.

So, user comes along and wants to replace John Doe, speaker number 3, with Mary Kay. User would type in '3' for 'i' and be able to rewrite John Doe's information with Mary Kay.
If you are dereferencing the value to the left of the "->", then i->something means you are dereferencing i correct? You would then expect i to be a (structure *) type - is that what i is?
Thank you rollie for pointing me in the right direction. I didn't want a reference, I wanted a pointer (pointers can be changed, references cannot). Since 'i' is a specific location, I needed it to be 'p[i].name' and the getSpeaker function to be 'p[index].name'

Or so I think. My code now compiles and links, but after I get to the 10th input, the program 'has stopped working' and crashed. Apparently the compiler thinks my syntax is correct, but it isn't?

My updated code:
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
#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

// Structure lists
    struct speakerInfo
    {
           string name;
           string bangoo;    //Japanese for phone
           string speakTopic;  
           int feeReq;
           };

//Function prototypes
void getSpeaker(speakerInfo *);
void printSpeaker(speakerInfo *);
void editSpeaker(speakerInfo *);

int main()
{
        int NUM_SPEAKERS = 10; //The number of speakers
        int index;             //Loop counter...        
        speakerInfo infos[10];    //Array to hold the stats for each speaker...
        
        //Choice for menu
        int submenu;
        const int enter = 11,
                  change = 22,
                  print = 33,
                  leave = 44;
    
        //Menu display with a do-while loop
        do{
        cout << "Please select a choice from this submenu.\n"
             << "11) Enter Speaker Information.\n"
             << "22) Change Speaker Information.\n"
             << "33) Print Speaker Information.\n"
             << "44) Leave this menu.\n"
             << "Selection: ";
        cin >> submenu;
        
        switch (submenu){
               case enter:{
                    //enter speaker information
                    getSpeaker(infos);
                    }
                    break;
               case change:{
                    //change speaker information
                    editSpeaker(infos);
                    }
                    break;
               case print:{
                    //print speaker information
                    printSpeaker(infos);
                    }
                    break;
                    }}
                    while (submenu != leave);

   system("pause");
   return 0;
}
void getSpeaker(speakerInfo *p){ //where p stands for the array name
                                         //array name = pointer
     int index=0;
     int size=10; // Array size
     for (index=0; index <= size; index++){
     cout <<"Please enter the following information of the speaker: \n";
     cout <<"Speaker Name:";
     cin.ignore();  //To skip remaining '\n' character
     getline (cin, p[index].name);
     cout<<"\nSpeaker Telephone Number:";
     cin.ignore();  //To skip remaining '\n' character
     getline (cin, p[index].bangoo);
     cout<<"\nSpeaker Topic:";
     cin.ignore(); //To skip remaining '\n' character
     getline (cin, p[index].speakTopic);
     cout<<"\nFee Required:";
     cin>>p[index].feeReq;
     }}

void printSpeaker(speakerInfo *p){
     int index=0;
     int size=10; // Array size
     for (index=0; index <= size; index++){
     cout <<"The information entered for each speaker is: \n";
     cout << "SPEAKER "<<index<<endl;
     cout << "Speaker Name: "<<p[index].name<<endl;
     cout << "Speaker Telephone Number: "<<p[index].bangoo<<endl;
     cout << "Speaker Topic: "<<p[index].speakTopic<<endl;
     cout << "Speaker Fee Required: "<<p[index].feeReq<<endl;
     }}
     
void editSpeaker(speakerInfo *p){
     int i; //placeholder for the number wanted to be edited.
     cout << "Please enter the number of the speaker you would like to edit."
          << "\nExample: 5\n";
     cin >> i;
     cout << endl;
     cout <<"Please enter the updated information of the speaker: \n";
     cout <<"Speaker Name:";
     cin.ignore();  //To skip remaining '\n' character
     getline (cin, p[i].name);
     cout<<"\nSpeaker Telephone Number:";
     cin.ignore();  //To skip remaining '\n' character
     getline (cin, p[i].bangoo);
     cout<<"\nSpeaker Topic:";
     cin.ignore(); //To skip remaining '\n' character
     getline (cin, p[i].speakTopic);
     cout<<"\nFee Required:";
     cin>>p[i].feeReq;
     }
You ran it and edited 10 speakers? Can you post the output for the failed run? As a general rule, you should always be validating input from a user - when you do 'cin >> i' in editSpeaker(), you don't ensure it's between the values 0-9; any other integers will cause you to index outside the bound of your array. You should be verifying the user actually entered an integer, and that the integer is in the range you expect.
WOOOOOOOOOOOOT.

The issue was: When I complied it and ran, it would get the the '10th speaker' and then freeze up, and windows would yell at me. That occured because I forgot arrays start at 0.

Okay, so the issue was the 'index <= size' in the loops. After I removed that, and made it 'index < size' it worked. I also had to remove several 'cin.ignore()' because it was a few too many and deleted the first character of my next line.

It also now edits fine.

And I also added the verification, thank you for the reminder.

In the end...

Thank you for all of your help rollie <3<3<3<3<3 my roommate kept staring at me blankly when I tried talking it out. Your questions helped direct me to where I was supposed to go.

YAY!!!!!!

Thank you thank you!

And my finished code:
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
// Structure lists
    struct speakerInfo
    {
           string name;
           string bangoo;    //Japanese for phone
           string speakTopic;  
           int feeReq;
           };

//Function prototypes
void getSpeaker(speakerInfo *);
void printSpeaker(speakerInfo *);
void editSpeaker(speakerInfo *);

int main()
{
        int NUM_SPEAKERS = 10; //The number of speakers
        int index;             //Loop counter...        
        speakerInfo infos[10];    //Array to hold the stats for each speaker...
        
        //Choice for menu
        int submenu;
        const int enter = 11,
                  change = 22,
                  print = 33,
                  leave = 44;
    
        //Menu display with a do-while loop
        do{
        cout << "Please select a choice from this submenu.\n"
             << "11) Enter Speaker Information.\n"
             << "22) Change Speaker Information.\n"
             << "33) Print Speaker Information.\n"
             << "44) Leave this menu.\n"
             << "Selection: ";
        cin >> submenu;
        
        switch (submenu){
               case enter:{
                    //enter speaker information
                    getSpeaker(infos);
                    }
                    break;
               case change:{
                    //change speaker information
                    editSpeaker(infos);
                    }
                    break;
               case print:{
                    //print speaker information
                    printSpeaker(infos);
                    }
                    break;
                    }}
                    while (submenu != leave);

   system("pause");
   return 0;
}
void getSpeaker(speakerInfo *p){ //where p stands for the array name
                                         //array name = pointer
     int index=0;
     int size=10; // Array size
     for (index=0; index < size; index++){
     cout <<"Please enter the following information of speaker "<<index<< " : \n";
     cout <<"Speaker Name:";
     cin.ignore();  //To skip remaining '\n' character
     getline (cin, p[index].name);
     cout<<"\nSpeaker Telephone Number:";
    // cin.ignore();  //To skip remaining '\n' character
     getline (cin, p[index].bangoo);
     cout<<"\nSpeaker Topic:";
    // cin.ignore(); //To skip remaining '\n' character
     getline (cin, p[index].speakTopic);
     cout<<"\nFee Required:";
     cin>>p[index].feeReq;
     }}

void printSpeaker(speakerInfo *p){
     int index=0;
     int size=10; // Array size
     for (index=0; index < size; index++){
     cout <<"The information entered for each speaker is: \n";
     cout << "SPEAKER "<<index<<endl;
     cout << "Speaker Name: "<<p[index].name<<endl;
     cout << "Speaker Telephone Number: "<<p[index].bangoo<<endl;
     cout << "Speaker Topic: "<<p[index].speakTopic<<endl;
     cout << "Speaker Fee Required: "<<p[index].feeReq<<endl;
     }}

     
void editSpeaker(speakerInfo *p){
     int i; //placeholder for the number wanted to be edited.
     cout << "Please enter the number of the speaker you would like to edit."
          << "\nExample: 5\n";
     cin >> i;
     if (i <=9)
          {
     cout << endl;
     cout <<"Please enter the updated information of the speaker: \n";
     cout <<"Speaker Name:";
     cin.ignore();  //To skip remaining '\n' character
     getline (cin, p[i].name);
     cout<<"\nSpeaker Telephone Number:";
     getline (cin, p[i].bangoo);
     cout<<"\nSpeaker Topic:";
     getline (cin, p[i].speakTopic);
     cout<<"\nFee Required:";
     cin>>p[i].feeReq;}
     else
     {
         cout << "I'm sorry, that is an invalid selection.\n"
              << "The speakers range from 0-9.\n"
              << "Please select this option again.\n\n";
              }
     }
Glad I could help :)
Topic archived. No new replies allowed.