gpt4 book ai didi

c++ - 从 void 子函数检索 int 值到 C++ 中的函数

转载 作者:太空狗 更新时间:2023-10-29 23:49:24 25 4
gpt4 key购买 nike

我是一名开始学习 C++ 编码的基础学生。我正在为我的大学作业做一个调查程序,在测试之后,我发现子函数的总和值不能正确地求和到主函数中的值。任何人的帮助!!!

代码如下:

#include <iostream>

using namespace std;

int Q1();
int Q2();
int Q3();
int Q4();
int Q5();

int main()
{
char select;
int E, A, C, N, O;
int extroversion=0, agreeableness=0, conscientiousness=0, neuroticism=0, opennesstoexperience=0;

cout << "This is a Self-Esteem Questionnaire." << endl;
cout << "\nInsert S to start the test (Q to Quit): ";
cin >> select;
select=toupper(select);

if (select=='S')
{

cout << "------------------INSTRUCTIONS-----------------" << endl;
cout << "For each statement 1-50 mark how much you agree" << endl;
cout << "with on the scale 1-5, where " << endl;
cout << "1=disagree, 2=slightly disagree, 3=neutral, " << endl;
cout << "4=slightly agree and 5=agree " << endl;
cout << "-----------------------------------------------" << endl;

cout << Q1() << endl;
extroversion+=E;

cout << Q2() << endl;
agreeableness+=A;

cout << Q3() << endl;
conscientiousness+=C;

cout << Q4() << endl;
neuroticism+=N;

cout << Q5() << endl;
opennesstoexperience+=O;

cout << extroversion << endl;
cout << agreeableness << endl;
cout << conscientiousness << endl;
cout << neuroticism << endl;
cout << opennesstoexperience << endl;

}

else
if(select=='Q')
{
cout << "Program quit!" << endl;
}
return 0;
}

int Q1()
{
int E=0;

cout << "I am the life of the party." << endl;
cout << "1=disagree, 2=slightly disagree, 3=neutral," << endl;
cout << "4=slightly agree and 5=agree" << endl;
cout << "\nRating: ";
cin >> E;

return E;

}

int Q2()
{
int A=0;

cout << "I feel little concern for others." << endl;
cout << "1=disagree, 2=slightly disagree, 3=neutral," << endl;
cout << "4=slightly agree and 5=agree" << endl;
cout << "\nRating: ";
cin >> A;

return A;

}

int Q3()
{
int C=0;

cout << "I am always prepared." << endl;
cout << "1=disagree, 2=slightly disagree, 3=neutral," << endl;
cout << "4=slightly agree and 5=agree" << endl;
cout << "\nRating: ";
cin >> C;

return C;

}

int Q4()
{
int N=0;

cout << "I get stressed out easily." << endl;
cout << "1=disagree, 2=slightly disagree, 3=neutral," << endl;
cout << "4=slightly agree and 5=agree" << endl;
cout << "\nRating: ";
cin >> N;

return N;

}

int Q5()
{
int O=0;

cout << "I have a rich vocabulary." << endl;
cout << "1=disagree, 2=slightly disagree, 3=neutral," << endl;
cout << "4=slightly agree and 5=agree" << endl;
cout << "\nRating: ";
cin >> O;

return O;

}`

最佳答案

让我们从简化这个问题开始。

int main()
{
int E;
int extroversion=0;

cout << Q1() << endl;
extroversion+=E;

cout << extroversion << endl;
return 0;
}

int Q1()
{
int E=0;
cout << "Give me a number" << endl;
cin >> E;
return E;
}

问题是您在 Q1 中声明的变量 E 与您在 主要。因此当你写:

    extroversion+=E;

您正在使用未初始化的变量。解决方案是将 main 重写为:

int main()
{
int extroversion=0;

int E = Q1(); // Capture the result of Q1 in E
cout << E << endl;// ... *then* print it.
extroversion+=E; // And now you can use the value of E in this function.

cout << extroversion << endl;
return 0;
}

请注意:“减少”问题是您首先应该发布的 - 我们不需要看到大量的文本,我们当然不需要看到您重复做同样的事情五次.删除冗长的文字(并检查问题是否仍然存在),然后发布减少的问题。

关于c++ - 从 void 子函数检索 int 值到 C++ 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41832390/

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