jeudi 11 janvier 2018

C++ If/else statement

first time posting here so please forgive me if formatting is not the norm.

So far this is the program. getTemp function should prompt the users for input, and return it back to main where it would print the input. Then printActivity is the function which determines the input and prints the activity based on the inputted temperature.

If I just keep the "if" statement, the program runs perferctly. But when I add the "else if" statement, I keep getting a compiler error statement it expects either a { or ( in the same line as the "else if". (I'm not infront of my compiler at the moment so I can regurgitate the exact error).

#include <iostream>

using namespace std;

void getTemp(int *x); // prototype
void printActivity (int y); // prototype


void getTemp(int *x) {

    *x = 0;

    cout << "Please Enter Temperature in Fahrenheit: ";
    cin >> *x;
}

void printActivity (int y) {

    if(y > 85) {
        cout << "\n\nBased on the temperature, the activity is: Swimming" << endl;
    }

    else if (y > 70 ) && (y <= 85) {
        cout "\n\nBased on the temperature, the activity is: Baseball" << endl;
    }



}


int main()
{
    int temp = 0;
    getTemp(&temp);  //  reference parameter passed to getTemp function
    cout << "The temperature is: " << temp << " degrees" << endl;

    int activity = temp; // value parameter passed to printActivity function
    printActivity(activity);

}

Aucun commentaire:

Enregistrer un commentaire