-3

I created a struct and void function. I want to write out age and name inside of struct xyz with the void abc. But i didn't understand i'm getting an error on case 1-2 part

type name isn't allowed

#include <iostream>
using namespace std;

struct xyz {
    int age = 20;
    string name = "name";
};
void abc() {
    int num;
    cin >> num;
    switch (num) {
    case 1: cout << xyz.age << endl;
        return;
    case 2: cout << xyz.name << endl;
        return;
    }
}
int main()
{
    for(;;)
    abc();
}
user438383
  • 5,716
  • 8
  • 28
  • 43

4 Answers4

0

You'd have to do something like this -

#include <iostream>
using namespace std;

struct xyz {
    int age = 20;
    string name = "name";
};
void abc() {
    xyz myStruct;
    int num;
    cin >> num;
    switch (num) {
    case 1: cout << myStruct.age << endl;
        return;
    case 2: cout << myStruct.name << endl;
        return;
    }
}
int main()
{
    for(;;)
        abc();
}

Another option would be something like this -

#include <iostream>
using namespace std;

struct xyz {
    int age;
    string name;
};
void abc() {
    xyz myStruct{20, "name"};
    int num;
    cin >> num;
    switch (num) {
    case 1: cout << myStruct.age << endl;
        return;
    case 2: cout << myStruct.name << endl;
        return;
    }
}
int main()
{
    for(;;)
        abc();
}
alonkh2
  • 533
  • 2
  • 11
0

You don't have created an instance of your type i.e. struct xyz, lets create one named static_instance and fix your code to something close what you seem to have intended

#include <iostream>
#include <string>

struct xyz {
    int age = 20;
    std::string name = "name";
} static_instance;

void abc() {
    int num{};
    std::cin >> num;
    switch (num) {
        case 1:
            std::cout << static_instance.age << "\n";
            break;
        case 2:
            std::cout << static_instance.name << "\n";
            break;
    }
}

int main() {
    while (true) {
        abc();
    }
}

Be aware that you seem to lack many basic principles of C++, for first improvements, that function abc should either be a member method or take a reference to your struct. Also prefer "stack" or automatic storage variables i.e. instances before static i.e. global ones. Like

#include <iostream>
#include <string>

struct xyz {
    int age = 20;
    std::string name = "name";
};

void print(const xyz& instance) {
    int num{};
    std::cin >> num;
    switch (num) {
        case 1:
            std::cout << instance.age << "\n";
            break;
        case 2:
            std::cout << instance.name << "\n";
            break;
    }
}

int main() {
    xyz instance{};
    while (true) {
        print(instance);
    }
}
Superlokkus
  • 4,731
  • 1
  • 25
  • 57
0

Your statement

struct xyz {
    int age = 20;
    string name = "name";
};

defines a type named xyz, but it does not define an object of this type. To define an object able to hold the actual data, define a variable of type xyz:

// Definition of the type xyz
struct xyz {
    int age = 20;
    string name = "name";
};

void abc() {
    // define a variable myXYZ, representing an object of type xyz:
    xyz myXYZ;

    int num;
    cin >> num;
    switch (num) {
    case 1: cout << myXYZ.age << endl;
        return;
    case 2: cout << myXYZ.name << endl;
        return;
    }
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
0

Since you tagged this with c++, let's go ahead and write this code as C++.

So, the function that accesses the members of the struct will be a member function (which is the safest way to design your code, in the long term). We create an object foo, which is an instance of xyz, and repeatedly call its member function abc to get input and print the appropriate output.

Note that, because abc is a member function, it can access the member variables age and name directly, without needing to specify which object we're talking about.

Note, too, the syntax in main() that declares foo and calls its member function abc. The key difference from the original post is the existence of an object foo.

#include <iostream>

struct xyz {
    int age = 20;
    string name = "name";

    void abc() {
        int num;
        std::cin >> num;
        switch (num) {
        case 1: std::cout << age << "\n";
            return;
        case 2: std::cout << name << "\n";
            return;
        }
    }
};

int main()
{
    xyz foo;
    for(;;)
        foo.abc();
}
Tim Randall
  • 4,040
  • 1
  • 17
  • 39