Hi Gurus, I have an assignment am doing. The user should enter details of 5 students and return results on the screen.I have the code below and its return 1 error. Can someone kindly help me.With regards

#include<iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
typedef struct
{
string name;
int age;
int i;
}
Student;
Student students[5];
for (i = 0 ; i < 5 ; i++)
{
cout << "Enter Name of student " << i+1 << ": " ;
cin >> students.name ;

cout << "Enter age: " ;
cin >> students.age ;

}
cout << students.name << " " << students.age << "yrs" << endl;
return 0;
}

Dani AI

Generated

Two quick causes stopped the program and prevented the expected five-line output: the loop index ended up declared inside the struct (so the for loop had no visible i), and each student field was accessed without an array index (so you never filled or printed students[0]..students[4]). correctly pointed out moving the index out of the struct and running a separate print loop. Below is a concise, safe pattern that implements those fixes and also handles names with spaces and simple input validation.

Use a plain struct (or a std::vector of structs), declare the loop index in the for header (for (int i = 0; i < 5; ++i)), write into students[i].name / students[i].age, and then loop again to print each element. When mixing operator>> and std::getline, consume leftover newline (for example with std::ws or std::cin.ignore) so getline sees the real name. Validate numeric input to avoid leaving the stream in a failed state.

#include <iostream>
#include <string>
#include <vector>
#include <limits>

struct Student { std::string name; int age = 0; };

int main() {
    std::vector<Student> students;
    students.reserve(5);

    for (int i = 0; i < 5; ++i) {
        Student s;
        std::cout << "Enter Name of student " << (i + 1) << ": ";
        std::getline(std::cin >> std::ws, s.name);

        std::cout << "Enter age: ";
        while (!(std::cin >> s.age)) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Invalid age, try again: ";
        }
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        students.push_back(std::move(s));
    }

    for (const auto& st : students)
        std::cout << st.name << " " << st.age << "yrs\n";

    return 0;
}

Notes: use students[i] when storing/reading individual records, never access students without an index; avoid declaring loop counters as struct members; and prefer std::vector when the count may change. This will produce the exact list format requested.

Recommended Answers

All 4 Replies

int i should be outside struct definition. Still post what error you are getting

Yes thats where the error was coming from but doesnt give me the desired results
I wanted output like this;
John 22yrs
Peter 12yrs
mike 11yrs
joyce 10yrs
cate 11yrs

Kindly assist

Post your latest code along with error (if any) or output. We already know your expected output now. Post what you are getting

cout << students.name << " " << students.age << "yrs" << endl;

Put this in a new loop from 0 to 4 again. What you have done will not print since i will be incremented to 5 and there is no element at 5th position in array.You will again have to loop through entire array from the beginning and print element one by one

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.