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 !!