Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Please solve errors of the program

+1 vote
asked May 1, 2021 by Jahanvi Giriya (240 points)
#include <iostream>
using namespace std;  
int main()
{  
    int age,idnum ;
    char nameofp[20],city[20],education[20], a, b;
   
    void getdata();
    void printdata();
   
    getdata();
    printdata();
   
   
    return 0;
}

 void getdata(){
     int age,idnum ;
    char nameofp[20],city[20],education[20];
        cout<<"Enter id of person:: ";
        cin>>idnum;
        cout<<"Enter name of person:: ";
        cin>>nameofp;
        cout<<"Enter city of person:: ";
        cin>>city;
        cout<<"Enter age of person:: ";
        cin>>age;
        cout<<"Enter education of person:: ";
        cin>>education;
       
       
    }
    void printdata(){
        int age,idnum ;
    char nameofp[20],city[20],education[20];
       cout<<"\n"<<"\n"<<"\n"<<"Id of person:: "<<idnum<<"\n";
        cout<<"Name of person:: "<<nameofp<<"\n";
        cout<<"City of person:: "<<city<<"\n";
        cout<<"Age of person:: "<<age<<"\n";
        cout<<"Education of person:: "<<education;
    }

1 Answer

0 votes
answered May 5, 2021 by Peter Minarik (63,180 points)
edited May 5, 2021 by Peter Minarik

Your variables are local to your functions. Your printdata() has variables that are not initialized and contain memory garbage (leftovers from the execution of your getdata() function). No wonder what you see on the screen doesn't make sense. You need the data (the variables) to be accessible for both of your functions.

Since you use C++, your best bet would be to organise your data and functions into a class, so every member of the class can access the same fields (age, name, etc).

One more thing to consider is that you may run out of buffer. 20 characters (19 as you need to reserve one for the terminating zero) may not be enough for the name or city or education. Using std::string would be a better choice.

Here's my code with the above suggested changes.

#include <iostream>

class Person
{
private:
    int age;
    int id;
    std::string name;
    std::string city;
    std::string education;
    
public:
    void GetData()
    {
        std::cout << "Enter id of person: ";
        std::cin >> id;
        std::cout << "Enter name of person: ";
        std::cin >> name;
        std::cout << "Enter city of person: ";
        std::cin >> city;
        std::cout << "Enter age of person: ";
        std::cin >> age;
        std::cout << "Enter education of person: ";
        std::cin >> education;
    }

    void PrintData() const
    {
        std::cout << "Id of person: " << id << std::endl;
        std::cout << "Name of person: " << name << std::endl;
        std::cout << "City of person: " << city << std::endl;
        std::cout << "Age of person: " << age << std::endl;
        std::cout << "Education of person: " << education << std::endl;
    }
};

int main()
{  
    Person person;
    person.GetData();
    std::cout << std::endl << "The collected data:" << std::endl << "-------------------" << std::endl;
    person.PrintData();

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...