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.

closed Just a beginner here, Need help with what I am doing wrong here

+5 votes
asked Oct 22, 2021 by Sohaib Mubashir (200 points)
closed Oct 25, 2021 by Admin
int main() {
   class human {
       
       int age;
       int height;
       std::string location;
      
      public:
      int set_age(int new_age) {
          age = new_age;
          return age;
      }
      int set_height(int new_height) {
          height = new_height;
          return height;
   }
   std::string set_location(std::string new_location) {
       location = new_location;
       return location;
       
   }
   
}

human hello;
hello.set_age(25);
hello.set_height(15);
hello.set_location("PK");
std::cout << hello.age << " " << hello.height << " " << hello.location << " \n";
}

I keep getting the error "initializer expected before hello" . I might be missing an important point here, so would really help if someone was to point out what's wrong. (It's C++)

This code may be inefficient for many pros out there, but I just learnt how to code and want to play around with it for a bit to get more familiar to it.
closed with the note: answered

4 Answers

0 votes
answered Oct 23, 2021 by Peter Minarik (45,000 points)
selected Oct 23, 2021 by Sohaib Mubashir
 
Best answer

You need a semicolon after the declaration of class human (unless you want to right away specify what variables would be of this type).

It's not an error, but classes usually are spelt according to upper CamelCase (or PascalCase).

When you try to print out the age, height and location they are private and can be accessed only within the class. You could make them public, but in that case, there's no point having the setter methods. Instead, you could create getter methods (alongside the setter methods).

Not an error, but again very strange to have a setter method return any value.

One more thing: you declare your class within the main() function. So the type class Human is only visible within the main function and not outside of it. If you are not happy with this, you should move the class declaration outside of your main() function.

You should use the #include statements to have references to standard input/output manipulations as well as have access to strings.

At the end of your main() function, you should return a value, since it is not a void function. Typically 0 indicates everything was fine.

I'd fix your code as below:

#include <iostream>
#include <string>

int main()
{
    class Human
    {
    private:
        int age;
        int height;
        std::string location;

    public:
        void set_age(int new_age) { age = new_age; }
        int get_age() const { return age; }
        
        void set_height(int new_height) { height = new_height; }
        int get_height() const { return height; }
        
        void set_location(std::string new_location) { location = new_location; }
        std::string get_location() const { return location; }
    };

    Human hello;
    hello.set_age(25);
    hello.set_height(15);
    hello.set_location("PK");
    std::cout << hello.get_age() << " " << hello.get_height() << " " << hello.get_location() << std::endl;
    return 0;
}
commented Oct 23, 2021 by Sohaib Mubashir (200 points)
Thank you for your answer. It worked! I really appreciate you along with the other people for taking their time to help me out. :)
commented Oct 23, 2021 by Peter Minarik (45,000 points)
My pleasure.
0 votes
answered Oct 22, 2021 by Luis Antonio Molina Yampa (140 points)
You have to put ";" after the last "}" of the class. Remember, class atributes are private, you can't acces directly, put them "public"
commented Oct 23, 2021 by Sohaib Mubashir (200 points)
Got it, Thanks!
0 votes
answered Oct 22, 2021 by Alibek (160 points)
Int age and height  are not accepted,
Int takes a numeric  value
0 votes
answered Oct 22, 2021 by Tarun Kumar Tarlana (350 points)
class should end with semicolon;

class example

{
 //

};//semicolon

before the line human hello add semi colon
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.
...