Why Is the Char Not Printing in My C++ Program?

  • Context: Comp Sci 
  • Thread starter Thread starter tebes
  • Start date Start date
  • Tags Tags
    Printing
tebes
Messages
38
Reaction score
0

Homework Statement


After I typed in the name, and tested it by running " lookup " under "option". But the "name:" showed nothing.
Code:
#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] = ' '; 
             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);
     cin.ignore(name_length,'\n');
     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);
     cin.ignore(name_length,'\n');
     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;
}


Homework Equations





The Attempt at a Solution

 
on Phys.org
Have you entered any students yet? Before you can look up an individual student, you must first have added some students (menu choice 1).
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K