我是一名开始学习编码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;
}`
Run Code Online (Sandbox Code Playgroud)
让我们首先将此问题简化为其基本要素.
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;
}
Run Code Online (Sandbox Code Playgroud)
问题是,该变量E已在第一季度宣布,有什么做的变量E您在声明main.因此,当你写:
extroversion+=E;
Run Code Online (Sandbox Code Playgroud)
您正在使用未初始化的变量.解决方案是重写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;
}
Run Code Online (Sandbox Code Playgroud)
请注意:"减少"问题是你应该首先发布的 - 我们不需要看到大量的文本,我们当然不需要看到你做同样的事情五次.删除措辞(并检查您是否仍有问题),然后发布减少的问题.
| 归档时间: |
|
| 查看次数: |
99 次 |
| 最近记录: |