0

I recently tried making a program that has several functions outside of main, but for some reason the "declaration is incompatible with" all of my variables and functions. Is there an easy way of using functions outside of main and refer to the variables interchangably? The error i receive is:E0147. and this is my code:

    #include <iostream>
using namespace std;
int dager;
int mintemp;
int maxtemp;
int nedbor;
int i = 1;
int j;
int mintempg;
int maxtempg;
int nedborg;

int mintemp() {
    cout << "hva er dag:" << i << " sin min temp? \n";
    cin >> mintempg;

    if (mintempg >= -70 && mintempg <= 70)
    {
        mintemp += mintempg;
    }
    else
    {
        cout << "ugyldig temperatur.";
        mintemp();
    }
}
int maxtemp() {
    cout << "hva er dag:" << i << " sin max temp? \n";
    cin >> maxtempg;

    if (maxtempg >= mintempg && maxtemp <= 70)
    {
        maxtemp += maxtempg;
    }
    else
    {
        cout << "ugyldig temperatur.";
        maxtemp();
    }
}
int nedbor() {
    cout << "hva er dag:" << i << " sin mm nedbør? \n";
    cin >> nedborg;

    if (nedborg >= 0 && nedborg <= 200)
    {
        nedbor += nedborg;
    }
    else
    {
        cout << "ugyldig mengde nedbor.";
        nedbor();
    }
}



int main() {
    cout << "Hvor mange dager er det i måneden? " << "\n";
    cin >> dager;


    for (j = dager; j > 0; j--) {
        if (dager >= 28 && dager <= 31)
        {
            mintemp();

            maxtemp();

            nedbor();

            i++;
        }
    }
    system("pause");
}

So as you can see it provides a lot of readability to my code to do it like this, which is why i decided i didn't want all of it to be in main. Also it makes it easier to reuse the code later.

Side question: Is there a way I can make the compilator not close whenever it has run through code without using system("pause"); ?, I seem to recall our professor mentioning it in one of our lectures, but I didn't quite catch it. Anyways all help appreciated !!

  • You cannot have a variable name `foo` and a function named `foo()`. C++ does not work this way. It's as simple as that. – Sam Varshavchik Sep 16 '17 at 00:54
  • oh thanks ! I didn't know. A quick question though, when it says that the functions must return a value, is there a difference between using "void functionmame()" or having the int function return 0 ? – D. Ravndal Sep 16 '17 at 00:58
  • 1
    One is a void function that doesn't return anything. The other one is a function that returns an `int` 0 value. That's the difference. – Sam Varshavchik Sep 16 '17 at 01:12
  • void will not return anything. Though, it does allow the return statement to exit the function early. – user3606329 Sep 16 '17 at 01:49

1 Answers1

-1

For your side question; In order to prevent the window from closing look at this post.

-If you are using Visual Studio and you are starting the console application out of the IDE: pressing CTRL-F5 (start without debugging) will start the application and keep the console window open until you press any key.

-You could insert std::getchar(); at the end of your main function. The function is included by .

reference: stackoverflow.com/questions/2529617/...