Answer to Question #202736 in C++ for Laiba Kanwal

Question #202736

Write a C++ program to input data for 10 students and each student has following data to be stored.

• Name of Student

• Age of Student.

• Phone No.

• Father Name.

Also display the entered data


1
Expert's answer
2021-06-04T07:14:06-0400
#include <iostream>
#include <string>
#include <vector>

struct Student
{
    std::string name;
    int         age;
    std::string phone;
    std::string nameFather;
};

int main()
{
    const int STUDENTS_COUNT = 10;
    std::vector<Student> students;

    for(int i = 0; i < STUDENTS_COUNT; ++i)
    {
        Student tmp;
        std::cout << "Enter the name of Student: ";
        std::getline(std::cin, tmp.name);

        std::cout << "Enter the age of Student: ";
        std::cin >> tmp.age;
        if(!std::cin)
        {
            std::cout << "Bad input!\n";
            return 1;
        }
        std::string tmpClearBufferLine;
        std::getline(std::cin, tmpClearBufferLine);
        
        std::cout << "Enter the Phone No. of Student: ";
        std::getline(std::cin, tmp.phone);

        std::cout << "Enter the father name of Student: ";
        std::getline(std::cin, tmp.nameFather);
        
        students.push_back(tmp);

        std::cout << "\n";
    }

    std::cout << "-----------------------------------------------------------------------\n";
    
    for(size_t i = 0; i < students.size(); ++i)
    {
        std::cout << "Student #" << (i + 1) << ":\n";
        std::cout << "\tname:        " << students[i].name << "\n";
        std::cout << "\tage:         " << students[i].age << "\n";
        std::cout << "\tphone:       " << students[i].phone << "\n";
        std::cout << "\tfather name: " << students[i].nameFather << "\n";
        std::cout << "\n";
    }
    
    return 0;    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment