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

时间:2017-01-24 15:46:29

标签: c++

我是一名开始学习编码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;

 }`

3 个答案:

答案 0 :(得分:5)

让我们首先将此问题简化为其基本要素。

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与<{1}}中声明的变量E nothing 没有关系。因此,当你写:

main

您正在使用未初始化的变量。解决方案是将 extroversion+=E; 重写为:

main

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

答案 1 :(得分:2)

您在main中使用卡车装载的未初始化变量。您正在从Q次调用所使用的各种cout函数返回值,但您未设置EAC N中的Omain。形式上,这意味着程序的行为是 undefined

替换

cout << Q1() << endl;

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

依此类推,一切都会好起来的。请使用逐行调试器自行查看。

答案 2 :(得分:1)

让我们从头开始(我认为您可能想要学习C / C ++,也许是通过运行许多在线C ++教程之一)。我认为你的主要关注点应该是以下(没有特定的顺序):

初始化:int,char等原始数据类型称为Plain Old Datatypes( POD )。在您在代码中使用的所有情况下,它们都没有初始化,遵循规则&#34;不为您不使用的内容付费。&#34;有关详细信息,请参阅What are POD types in C++?

范围:变量存在于由声明它们的位置定义的程序区域内。例如,在main()中,您声明int E;(不初始化它,因此它只是获取该位置内存中的任何值)。 main() E 的范围从main()末尾的结尾处开始范围是函数 main )。现在我们转到您声明int Q1()的函数int E = 0;。在 函数中, E 在声明它的地方出现,然后在 Q1结束时不存在(其范围是函数 Q1 )。当您在 Q1 中设置 E 时,当您退出 Q1 时,该变量(及其值)消失,因此当您添加 E ,您要添加 main 中声明的 E ,它只是一个随机值。因此,您在 main

中使用的 E
 extroversion+=E;

与您在 Q1中设置的 E 不一样

 cin >> E;

它们是不同的变量。

有关详细信息,请参阅https://www.tutorialspoint.com/cplusplus/cpp_variable_scope.htm

功能:您正在调用返回值的函数,但您不会使用该返回值。我认为你对变量范围和函数概念的误解是你困难的根源。 Frex,在Q1()中你返回 E 的值,这确实是你想要的。你只是不知道如何使用它。在这种情况下,我会替换

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

extroversion = Q1();

有关详细信息,请参阅http://www.cplusplus.com/doc/tutorial/functions/

上面的代码存在许多问题,我认为可以通过访问我引用的一些在线教程来纠正这些问题。我可以重写你的代码,但我认为(1)它应该与我在这里给出的建议一致,(2)如果你自己重写它,你会学到更多! :-)在我看来,你可能只是一直在添加东西,以便让你的代码编译,这是不必要的。我可能会建议你浏览每一行,并确保你理解它为什么存在(以及为什么它在哪里)。