[英]Simple C++ code won't run
我的程序从字面上开始,提示输入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;
}
实际上,有很多事情需要更改。
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
例程由编译器看着是试图“重新定义”并已定义程序。
另外,事实是,在Weather
例程中,您正在按值传递变量,但是在例程中,试图读取并设置weather
变量的值。 如果希望能够在例程中设置变量值,则需要通过引用传递。
您以错误的方式调用函数。
您尚未在main function
声明或初始化变量。
并且不接受用户的任何输入来初始化变量。
调用函数的正确方法是
试试这个代码
if (selection==1){
Weather(weather);
WaterClarity(waterclarity);
}
else if (selection == 2){
Season(season);
}
但在此之前,您必须接受变量的输入。
int weather;
int waterclarity;
int season;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.