Objective : Write a C++ program to input and output the basic details of a Student like Name and Age, Gender, Roll Number, etc.
Program Code:
#include <iostream>
#include <string>
using namespace std;
int main() {
// Declare variables to store student details
string name;
int age;
char gender;
string rollNumber;
// Input student details
cout << "Enter student details:" << endl;
cout << "Name: ";
getline(cin, name); // Use getline to allow spaces in the name
cout << "Age: ";
cin >> age;
cout << "Gender (M/F): ";
cin >> gender;
// Clear input buffer before getting string input
cin.ignore();
cout << "Roll Number: ";
getline(cin, rollNumber);
// Output student details
cout << "\nStudent Details:" << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Gender: " << (gender == 'M' || gender == 'm' ? "Male" : "Female") << endl;
cout << "Roll Number: " << rollNumber << endl;
return 0;
}
No comments:
Post a Comment