-1

So I use CLion to code for my Computer Science class. I've come across an error on our machines in class that doesn't happen in my personal machine, Terminal or 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$

The code itself looks like this:

    /*
 ============================================================================
 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;
    } 

I'm assuming that the problem is in the returns from each function in the corresponding lines. I just can't figure out a way to fix it. Sorry about the long post, any help would be greatly appreciated.

Axel Yates
  • 15
  • 4
  • 2
    `return cout << "The max of your numbers is: " << max << endl;` What do you think this does? – Baum mit Augen Oct 16 '15 at 01:13
  • on my home computer it returns the answer. As in, if I put in 4 numbers it'll output the highest one from that function. – Axel Yates Oct 16 '15 at 01:18
  • This doesn't work on either your home computer or the school computer. It is invalid code; it contains glaring errors that would cause the same problem no matter where you compiled and ran the code. – Ken White Oct 16 '15 at 01:23
  • @AxelYates You seem to have a quite basic misunderstanding about what `return` does. You should revisit that section in the book you are using. If you don't have one yet: [Those](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) are some of the best, if not simply the best, out there. – Baum mit Augen Oct 16 '15 at 01:25

2 Answers2

1

Your problem lies here (though this is just one instance):

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

This is attempting to return the result of the cout "statement", which will indeed give you an ostream as the error indicates.

You probably just want to output that text then return just the value, as in:

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

However, since you're never actually using the return values, you may as well make the functions void, such as with:

void min_max(double selection)

then just use return on its own within those functions (after the cout << ... of course).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

You're returning a steam of characters the "cout << //stuff". The function is expecting you to simply return the double (the sum variable).

Additionally, it doesn't looks like you're even USING the return value. In this case it may even be better to just make the function void and remove the return statement altogether.

If you want to be able to take the answer and pass it around, the best way to fix it is to have the sum method call the cout function to output the sum to the console, and then in the next line return the actual sum.

Andrew Shirley
  • 407
  • 2
  • 10