user3562785
user3562785

Reputation: 1

Simple C++ code won't run

My program literally starts, prompts for 1 or 2, you enter it, and then it ends. Why is it doing this? I could really use some ideas. if anyone has a suggestion as to how to better set this up, i'm totally open, I don't have much experience yet.

#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;
}

Upvotes: 0

Views: 117

Answers (2)

trumpetlicks
trumpetlicks

Reputation: 7065

There are actually quite a few things that need to change.

What Ankit B says is true, but it still wont work. In order to call the way he is stating, you will need to declare the variables weather, waterclarity, and season in your main routine. It would look as follows:

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;
}

Adding the keyword void in front of your routines Weather, WaterClarity, and Season within the main routine is looked at by the compiler as an attempt to "re-define" and already defined routine.

Also, the fact is that within your Weather routine, you are passing variables by value, but within the routine trying to read in and set the weather variable's value. If you want to be able to set the variables value within the routine, you will need to pass by reference.

Upvotes: 2

A B
A B

Reputation: 1521

You are calling function in incorrect manner.

you haven't declare or initialized the variable in your main function.

and not accepting any input from the user to initialized the variables.

The correct way of calling the function is

Try this code

if (selection==1){
 Weather(weather);
 WaterClarity(waterclarity);  
 }
 else if (selection == 2){
    Season(season);
 }

but before that you have to accept the input for variables .

int weather;
int waterclarity;
int season;

Upvotes: 1

Related Questions