gpt4 book ai didi

c++ - 如何将变量添加到其当前值,以及随后的用户输入?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:58:56 25 4
gpt4 key购买 nike

我正在使用 switch 语句来存储用户输入的值。如果用户选择了某种情况,他们会询问一个数字并将该值存储在一个变量中。如果用户选择了相同的案例怎么办,您如何将该值添加到当前值。我试图将第一个值存储在 activity_count 中,然后将该值保存到 activity_counts。然后添加activity_counts 但是这个值一直在变化,随着循环不断更新。我知道我的逻辑是错误的,有人可以让我更好地了解我需要做什么吗?

这是我的代码

for(daily_count = 0; daily_count < patient_count; daily_count++){
int options;
int activity_count;
int activity_counts;

cout << "1. Wash_Hands\n";
cout << "9. Exit\n";

cout << "\n";
cout << "Choose a number with a description, which best decribes patient's obsessive activities: ";
cin >> options;
cout << "\n";


switch(options){
case 1:
cout << "How much times did " << patient_name << " wash his hair:";
cin >> activity_count;
if(activity_count != 0)
activity_counts = activity_count;
activity_counts += activity_counts;
cout << activity_counts << "\n\n";
break;
default:
exit(0);
}
}

最佳答案

你的代码还有更多问题:

       if (activity_count != 0)
activity_counts = activity_count;
activity_counts += activity_counts;
cout << activity_counts << "\n\n";
break;

只有 if 之后的第一个语句被条件触及,其他总是执行(可能缺少大括号 {} )

       if (activity_count != 0)

这似乎没用 - 加零有什么问题?

            activity_counts = activity_count;

这有效地覆盖 activity_counts - 这是你的意思吗?

            activity_counts += activity_counts;

通过向其添加相同的值,它加倍 activity_counts - 是这个意思吗?

So, my friend, your code probably needs at least these 2 changes:

更改 1:Inicialize activity_counts0move for 循环前面的声明 - 使用

int activity_counts = 0;

for (daily_count = 0; daily_count < patient_count; daily_count++){
int options;
int activity_count;

未初始化声明代替这部分代码(产生一个 随机值 for 循环中的声明变量):

for (daily_count = 0; daily_count < patient_count; daily_count++){
int options;
int activity_count;
int activity_counts;

改变 2:代替

       if (activity_count != 0)
activity_counts = activity_count;
activity_counts += activity_counts;

简单使用

            activity_counts += activity_count;

(注意左边是复数,右边是单数。)

关于c++ - 如何将变量添加到其当前值,以及随后的用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40332189/

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