2015-10-15 63 views
-5

我正在開發一個程序來接受用戶對年齡變量的輸入,然後計算出該人應該進入的年級。我的代碼似乎正在運行,因爲它應該返回正確的值。然而;我也得到了兩個隨機整數以及這些值。我已經瀏覽過幾次,找不到問題。我的代碼如下所示,並且佈置在它是爲了滿足通過我的教授作爲此設置的可接受的程序的標準的方式被分配:在C++中返回隨機值的函數。找不到問題

#include <iostream> 
using namespace std; 

int gradecalculate (int age) 

{ 

    int grade; 

    if (age >= 6 && age <= 17) 
    grade= age-5; 
    return grade; 
    // Program does nothing if # not in range. 

} 

bool messagereturn (int age) 

{ 

    if (age < 5) 
    {cout << "You are too young for school." << endl;} 

    else if (age > 17) 
    {cout << "You are too old for school." << endl;} 

    else 
    {cout << "You should be in grade " << gradecalculate (age) << "." << endl;} 

} 

bool greet (int age) 

{ 

    if (age == 17) 
    {cout << "Hello Senior!" << endl;} 

} 

int main() 

{ 

    int age; 
    cout << "Enter your age: "; 
    cin >> age; 

    cout << messagereturn (age) << endl; 
    cout << greet (age) << endl; 

    return 0; 

} 

當運行該代碼(使用碼塊)的輸出爲如下,我無法接收任何錯誤代碼:

當進入17-

Enter your age: 17 
You should be in grade 12. 
96 
Hello Senior! 
96 

當進入11 -

Enter your age: 11 
You should be in grade 6. 
96 
11 
+0

'messagereturn'和'greet'被定義爲返回值,但沒有'return'語句。這構成了未定義的行爲。 – owacoder

+0

'messagereturn()'和'greet()'都被聲明爲返回一個'bool'值,但它們都沒有返回任何東西。另外,由於'gradecalculate()'僅適用於5-17歲,所以不需要再次檢查年齡。在特定條件下編寫沒有返回值的函數是不好的。 –

+0

我已經使用了隨機函數,它也給了我隨機的結果。 –

回答

0

messagereturn()greet()函數聲明爲返回bool值,但其中沒有一個實際返回任何東西,因此它們的值是隨機的。由於您要將返回值發送到std::cout,但函數直接將它們自己的消息發送到std::cout,因此它們根本不應返回任何bool值。

此外,由於gradecalculate()僅適用於5-17歲,因此無需再次檢查age。編寫一個在某些條件下沒有return值的函數是不好的。

試試這個:

#include <iostream> 

using namespace std; 

int gradecalculate (int age) 
{ 
    return age-5; 
} 

void messagereturn (int age) 
{ 
    if (age < 5) 
    {cout << "You are too young for school." << endl;} 

    else if (age > 17) 
    {cout << "You are too old for school." << endl;} 

    else 
    {cout << "You should be in grade " << gradecalculate (age) << endl;} 
} 

void greet (int age) 
{ 
    if (age == 17) 
    {cout << "Hello Senior!" << endl;} 
} 

int main() 
{ 
    int age; 
    cout << "Enter your age: "; 
    cin >> age; 

    messagereturn (age); 
    greet (age); 

    return 0; 
} 

或者,你可以在功能合併在一起,並有它返回一個std::string你然後發送到std::cout

#include <iostream> 
#include <string> 
#include <sstream> 

using namespace std; 

int gradecalculate (int age) 
{ 
    return age-5; 
} 

string messagereturn (int age) 
{ 
    if (age < 5) 
    {return "You are too young for school.";} 

    else if (age > 17) 
    {return "You are too old for school.";} 

    else 
    { 
     ostringstream oss; 
     oss << "You should be in grade " << gradecalculate(age); 
     if (age == 17) 
     {oss << "\n" << "Hello Senior!";} 
     return oss.str(); 
    } 
} 

int main() 
{ 
    int age; 
    cout << "Enter your age: "; 
    cin >> age; 

    cout << messagereturn (age) << endl; 

    return 0; 
} 
0

你的兩個函數bool messagereturn (int age)bool greet (int age)期望一個bool返回,但你不return什麼,這可能是你輸出兩個隨機整數的原因。

我不知道你希望從這些函數返回的,但這裏是你的代碼返回false,而不是什麼:

bool messagereturn (int age) 

{ 

    if (age < 5) 
    {cout << "You are too young for school." << endl;} 

    else if (age > 17) 
    {cout << "You are too old for school." << endl;} 

    else 
    {cout << "You should be in grade " << gradecalculate (age) << "." << endl;} 
    return false; 
} 

bool greet (int age) 

{ 

    if (age == 17) 
    {cout << "Hello Senior!" << endl;} 
    return false; 
} 

此外,在功能int gradecalculate (int age),該grade變量未初始化依賴上的值爲age。下面是與grade代碼初始化爲0。

int gradecalculate (int age) 

{ 

    int grade = 0; 

    if (age >= 6 && age <= 17) 
    grade= age-5; 
    return grade; 
    // Program does nothing if # not in range. 

} 
0

你的功能greet()messagereturn()被聲明爲返回bool但他們什麼也不返回,改變他們的聲明void greet(int age);void messagereturn(int age)也全部印刷COUT爲這些函數內完成,有沒有必要這樣做:

cout << messagereturn(age) << endl; 
cout << greet(age) << endl; 

刪除cout和endl部分,只是離開功能的調用。

最後

int gradecalculate (int age) 
{ 

    int grade = 0; //initialize grade to something, else you're returning garbage when age < 6 or > 17 

    if (age >= 6 && age <= 17) 
    grade= age-5; 
    return grade; 
    // Program does nothing if # not in range. 

} 
-1

您不能返回局部變量。你在gradecalculate()中做到這一點。

申報等級全球並初始化爲0。

也有是在如果你的if語句不爲真,則返回一個未初始化的變量,函數的條件。

messagereturn and greet should void,not bool。

變化

cout << messagereturn (age) << endl; 
cout << greet (age) << endl; 

messagereturn(年齡);

迎接(年齡);

因爲在這些函數中你已經使用了標準輸出,並且函數不返回任何東西。