2015-10-16 50 views
-1

因此,我使用CLion編寫我的計算機科學課程。我在我們的機器上發現了一個錯誤,這在我的個人計算機Terminal或CLion中沒有發生。程序不會在學校電腦上編譯,但在我的電腦上完美編譯

[email protected]:~/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 
[email protected]:~/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; 
    } 

我假設的問題是從相應的線路各功能的回報。我只是無法找出解決方法。很抱歉,很長的文章,任何幫助將不勝感激。

+2

'return cout <<「您的號碼的最大值是:」<< max << endl;'您認爲這樣做了什麼?我的家用電腦上的 –

+0

它返回答案。如果我輸入4個數字,它會輸出該函數中最高的一個。 –

+0

哇... * facepalm * – Mystra007

回答

0

您正在返回一串字符「cout < < // stuff」。該函數期望您僅僅返回double(和變量)。

此外,它看起來你甚至不使用返回值。在這種情況下,將函數設爲void並完全刪除return語句可能會更好。

如果你想能夠接受答案並傳遞給它,解決它的最好方法是讓sum方法調用cout函數將總和輸出到控制檯,然後在下一行返回實際總和。

1

你的問題就在這裏(雖然這只是一個實例):

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

這是試圖返回cout「聲明」,這的確會給你一個ostream作爲錯誤的結果表示。

你可能只是想輸出的文本,然後返回只是的值,如:

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

但是,因爲你從來沒有真正使用返回值,你不妨做功能無效,如用:

void min_max(double selection) 

然後只在其自己的使用return這些函數中(後當然cout << ...)。

+0

修復它。我知道這是一件小事。我仍然在學習繩索。感謝大家。 –

相關問題