-1

I used structure for multiple entry of students. But I am getting error in the following code:

#include <iostream>
using namespace std;
struct Student{
   char stuName[30];
   int stuRollNo;
   int stuAge;
};

void printStudentInfo(struct Student s[]);

int main(){
    int i;
    for(i=0;i<3;i++){
   Student s[i];
   cout<<"Enter Student Name: ";
   cin.getline(s[i].stuName, 30);
   cout<<"Enter Student Roll No: ";
   cin>>s[i].stuRollNo;
   cout<<"Enter Student Age: ";
   cin>>s[i].stuAge;
   printStudentInfo(s[i]);}
   return 0;
}

void printStudentInfo(struct Student s[i]){
   cout<<"Student Record:"<<endl;
   cout<<"Name: "<<s[i].stuName<<endl;
   cout<<"Roll No: "<<s[i].stuRollNo<<endl;
   cout<<"Age: "<<s[i].stuAge;
}

error:

main.cpp:29:25: error: cannot convert ‘Student’ to ‘Student*’ for argument ‘1’ to ‘void printStudentInfo(Student*)’
    printStudentInfo(s[i]);} 
                         ^
main.cpp: At global scope:
main.cpp:33:40: error: ‘i’ was not declared in this scope
 void printStudentInfo(struct Student s[i]){ 
                                        ^
main.cpp: In function ‘void printStudentInfo(...)’:
main.cpp:35:20: error: ‘s’ was not declared in this scope
    cout<<"Name: "<<s[i].stuName<<endl; 
                    ^
main.cpp:35:22: error: ‘i’ was not declared in this scope
    cout<<"Name: "<<s[i].stuName<<endl;

I get the point that I need to define i in the function but how will it take values accordingly. If i take a for loop in the function the values will get printed more than the time required. If i do not take an array it will give a segmentation fault.

Please help me with this code. I am new to c++.

2 Answers2

1

There are several things incorrect. When using C++ please consider adopting the correct objects such as std::string for storing the student name. Also, differently from C, we don't have to specify the struct keyword when passing as a function parameter.

Also, the way you were passing the structure to the printStudentInfo() function was wrong. Additionally, I suggest against using namespace std if possible.

Here follows a minimal correction of your code (please note that it can be further improved, I just got it barely working):

#include <iostream>
#include <string>

struct Student{
   std::string stuName;
   int stuRollNo;
   int stuAge;
};

void printStudentInfo(const Student& s);

int main()
{
    Student s[3];
    for(int i=0;i<3;i++)
    {
        std::cout<<"Enter Student Name: ";

        if (i) std::cin.ignore();
        std::getline(std::cin, s[i].stuName);

        std::cout<<"Enter Student Roll No: ";
        std::cin>>s[i].stuRollNo;

        std::cout<<"Enter Student Age: ";
        std::cin>>s[i].stuAge;

        printStudentInfo(s[i]);
   }
   return 0;
}

void printStudentInfo(const Student& s){
   std::cout<<"Student Record:"<<std::endl;
   std::cout<<"Name: "<<s.stuName<<std::endl;
   std::cout<<"Roll No: "<<s.stuRollNo<<std::endl;
   std::cout<<"Age: "<<s.stuAge<<std::endl;
}
André Caceres
  • 719
  • 4
  • 15
  • Good answer! You can explain `if (i) std::cin.ignore();` a bit in detail or add some reference for the OP. – Azeem Feb 16 '20 at 08:04
0

A few points:

  • First of all, the code is not indented. Use your IDEs code beautify options or use some online to indent your code. Example: https://codebeautify.org/cpp-formatter-beautifier

  • For learning purposes using namespace std; is fine. But, try to avoid it in a more serious code. Read this thread for more information: Why is "using namespace std;" considered bad practice?

  • In the Student type, the members are prefixed with stu which is redundant because they're already part of a properly named type i.e. Student and would be referenced in the code by meaningful named objects. Simple and direct member names such as name, rollno, and age would suffice.

  • The member for student name is a char array with a magic number i.e. 30. Try to learn and use std::string which is more safe and straightforward. And, always try to avoid magic numbers. Use proper names for magic numbers using const, constexpr (if C++11 or above), or #define macro replacements according to your requirements. For example, #define NAME_LENGTH 30.

  • In C++, you don't need to use prefix struct everywhere reference the type except the definition or a forward declaration. So, void printStudentInfo(struct Student s[]); would be void printStudentInfo(Student s[]);.

  • The function printStudentInfo is supposed to print only one object of Student type so the subscript operator with argument s[] is redundant. The correct type for this argument in this context would be Student s i.e. void printStudentInfo(Stdudent s). There are multiple ways to pass arguments to a function in C++ such as pass-by-value and pass-by-reference. In your context, pass-by-reference makes more sense i.e. void printStudentInfo(const Student& s). Read this relevant thread on the subject: What's the difference between passing by reference vs. passing by value?

  • Again, a magic number for array size. Use a constant with a proper name such as MAX_STUDENTS.

  • In C++, declare and define a loop control variable inside loop: for ( int i = 0; ... )

  • Declare Student s[i]; outside the loop. In loop, it means that it is being created every time the loop iterates with a new variable size i.e. 0, 1, 2. That is not intended here. Move it outside with a constant as its size e.g. Student s[MAX_STUDENTS];. A relevant thread: Why aren't variable-length arrays part of the C++ standard?. In the future, when you're familiar with STL (Standard Template Library), use std::vector.

  • Miscellaneous (you can study as you go along): array initialization, std::endl vs \n, unsigned vs signed (for age, rollno, ...), std::size_t, etc.

Here's a working example (live):

#include <iostream>
#include <string>

struct Student
{
    std::string name;
    int         rollno;
    int         age;
};

void printStudentInfo( const Student& s );

int main()
{
    const int MAX_STUDENTS = 3;
    Student s[MAX_STUDENTS];

    for ( int i = 0; i < MAX_STUDENTS; ++i )
    {
        std::cout << "Enter Student Name: ";
        std::getline( std::cin, s[i].name );

        std::cout << "Enter Student Roll No: ";
        std::cin >> s[i].rollno;

        std::cout << "Enter Student Age: ";
        std::cin >> s[i].age;

        printStudentInfo( s[i] );
    }

    return 0;
}

void printStudentInfo( const Student& s )
{
    std::cout << "Student Record:\n"
              << "Name    : " << s.name << '\n'
              << "Roll No : " << s.rollno << '\n'
              << "Age     : " << s.age << '\n';
}
Azeem
  • 11,148
  • 4
  • 27
  • 40