vendredi 24 mars 2017

recursive function not called properly

I'm using a recursive method to convert a number into a representation in base of 7. However, after I run the program, the output string is identical to the input number. What's cause that the function is not called?

#include <iostream>
#include <string>
using namespace std;

string convertToBase7(int num) {
if(num<0) {
    string result;
    result = "-" + to_string(-num);
    return result;
}
if(0<=num<7){
    return to_string(num);
}
cout << "recursive" << endl;
return convertToBase7(num/7) + to_string(num%7);
}


int main() {
int input = 100;
string output = convertToBase7(input);
cout << "Input: " << input;
cout << ", Output: " << output << endl;
return 0;
}

The output is:

Input: 100, Output: 100

Aucun commentaire:

Enregistrer un commentaire