Errors during initializing char to " "

//aren't allowed to use string in this program.
//error : incompatible types in assignment of `const char[1]' to `char[20]
#include <iostream>
using namespace std;
struct student
{
int idnum;
char name[20];
char gender;
};
void menu(int *);
int main()
{
student kbccstudent[500];
int p=5,y;
for(int x=0; x<500; x++)
{
kbccstudent[x].idnum = 0;
kbccstudent[x].name = " ";
kbccstudent[x].gender = " " ;
}
while(p!=4)
{
menu(&p);
switch(p)
{
case(1):
{
cout<<"What entry to update ? /n";
cin>>y;
cout<<"Enter student ID? \n";
cin>>kbccstudent[y].idnum;
cout<<"Enter student name? \n";
cin>>kbccstudent[y].name;
cout<<"Enter student Gender? (M/F) /n";
cin>>kbccstudent[y].gender;
cout<<"Datebase updated.";}
}
}
system("pause");
return 0;
}
void menu(int *p)
{
cout<<"Student Record Database \n"
<<"----------------------- \n"
<<"Options \n"
<<"1. Add student \n"
<<"2. Update record \n"
<<"3. Lookup record \n"
<<"4. Quit \n"
<<" Selection ? \n";
cin>>*p;
}
kbccstudent[x].name = " ";

There is no assignment operator for arrays. Assuming that you are trying to initialize kbccstudent[x].name to an empty string you can do so by kbccstudent[x].name[0] = '\0';. Notice the single quote marks in '\0'. If you were to write kbccstudent[x].name[0] = ' '; then kbccstudent[x].name[0] would have a space character at index zero and the remaining 19 characters would be garbage values ( whatever happened to be in memory) with no terminating null.

for kbccstudent[x].gender = " " ; which is just a single character replace the double quotes with single quotes kbccstudent[x].gender = ' ';

' ' is a single character, the space character.

" " is a string literal consisting of two characters, a space character and a terminating null.

I didn't really examine the rest of your code.
It works. Thank you. Is it possible that I replace the [0] after name with 20, to replace the 0 - 19 characters with zero ?

Last edited on
name is a character array of twenty characters. The values are accessed through the index values 0-19 so name[0] is the first character, name[1] is the second character and so forth up to name[19] which is the last character in the array.
I got it. Thank you.
one more question, at "cin.getline(kbccstudent[y].name, name_length);" , it doesn't allow to type in and skipped to next line. May I know the problems ?
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
#include <iostream>
#include <cstring>
#include <cstdlib> 
using namespace std; 
const int name_length = 20; 
struct student
{
int idnum;
char name[name_length]; 
char gender;
};

void menu(int *);
int main()
{
    student kbccstudent[500];
    int p=5,y,procceed;
    for(int x=0; x<500; x++)
     {
             kbccstudent[x].idnum = 0;
             kbccstudent[x].name[0] = '\0'; 
             kbccstudent[x].gender = ' ' ; 
     }
    while(p!=4)
{
    menu(&p); 
    switch(p)
    {
    case(1):
            {
     cout<<"What entry to update? \n";
     cin>>y;
     cout<<"\nEnter student name? \n";
     cin.getline(kbccstudent[y].name, name_length);
     cout<<"\nEnter student ID ? \n";
     cin>>kbccstudent[y].idnum; 
     cout<<"\nEnter student gender? (m/f) \n"; 
     cin>>kbccstudent[y].gender;
     cout<<"Database updated. \n";
     break;
            }
     case(2):
             {
     loop:
     cout<<"What entry to update? \n";
     cin>>y;
     cout<<"\nStudent record "<<y
          <<"\nName: "<<kbccstudent[y].name
          <<"\nID: "<<kbccstudent[y].idnum
          <<"\nGender: "<<kbccstudent[y].gender
          <<"\nPress 1 to procceed\n";
     cin>>procceed;  
     if(procceed == 1)
     {
     cout<<"\nEnter student name? \n";
     cin.getline(kbccstudent[y].name,name_length);
     cout<<"\nEnter student ID ? \n";
     cin>>kbccstudent[y].idnum; 
     cout<<"\nEnter student gender? (m/f) \n"; 
     cin>>kbccstudent[y].gender;
     cout<<"Database updated. \n";
     }
     else
     {
     cout<<"\nNot the right student. Going back\n";  
     goto loop; 
     }
     break;
            }
     case(3):
             {
     cout<<"What entry to lookup? \n";
     cin>>y;
     cout<<"\nStudent record "<<y
          <<"\nName: "<<kbccstudent[y].name
          <<"\nID: "<<kbccstudent[y].idnum
          <<"\nGender: "<<kbccstudent[y].gender;
     break;
             }
     case(4):
            {
     cout<<"Warning , window to be closed.\n";
     system("pause");
     exit(0);
             }
     }
}
    system("pause");
    return 0;
}
void menu(int *p)
{
     cout<<"\nStudent Record Database \n"
         <<"----------------------- \n"
         <<"Options \n"
         <<"1. Add student \n"
         <<"2. Update record \n"
         <<"3. Lookup record \n"
         <<"4. Quit \n"
         <<"Selection ? ";
     cin>>*p;
}
Last edited on
line 32 cin>>y; does not remove the end of line character from the input stream. Consequently, the call to cin.getline on line 34 reads everything left on the line and discards the end of line character. For example, if in response to cin>>y; you enter 5, then the 'Enter' key, two characters are inserted into the input stream buffer: '5' and '\n'. cin>>y; removes the '5' and stores it in the variable y, but the end of line character '\n' is still in the buffer. The cin.getline call on line 34 reads and stores everything left on the line up to the '\n' character. The '\n' character is read and discarded. But since there was nothing left on the line except the '\n' character, kbccstudent[y].name is set to an empty string.

To fix the problem you need to discard the rest of the line left in the input stream buffer following the call to cin>>y;. One common way to do that is:

std::cin.ignore( std::numeric_limits< std::streamsize >::max(), '\n' );

Add the above line right after cin>>y; or at least before the call to cin.getline.

You will need to add the line #include <limits> up with your other includes.

See http://cplusplus.com/reference/iostream/istream/ignore/ for information on ignore().
Topic archived. No new replies allowed.