A little problem

Problem is on the function populator. When I cin it only asks for 6 answers. But i need 7. Telephone part is totally missed out.

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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <iostream>
#include <string>
#include <sstream>


using namespace std;
struct student
    {
           string name;
           int grade;
           string gender;
           string studentnumber;
           string medperil;
           string address;
           string telephone;
    }; 
   


void populator(student array[] , int length)
{
     cout << "Enter your students  information in the format shown below for each student" << endl;
     cout << " Name" << endl;
     cout << " Grade" << endl;
     cout << " Gender" << endl;
     cout << " Studentnumber" << endl;
     cout << " Medperil" << endl;
     cout << " Address" << endl;
     cout << " Phone" << endl;
     
     for (int m=0 ; m < length ; m++)
     {      
      
         cin >> array[m].name;  
         cin >> array[m].grade;
         cin >> array[m].gender;
         cin >> array[m].studentnumber;
         getline (cin,array[m].medperil);
         getline (cin, array[m].address);
         getline (cin, array[m].telephone);  
         
         cout << "Next student" << endl;
         cout << "" << endl;
         system("PAUSE");
         system ("CLS");                
     }
         
         
}
void displayer(student array[], int length)
{
     for (int m=1 ; m <= length ; m++)
     {      
            cout << "Student " << m << endl;
               
         cout << array[m-1].name << endl;  
         cout << array[m-1].grade << endl;
         cout << array[m-1].gender<< endl;
         cout << array[m-1].studentnumber << endl;
         cout << array[m-1].medperil << endl;
         cout << array[m-1].address << endl;
         cout << array[m-1].telephone << endl;
         cout << "" << endl;
         system("PAUSE");
         system ("CLS");
                
     }
     
     
}
void changer(student array[])
{
     int m, input;
     string ans;
     int holder;
     string holders;
     cout << "Do you want to change anything. y/n" << endl;
     cin >> ans;
     while (ans == "y" || ans == "Y")
     {
           
     
           cout << "Please say the position of the student you would like to change" << endl;
           cin >> m;
           cout << "what would you like to change?" << endl;
           cout << " 1) Name" << endl;
           cout << " 2) Grade" << endl;
           cout << " 3) Gender" << endl;
           cout << " 4) Studentnumber" << endl;
           cout << " 5) Medperil" << endl;
           cout << " 6) Address" << endl;
           cout << " 7) Phone" << endl;
           cin >> input;
           if (input ==1)
           {
                     cout << "what would you like to change it to?" << endl;
                     cin >> holders;
                     holders = array[m].name;  
                     cout << "changed" << endl;                   
           }
           else if (input == 2)
           {
                cout << "what would you like to change it to?" << endl;
                cin >> holder;
                holder = array[m].grade; 
                cout << "changed" << endl;  
           }
           else if (input == 3)
           {
                cout << "what would you like to change it to?" << endl;
                cin >> holders;
                holders= array[m].gender;  
                cout << "changed" << endl;  
           }
           else if (input ==4)
           {
                cout << "what would you like to change it to?" << endl;
                cin >> holders ;
                holders = array[m].studentnumber ;  
                cout << "changed" << endl;                 
           }
           else if (input ==5)
           {
                cout << "what would you like to change it to?" << endl;
                getline (cin,holders);

                holders= array[m].medperil  ; 
                cout << "changed" << endl;  
           }
           else if (input ==6)
           {
                cout << "what would you like to change it to?" << endl;
                cin >> holders ;
                holders= array[m].address  ; 
                cout << "changed" << endl;  
           }
           else if (input ==7)
           {
                cout << "what would you like to change it to?" << endl;
                cin >> holders;
                holders = array[m].telephone ; 
                cout << "changed" << endl;   
           }
           else
           {
            cout << "Nothing to change" << endl;
           }
           cout << "Do you want to change anything. y/n" << endl;
           cin >> ans;
     } 
     
}
    
   


int main(void)
{
    int length; 
    int input;
    string ans;
    
    cout << "How many students are there in your class" << endl;    
    cin >> length; // inputting the length of the class    
    student array[length]; 
    ans = "y";
    while (ans == "y") 
    {
    cout << "What do you want to do?" << endl;
    cout << "1) Change the values" << endl;
    cout << "2) Display the values" << endl;
    cout << "3) Populate the array" << endl;
    cin >> input;
    if (input ==1)
    {
              changer(array);
    }
    else if (input ==2)
    {
              displayer(array, length);
    }
    else if (input ==3)
    {
         populator(array, length);
    }
    else
    {
        cout << "The end" << endl;
    }
    cout << "Do you wish to continue?" << endl;
    cin >> ans;
    system ("CLS");
    system("PAUSE");
    }
      
   
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
I believe it's because in your void populator(student array[] , int length) function, you have for (int m=0 ; m < length ; m++) instead of for (int m=0 ; m <= length ; m++). Also, wouldn't it be easier and cleaner looking, to use switch() for the variable input instead of a bunch of if's and else if's? Just wondering..
Last edited on
white nite. I hate case structure:p no that is not the problem. I have tried . the problem is on

cin >> array[m].name;
. I just figured that out. help!
I had too many problems running your code. You cannot use array as a variable. When something turns blue, it should be a warning to you that it cannot be a variable. Second, you cannot define an array with a number that isn't a constant. If you need an array that isn't constant, then look into vector or deque as an alternative. Lastly, since I could not run the program to see if it would work, I suspect that it didn't run properly because you did not flush it. I can't say for sure because I can't run it and don't have the time to fix all the errors. I would also set everything to getline because I remember alternating between using it and not using it caused me problems in the past. Sorry I didn't have time to fix your code, if you are still having problems with it later, I can work with it later for you.
William: Program runs perfectly for me. I am using c++. Array is not a keyword in c++.
bump!
Don't mix geline and operator>> without reading this:

http://cplusplus.com/forum/articles/7311/
Last edited on
tell me the problem. I am just a beginner. That is why I am posting in beginner. As a student those details dont matter. We want the program to work. Thats it for now.
bump
bump
Follow that link and read it. It has the solution to your problem.
sorry i dont find anything there. please help me
My mistake, that thread doesn't seem to have the details (anymore?). This thread has what you need (ignoring the newline that gets left in the stream):
http://www.cplusplus.com/forum/beginner/20577/
thanks dude. that post has same problem
He didn't use Array, he used array and in my compiler it turned blue. I am using C# so maybe in his compiler it isn't a problem, but it was definitely in mine when I cut and paste the code to my test project.

biplav17, even though you are a beginner, the information is important to know if you are to program c/c++. So I will try to simplify it for you because even though I didn't click and read the link, I suspect I know what it says already.

When I wrote code and used getline in parts of my code while using cin for other parts, it caused problems with the iostream. You should stick to using just one or the other. I personally choose getline just because I find it easier to manipulate and test for errors. Even getting a number with getline is easy because you can test the characters to make sure they fall within number ranges while easy converting them using the sstream header and the istringstream class. I use istringstream and ostringstream in a lot of my code, it is very useful.

If you choose not to use getline, then you have to guess the maximum amount of characters someone will input and set up a char array to hold the values. Not you have potential memory problems if someone want to be a jerk and type in 60001 characters just because they know you set up the array as char[60000]. Since string expands as needed but stays small when not much space is used, it is better to use and works great with getline. Also getline can be use other ways with the third variable. The third variable is automatically defined as '\0' and you can change that to anything so instead of detecting a new line, you can detect a certain character which would be anything character including those of non alpha and numeric kind. I personally like the upside down exclamation point.

Understanding this is important because if you don't grab the issue you are having and actually understand why, then in the future you will be right back here asking again, which will waste your precious time when you could have accomplished a lot more with it. Everything people tell you here is to help you, even if it seems tedious at times. But C/C++ doesn't leave any room for error, you either perfect the code or never have the program you truly want.

Good luck with your programs.
why not use printf or scanf as getline only gets string . I learnt it on the past week. lol
You can use them if you like, they are the older versions of cin and cout (Basically the C versions of these C++ functions). They can do what you need them to do but like I said earlier, you will be limited to preset size limits since you have to use character arrays instead of strings. This is why I don't use them, less memory issues unless you can control the data that is coming in like lets say a window application reading in an edit box.
Topic archived. No new replies allowed.