使用 类 但无法显示答案。
Working with classes but can't display the answer.
这道题本来不想写的,但是好像不能输出名字。这是我 class 作业的一部分,也是作业中最难的部分,但我的老师说我们可以寻求帮助并向其他人请教,只要我们真正学习并理解我们在做什么,并且我们为什么这样做。所以我问这个是为了帮助我理解为什么我的程序不会输出人名。
我想做的是;输入一个包含大写和小写字母的姓名 "lEonArDo DA vINCi",以及他们出生的日期“1520”,然后输入将姓名变为小写并仅将单词中的第一个字母大写的代码,最后输出那个名字。
我坚持的是输出名称。我只是不知道我做错了什么。我已经在我的 class 中问过其他人,但他们仍然在第一部分,或者他们以某种方式停留在问题上。
class Person{ //Can't change from here.
public:
Person(string n, int year) : name(n), yearOfBirth(year){}
string getName();
friend ostream& operator <<(ostream&, const Person&);
private:
string name; int yearOfBirth;
}; // to here.
string Person::getName() //output the name
{
// I think the output should be here but it doesn't work.
// I have also switched the code in the operator into the getName put it still doesn't work.
return name;
}
ostream& operator << (ostream& output, const Person& P){ //turns everything into lowercase.
locale loc;
string temp = ""; //well be asigning the lowercase letters here; one at a time.
int ii = 0;
for (string::size_type i = 0; i < P.name.length(); i++) { //reads the input one at a time.
if ((ii == 0) || (ii >= 1 && ((P.name[i - 1]) == char(32)))) {(toupper(P.name[i], loc), (temp += toupper(P.name[i], loc))); }//if it was lowercase and the first letter, it turns it into uppercase and assigns it to temp.
else (tolower(P.name[i], loc), (temp += tolower(P.name[i], loc))); //if it was uppercase it turns it into lowercase and assigns it to temp.
ii++;
} //name = temp; //assigns the full lowercase word back to input01.*/
output << temp << " was born in " << P.yearOfBirth;
return output;
}
int main(){
Person::Person("lEonArDo DA vINCi", 1520);
// I have tried putting a output statement here as well
return 0;
}
所以,我的问题是;我应该如何输出名称? (即 cout。)我需要将输出语句放在哪里? (在 getName 或 main 函数中。)即使你只能告诉我一个模糊的答案,它应该在哪里,也会对我有很大帮助。
很抱歉问了一个愚蠢的问题,浪费了您的时间。
Person::Person("lEonArDo DA vINCi", 1520);
不造人。您想要的更多是
Person leonardo("lEonArDo DA vINCi", 1520);
这会生成一个名为 leonardo
的 Person
类型的变量,并使用 "lEonArDo DA vINCi", 1520
对其进行初始化。然后可以使用 leonardo
访问此 Person
。例如,获取并打印名称
std::cout << leonardo.getName() << std::endl;
或者,由于您已经有一个 << 重载,
std::cout << leonardo <<std::endl;
使用运算符<<函数。
您可能希望将大小写规则从 << 运算符移至 Person
构造函数。通过这种方式,您可以存储正确的版本并在 << 运算符中仅打印出正确的版本,而不是存储破损的 "lEonArDo DA vINCi"。这使得 << 运算符更简单,并且只需要在构造时 运行 一次而不是每次打印时进行更正。
这道题本来不想写的,但是好像不能输出名字。这是我 class 作业的一部分,也是作业中最难的部分,但我的老师说我们可以寻求帮助并向其他人请教,只要我们真正学习并理解我们在做什么,并且我们为什么这样做。所以我问这个是为了帮助我理解为什么我的程序不会输出人名。
我想做的是;输入一个包含大写和小写字母的姓名 "lEonArDo DA vINCi",以及他们出生的日期“1520”,然后输入将姓名变为小写并仅将单词中的第一个字母大写的代码,最后输出那个名字。
我坚持的是输出名称。我只是不知道我做错了什么。我已经在我的 class 中问过其他人,但他们仍然在第一部分,或者他们以某种方式停留在问题上。
class Person{ //Can't change from here.
public:
Person(string n, int year) : name(n), yearOfBirth(year){}
string getName();
friend ostream& operator <<(ostream&, const Person&);
private:
string name; int yearOfBirth;
}; // to here.
string Person::getName() //output the name
{
// I think the output should be here but it doesn't work.
// I have also switched the code in the operator into the getName put it still doesn't work.
return name;
}
ostream& operator << (ostream& output, const Person& P){ //turns everything into lowercase.
locale loc;
string temp = ""; //well be asigning the lowercase letters here; one at a time.
int ii = 0;
for (string::size_type i = 0; i < P.name.length(); i++) { //reads the input one at a time.
if ((ii == 0) || (ii >= 1 && ((P.name[i - 1]) == char(32)))) {(toupper(P.name[i], loc), (temp += toupper(P.name[i], loc))); }//if it was lowercase and the first letter, it turns it into uppercase and assigns it to temp.
else (tolower(P.name[i], loc), (temp += tolower(P.name[i], loc))); //if it was uppercase it turns it into lowercase and assigns it to temp.
ii++;
} //name = temp; //assigns the full lowercase word back to input01.*/
output << temp << " was born in " << P.yearOfBirth;
return output;
}
int main(){
Person::Person("lEonArDo DA vINCi", 1520);
// I have tried putting a output statement here as well
return 0;
}
所以,我的问题是;我应该如何输出名称? (即 cout。)我需要将输出语句放在哪里? (在 getName 或 main 函数中。)即使你只能告诉我一个模糊的答案,它应该在哪里,也会对我有很大帮助。 很抱歉问了一个愚蠢的问题,浪费了您的时间。
Person::Person("lEonArDo DA vINCi", 1520);
不造人。您想要的更多是
Person leonardo("lEonArDo DA vINCi", 1520);
这会生成一个名为 leonardo
的 Person
类型的变量,并使用 "lEonArDo DA vINCi", 1520
对其进行初始化。然后可以使用 leonardo
访问此 Person
。例如,获取并打印名称
std::cout << leonardo.getName() << std::endl;
或者,由于您已经有一个 << 重载,
std::cout << leonardo <<std::endl;
使用运算符<<函数。
您可能希望将大小写规则从 << 运算符移至 Person
构造函数。通过这种方式,您可以存储正确的版本并在 << 运算符中仅打印出正确的版本,而不是存储破损的 "lEonArDo DA vINCi"。这使得 << 运算符更简单,并且只需要在构造时 运行 一次而不是每次打印时进行更正。