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

  • Comp Sci
  • Thread starter tebes
  • Start date
  • Tags
    Printing
In summary, the program allows for the addition, update, and lookup of student records in a database. However, before looking up a student record, the user must first add students to the database. The program also has a menu function to guide the user through the different options.
  • #1
tebes
39
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
  • #2
Have you entered any students yet? Before you can look up an individual student, you must first have added some students (menu choice 1).
 

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

Why is my char variable not being printed out in C++?

There could be several reasons for this issue. One possibility is that the char variable is not being properly initialized or assigned a value. Another possibility is that there is an error in the syntax of the code that is preventing the char from being printed out. It is also possible that the char is being printed, but the output is not visible due to a formatting issue or the console window closing too quickly.

How can I debug my C++ code to figure out why the char is not printing out?

The best way to debug this issue is to use a debugger tool, such as the built-in debugger in an integrated development environment (IDE) or a standalone debugger. This will allow you to step through your code line by line and see the value of your char variable at each step. You can also use print statements or the console window to print out the value of the char at different points in your code to track down the issue.

Can I use cout to print out a char in C++?

Yes, you can use the cout statement to print out a char in C++. However, it is important to make sure that the char is properly formatted and that the correct syntax is used to print it out. For example, you may need to use the "endl" statement after your char variable to ensure that it is printed on a new line.

What is the difference between printing out a char and a string in C++?

In C++, a char is a single character, while a string is a sequence of characters. When printing out a char, you can use the "cout" statement and specify the char variable, while when printing out a string, you need to use the "cout" statement and specify the string variable with the "<<" operator. Additionally, strings can contain multiple characters and can be manipulated in various ways, while chars are limited to a single character and have more limited functionality.

Is there a specific format I need to use to print out a char in C++?

Yes, there are a few different ways to format a char for printing in C++. The most common is to use the "cout" statement and specify the char variable, but you can also use the "printf" statement or other formatting options. It is important to make sure that the syntax is correct and that any necessary formatting options, such as "endl" or "setw", are included to ensure the char is printed correctly.

Similar threads

Replies
7
Views
1K
Replies
6
Views
3K
Replies
3
Views
1K
Replies
8
Views
7K
Replies
14
Views
4K
Replies
13
Views
2K
Replies
7
Views
6K
Back
Top