【问题标题】:Program won't compile on school computers but compiles perfectly on mine程序无法在学校计算机上编译,但在我的计算机上编译完美
【发布时间】:2015-10-16 01:08:39
【问题描述】:

所以我使用 CLion 为我的计算机科学课程编写代码。我在课堂上的机器上遇到了一个错误,这在我的个人机器、终端或 CLion 中没有发生。

ary0012@cse06:~/ProblemSet3$ g++ homework3.cpp
homework3.cpp: In function ‘double sum_mean(double)’:
homework3.cpp:111:65: error: cannot convert ‘std::basic_ostream<char>::__ostream_type {aka std::basic_ostream<char>}’ to ‘double’ in return
homework3.cpp:130:67: error: cannot convert ‘std::basic_ostream<char>::__ostream_type {aka std::basic_ostream<char>}’ to ‘double’ in return
homework3.cpp: In function ‘double min_max(double)’:
homework3.cpp:159:65: error: cannot convert ‘std::basic_ostream<char>::__ostream_type {aka std::basic_ostream<char>}’ to ‘double’ in return
homework3.cpp:179:65: error: cannot convert ‘std::basic_ostream<char>::__ostream_type {aka std::basic_ostream<char>}’ to ‘double’ in return
ary0012@cse06:~/ProblemSet3$

代码本身如下所示:

    /*
 ============================================================================
 Name        : Homework3.cpp
 Author      : Axel Yates (email address)
 Version     : 1.0
 Copyright   : 2015
 Description : 
 ============================================================================
 */

#include <iostream>
using namespace std;

void display_credentials();
//


void display_menu();
//


double sum_mean(double);
//


double min_max(double);
//


double selection;
//



int main()
{

    do {
        display_credentials();
        display_menu();

        cout << "Enter your selection: ";
        cin  >> selection;

        if (selection == 1) {
            sum_mean(selection);
        }
        else if (selection == 2) {
            sum_mean(selection);
        }
        else if (selection == 3) {
            min_max(selection);
        }
        else if (selection == 4) {
            min_max(selection);
        }
        else if (selection == 5)
            cout << "Thanks! Have a great day!" << endl;
        else if (selection > 5 || selection < 1)
            cout << "That's not a valid number, try again." << endl;

        }while(selection != 5);
    return EXIT_SUCCESS;

}

void display_credentials()
{
    cout << "+----------------------------------------------+" << endl;
    cout << "|       Computer Science and Engineering       |" << endl;
    cout << "|        CSCE 1030 - Computer Science I        |" << endl;
    cout << "|   Axel Yates  studID   email address here   |" << endl;
    cout << "+----------------------------------------------+" << endl;

    return;
}

void display_menu()
{
    cout << "***************** M E N U *****************" << endl;
    cout << "| 1. Sum  of numbers                      |" << endl;
    cout << "| 2. Mean of numbers                      |" << endl;
    cout << "| 3. Min  of numbers                      |" << endl;
    cout << "| 4. Max  of numbers                      |" << endl;
    cout << "| 5. Exit                                 |" << endl;
cout << "*******************************************" << endl;


return;
}

double sum_mean(double selection)
{
    if (selection == 1)
    {
        int quantity, counter = 0, num = 1;
        double num1 = 0, sum = 0;

        cout << "Enter the positive quantity of numbers to read in(SUM): ";
        cin >> quantity;
        while (counter < quantity)
        {
            counter++;
            cout << "Enter Number " << num++ << ": ";
            cin >> num1;
            sum = sum + num1;
        }

        return cout << "The sum of your numbers is: " << sum << endl;
    }

    else if (selection == 2)
    {
            double num1 = 0, temp = 0, sum = 0, mean = 0;
            int counter = 0, quantity, num = 1;

            cout << "Enter the positive quantity of numbers to read in(MEAN): ";
            cin >> quantity;
            while (counter < quantity)
            {
                counter++;
                cout << "Enter Number " << num++ << ": ";
                cin >> num1;
                sum = num1 + sum;
                mean = (sum)/2;
            }

        return cout << "The mean of your numbers is: " << mean << endl;
    }
    return 0;
}

double min_max(double selection)
{
    if (selection == 3)
    {
    int quantity;
    int counter = 0;
    int num = 1;
    double num1 = 0;
    double sum;
    double min;
        cout << "Enter the positive quantity of numbers to read in(MIN): ";
        cin >> quantity;
        do
        {
                cout << "Enter Number " << num++ << ": ";
                cin >> num1;
                if (num1 < min)
                {
                    min = num1;
                }
                counter++;

        }while (counter != quantity);

        return cout << "The min of your numbers is: " << min << endl;
    }
    else if (selection == 4)
    {
        double num1 = 0, temp = 0, max = 0;
        int counter = 0, quantity, num = 1;

        cout << "Enter the positive quantity of numbers to read in(MAX): ";
            cin >> quantity;
            while (counter < quantity)
            {
                cout << "Enter Number " << num++ << ": ";
                cin >> num1;
                if (num1 >= max)
                {
                    max = num1;
                }
                    counter++;
            }

            return cout << "The max of your numbers is: " << max << endl;
        }
        return 0;
    } 

我假设问题出在相应行中每个函数的返回中。我只是想不出办法来解决它。很抱歉这篇长文,任何帮助将不胜感激。

【问题讨论】:

  • return cout &lt;&lt; "The max of your numbers is: " &lt;&lt; max &lt;&lt; endl; 你觉得这有什么作用?
  • 在我的家用电脑上它会返回答案。例如,如果我输入 4 个数字,它将从该函数中输出最高的数字。
  • 这不适用于您的家庭计算机或学校计算机。这是无效的代码;它包含明显的错误,无论您在哪里编译和运行代码,都会导致同样的问题。
  • @AxelYates 你似乎对return 的作用有一个非常基本的误解。您应该重新访问您正在使用的书中的该部分。如果您还没有:Those 是一些最好的,如果不是最好的,就在那里。

标签: c++ function terminal clion


【解决方案1】:

您的问题出在此处(尽管这只是一个实例):

return cout << "The sum of your numbers is: " << sum << endl;

这是试图返回cout“语句”的结果,正如错误所示,它确实会给你一个ostream

您可能只想输出该文本,然后返回 just 值,如:

cout << "The sum of your numbers is: " << sum << endl;
return sum;

但是,由于您从未真正使用返回值,您也可以将函数设为无效,例如:

void min_max(double selection)

然后在这些函数中单独使用return(当然是在cout &lt;&lt; ... 之后)。

【讨论】:

  • 修复了它。我知道这是一件小事。我还在学习绳索。谢谢大家。
【解决方案2】:

你返回的字符流是“cout

此外,看起来您甚至没有使用返回值。在这种情况下,将函数设为 void 并完全删除 return 语句可能会更好。

如果您希望能够获取答案并将其传递出去,最好的解决方法是让 sum 方法调用 cout 函数将总和输出到控制台,然后在下一行返回实际总和。

【讨论】:

    猜你喜欢
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    相关资源
    最近更新 更多