我的程序字面上开始,提示输入1或2,然后输入,然后结束。它为什么这样做?我真的可以使用一些想法。如果有人建议如何更好地设置它,我完全开放,我还没有太多的经验。
#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;
}
答案 0 :(得分:2)
实际上有很多事情需要改变。
Ankit B所说的是真的,但它仍然无法运作。为了调用他所说的方式,您需要在主例程中声明变量weather
,waterclarity
和season
。它看起来如下:
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
例程中的例程Weather
,WaterClarity
和Season
前面添加关键字main
作为尝试重新定义&#34;已定义的例程。
此外,事实是在Weather
例程中,您按值传递变量,但在尝试读入的例程中设置weather
变量的值。如果您希望能够在例程中设置变量值,则需要通过引用传递。
答案 1 :(得分:1)
您正在以错误的方式调用函数。
您尚未在main function
中声明或初始化该变量。
并且不接受用户的任何输入来初始化变量。
调用该函数的正确方法是
试试此代码
if (selection==1){
Weather(weather);
WaterClarity(waterclarity);
}
else if (selection == 2){
Season(season);
}
但在此之前你必须接受变量的输入。
int weather;
int waterclarity;
int season;