首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单的C++代码不会运行

简单的C++代码不会运行
EN

Stack Overflow用户
提问于 2014-04-23 12:03:40
回答 2查看 93关注 0票数 0

我的程序从字面上开始,提示输入1或2,你输入它,然后它结束。它为什么要这么做?我真的需要一些想法。如果有人有关于如何更好地设置的建议,我是完全开放的,我还没有太多的经验。

代码语言:javascript
复制
#include <iostream>
#include <iomanip>
using namespace std;


void Weather(int weather)
{
    cout << "What clarity is the weather today in: " << endl;
    cout << "Clear" << endl << "Cloudy" << endl << "Dark";
    cin >> weather;
    cout << "Today's weather is " << weather;
    system("pause");

}

void WaterClarity(int waterclarity)
{
    cout << "What condition is the water in: " << endl;
    cout << "Clear" << endl << "Cloudy" << endl << "Murky";
    cin >> waterclarity;
}

void Season(int season)
{
    int month = 0;
    cout << "Using numbers 1-12 what month is it?";
    cin >> month;
    if (month == 1){
                cout << "The fish in season are: " << endl;
        cout << "Mahi Mahi" << endl << "Jack Crevalle" << endl << "Pompano";
        cout << "Redfish" << endl << "Spanish Mackrel";
    }
    else if (month == 2){
        cout << "The fish in season are: " << endl;
        cout << "Cobia" << endl << "Mahi Mahi" << endl << "Jack Crevalle" << endl << "Pompano";
        cout << "Redfish" << endl << "Spanish Mackrel";
    }
    else if (month == 3){
        cout << "The fish in season are: " << endl;
        cout << "Blackfin Tuna" << endl << "Cobia" << endl << "Mahi Mahi" << endl << "Jack Crevalle" << endl << "Pompano";
        cout << "Redfish" << endl << "Spanish Mackrel";
    }
    else if (month == 4){
        cout << "The fish in season are: " << endl;
        cout << "Blackfin Tuna" << endl << "Kingfish" << endl << "Mahi Mahi" << endl << "Jack Crevalle" << endl << "Pompano";
        cout << "Redfish" << endl << "Spanish Mackrel" << endl << "Tarpon";
        }
    else if (month == 5){
        cout << "The fish in season are: " << endl;
        cout << "Blackfin Tuna" << endl << "Kingfish" << endl << "Mahi Mahi" << endl << "Jack Crevalle" << endl;
        cout << "Tarpon";
    }
    else if (month == 6){
        cout << "The fish in season are: " << endl;
        cout << "Cobia" << endl << "Snapper" << endl << "Mahi Mahi" << endl << "Jack Crevalle" << endl;
        cout << "Tarpon";
    }
    else if (month == 7){
        cout << "The fish in season are: " << endl;
        cout << "Cobia" << endl << "Snapper" << endl << "Mahi Mahi" << endl << "Jack Crevalle" << endl;
        cout << "Tarpon";
    }
    else if (month == 8){
        cout << "The fish in season are: " << endl;
        cout << "Blackfin Tuna" << endl << "Redfish" << endl << "Mahi Mahi" << endl << "Jack Crevalle" << endl;
        cout << "Tarpon";
    }
    else if (month == 9){
        cout << "The fish in season are: " << endl;
        cout << "Blackfin Tuna" << endl << "Snapper" << endl << "Mahi Mahi" << endl << "Flounder" << endl;
        cout << "Tarpon" << endl << "Kingfish";
    }
    else if (month == 10){
        cout << "The fish in season are: " << endl;
        cout << "Blackfin Tuna" << endl << "Snapper" << endl << "Mahi Mahi" << endl << "Flounder" << endl;
        cout << "Tarpon" << endl << "Kingfish" << endl << "Redfish";
    }
    else if (month == 11){
        cout << "The fish in season are: " << endl;
        cout << "Blackfin Tuna" << endl << "Spanish Mackrel" << endl << "Mahi Mahi" << endl << "Flounder" << endl;
        cout << "Tarpon" << endl << "Kingfish" << endl << "Pompano";
    }
    else if (month == 12){
        cout << "The fish in season are: " << endl;
        cout << "Jack Crevalle" << endl << "Mahi Mahi" << endl << "Spanish Mackrel" << endl;
        cout << "Tarpon" << endl << "Kingfish" << "Pompano";
    }
}

int main()
{
    int selection = 0;
    cout << "Would you like to fish by lure or by season?"<<endl;
    cout << "Enter 1 for Lure, and 2 for Season.";
    cin >> selection;
        if (selection==1){
            void Weather(int weather);
            void WaterClarity(int waterclarity);


        }
        else if (selection == 2){
                void Season(int season);
        }
    system("pause");
    return 0;
}
EN

回答 2

Stack Overflow用户

发布于 2014-04-23 12:09:37

实际上,有相当多的事情需要改变。

Ankit B所说的是真的,但它仍然不起作用。为了调用他的声明方式,您需要在主例程中声明变量weatherwaterclarityseason。它将如下所示:

代码语言:javascript
复制
int main()
{
    int selection = 0;
    int weather = 0;
    int waterclarity = 0;
    int season = 0;

    cout << "Would you like to fish by lure or by season?"<<endl;
    cout << "Enter 1 for Lure, and 2 for Season.";
    cin >> selection;
        if (selection==1){
            Weather(weather);
            WaterClarity(waterclarity);


        }
        else if (selection == 2){
                Season(season);
        }
    system("pause");
    return 0;
}

将关键字void添加到main例程中的例程WeatherWaterClaritySeason之前,编译器会将其视为试图“重新定义”已经定义的例程。

此外,事实是,在Weather例程中,您是按值传递变量,但在例程中,您试图读入并设置weather变量的值。如果您希望能够在例程中设置变量值,则需要通过引用传递。

票数 2
EN

Stack Overflow用户

发布于 2014-04-23 12:06:27

您调用函数的方式不正确。

您尚未在main function中声明或初始化该变量。

并且不接受来自用户的任何输入来初始化变量。

调用函数的正确方法是

试试这段代码

代码语言:javascript
复制
if (selection==1){
 Weather(weather);
 WaterClarity(waterclarity);  
 }
 else if (selection == 2){
    Season(season);
 }

但在此之前,您必须接受变量的输入。

代码语言:javascript
复制
int weather;
int waterclarity;
int season;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23234522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档