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
Click For Summary
SUMMARY

The discussion addresses an issue in a C++ program where the student name is not printing correctly during a lookup operation. The program utilizes a struct named student to store student records, including an idnum, name, and gender. The problem arises because the program does not properly handle input for the name field, particularly when using cin.getline() after cin operations, leading to unexpected behavior. The solution involves ensuring that students are added before attempting to look them up.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with C++ structures and arrays
  • Knowledge of input/output operations in C++
  • Experience with control flow statements in C++
NEXT STEPS
  • Review C++ input handling, specifically the differences between cin and cin.getline()
  • Learn about struct initialization and memory management in C++
  • Explore debugging techniques for C++ programs to identify input/output issues
  • Investigate best practices for user input validation in C++ applications
USEFUL FOR

C++ developers, computer science students, and anyone involved in building console applications that require user input and data management.

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

 
Physics news 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