gpt4 book ai didi

C++ If语句将不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:13:48 27 4
gpt4 key购买 nike

//Writing a letter

#include <iostream>
using namespace std;

int main() {
string first_name; //Name of addressee
string friend_name; //Name of a friend top be mentioned in the letter
char friend_sex, m, f; //variable for gender of friend

friend_sex = 0;

cout << "\nEnter the name of the person you want to write to: ";
cin >> first_name;

cout << "Enter the name of a friend: ";
cin >> friend_name;

cout << "Enter friend's sex(m/f): "; //Enter m or f for friend
cin >> friend_sex; //Place m or f into friend_sex

cout << "\nDear " << first_name << ",\n\n"
<< " How are you? I am fine. I miss you!blahhhhhhhhhhhhhhhh.\n"
<< "blahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.\n"
<< "Have you seen " << friend_name << " lately? ";

//braces only necessary if there are more than one statement in the if function
if(friend_sex == m) {
cout << "If you see " << friend_name << ", please ask him to call me.";
} //If friend is male, output this
if(friend_sex == f) {
cout << "If you see " << friend_name << ", please ask her to call me.";
} //If friend is female, output this

return(0);
}

实际结果是这样的:

Enter the name of the person you want to write to: MOM

Enter the name of a friend: DAD

Enter friend's sex(m/f): m

Dear MOM,

How are you? I am fine. I miss you! blahhhhhhhhhhhhhhhhh.
blahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.
Have you seen DAD lately?

这个程序正在模拟一封简短的信件。输出一个单词 block 很容易,但是当我想在字母中放置一些条件时,我就遇到了麻烦。即使我在程序询问我时输入了 friend_sex (m/f),if 语句的输出也没有实现。为什么?

最佳答案

您正在针对未初始化的字符变量 m 测试 friend_sex。您可能想要针对文字值 'm' 进行测试。这就像有一个名为 seven 的整数变量,并期望它保存值 7

关于C++ If语句将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9234470/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com