我需要C++结构的帮助[关闭]

问题描述 投票:-3回答:2

该程序是取一个结构名为 "st "的对象,将取年龄,然后再取名和姓,比标准的 "st "要好。

(main.cpp:33:10: error: invalid use of non-static member function 'void Student::age(int)')

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

    struct Student{
    static string f,l;
    static int  a,s;
    void age(int ag);
    void first_name(string fi)
    {
        f=fi;
    }
    void last_name(string la)
    {
        l=la;
    }
    void standard(int st)
    {
        s=st;
    }
};
void Student :: age( int ag)
{
    a=ag;
}

int main() {
     Student st;
     cin >> st.age >> st.first_name >> st.last_name >> st.standard;
     cout << st.age << " " << st.first_name << " " << st.last_name << " " << st.standard;

    return 0;
}
c++ structure
2个回答
2
投票

你可以取一个结构名为 "st "的对象,会先取年龄,再取名和姓,比标准的(main.cpp:33:10:错误:无效使用非静态成员函数'void Student::age(...) 0-9个字 变成一个字符串数组。

int a, b;
string c[]={"", "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
cin >> a >> b;
for(int i = a; i <= b; i++)
    cout << ((i <= 9) ? c[i] : ((i % 2 == 0) ? "even" : "odd")) << endl;

如果中间输入 1-9 然后它将检查和 印字 否则,如果只是检查 完好无损.


1
投票

我想这是你想得到的。

#include <iostream>

void printText(int);

int main(void) {
    int x = 0, y = 0;

    std::cout << "Enter two values: ";
    std::cin >> x >> y;

    for (int i = x; i <= y; i++) {
        if (i >= 10) {
            if (i % 2 == 0) std::cout << "even";
            else std::cout << "odd";

            std::cout << std::endl;
        } else
            printText(i);
    }

    return 0;
}

void printText(int num) {
    std::string number[] =
        {"zero", "one", "two", "three", "four", "five",
        "six" , "seven", "eight", "nine"};

    std::cout << number[num] << std::endl;
}

该程序接受直到 9 中定义的文本,并将其打印为数字。printText 功能。当这个数字达到大于或等于 10它开始打印数字是偶数还是奇数,而不是打印数字。

输出示例。

Enter two values: 7 12
seven
eight
nine
even
odd
even
© www.soinside.com 2019 - 2024. All rights reserved.