Search code examples
c++functionheaderrecurrence

C++ - Recurrence


I want use two functions with recurrence in the same time, and I have a problem:

I have two functions. Each use other function from the same header file. But when I want use below function in above function, I have an error. I know, what happened, but I don't know how to fix it. My code:

Main.cpp:

include <iostream>
#include <cstdlib>
#include <C:\Users\Natch\Documents\CPP\Calc\incls.h>
using namespace std;

//Global vars



//Global functions




//Main program


int main() {
menu(1);
system("pause");
}

Incls.h:

#ifndef INCLS_H_INCLUDED
#define INCLS_H_INCLUDED



#endif // INCLS_H_INCLUDED

#include <iostream>
#include <cstdlib>

using namespace std;
//Vars

int a, b, func, sa, ctrl;
long double sum, sqrt;

//Voids
void down(int func) {
 if (func < 6) {
          cout << "Wprowadz pierwszy skladnik dzialania:" << endl;
          cin >> a;
          cout << "Wprowadz drugi skladnik dzialania:" << endl;
          cin >> b;
          switch(func){
              case 1:
                sum = a+b;
                cout << "Wynik wynosi: " << sum << endl;
                menu(2);
                break;
              case 2:
                sum = a-b;
                cout << "Wynik wynosi: " << sum << endl;
                break;
              case 3:
                sum = a*b;
                cout << "Wynik wynosi: " << sum << endl;
                break;
              case 4:
                sum = a/b;
                cout << "Wynik wynosi: " << sum << endl;
                break;
              case 5:
                sum = a%b;
                cout << "Wynik wynosi: " << sum << endl;
                break;
          }
 }else if (func == 6)
 {
       cout << "Podaj ilosc liczb do obliczenia sredniej:" << endl;
       cin >> sa;
 }else if (func == 7)
 {
       cout << "Podaj liczbe do wyciagniecia pierwiastka:" << endl;
       cin >> a;
 }else if (func == 8)
 {
       cout << "Podaj liczbe do wyciagniecia potegi:" << endl;
       cin >> a;
 }else if (func== 9)
 {
     system("exit");
 }
}

void menu(int ctrl) {
if (ctrl==1){
    system("cls");
    system("color 06");
    cout << "Kalkulator" << endl;
    cout << "Wybierz funkcje: " << endl;
    cout << "1.Dodawanie" << endl;
    cout << "2.Odejmowanie" << endl;
    cout << "3.Mnozenie" << endl;
    cout << "4.Dzielenie" << endl;
    cout << "5.Reszta z dzielenia" << endl;
    cout << "6.Srednia arytmetyczna" << endl;
    cout << "7.Pierwiastek kwadratowy" << endl;
    cout << "8.Potega n^2" << endl;
    cout << "9.Wyjscie" << endl;
    cout << "Twoj wybor:" << endl;
    cin >> func;
    down(func);
}else if (ctrl==2){
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    menu(1);
}
}

Solution

  • Firstly, the word you're probably looking for is "recursion". You might want to be a little bit careful here, because by having functions repeatedly call each other during normal operation like this, you may eventually run out of stack space. A more detailed explanation of what this actually means is outside of the scope of this answer, but you'd probably be better using a conventional while loop instead of recursion in this case.

    Secondly, when you get an error, please tell us what it is! Don't make us guess, especially as there's no guarantee that what you're seeing will be the same error as we see when we run your program.

    Thirdly, tell us what platform and compiler you're using. I'm assuming you're using windows, given system("cls"), but again: don't make us guess.

    Now, the actual error. I get this:

    test.h: In function ‘void down(int)’: test.h:30: error: ‘menu’ was not declared in this scope

    What you need is a "function prototyp" which describes the interface of the menu function before it is actually defined. Just stick

    void menu(int);
    

    above the definition of down, and you'll find the compile error goes away. Whether the application runs as intended after that I don't know, because you haven't given enough information!