My first ever program written in an IDE and 2 issues

Two Issues:

first :
How do i create multiple files in a project using codeblock 10.05. The code in main.cpp compiles and runs but if i add another file to that same project i get a list of error messages.

second :
i can't seem to get the "else if" part of my code to work. Here is the code.

#include <iostream>
#include <string>
//#include "fact_modifier.h"

using namespace std;

double funcFactorial (int a);
/*void tooHigh(int);*/

int main()
{

double myResult;
string userName;

cout << "Please enter your name" <<endl;
getline (cin, userName);
cout << "Welcome " << userName << "!\n\n";
cout << "Please enter an integer between (0 - 9) and press the enter key\n";
int a;
cin >> a;

myResult = funcFactorial (a);
cout << "The factorial of : " << a << " is" << " " << myResult << "\n\n";
cout << "Thank you for trying out our program!" <<endl;


return 0;
}

double funcFactorial (int b)
{
if (b > 0 || b == 9)
return b * funcFactorial(b - 1);
else if (b > 9)
cout << "You have entered an invalid number" << endl;
else
return 1;
}
Last edited on
2. If funcFactorial is passed 10, the first condition is 10 > 0 OR 10 = 9. Of course 10 > 0, so this is true and the else statements are not reached. What you need is b > 0 && b <= 9.

1. What are the errors? If it's "undefined reference to ...", you probably didn't add the file to your project. I'm not sure how that is done in code blocks. look for something like project -> add in the menu.
If it's a different kind of error, post your code.
Your "else if" will never be hit because your if satisfies even more conditions. I mean b > 0 will always be true when b > 9
Thanks guys for your prompt response. Actually b>0 was why i used the "||" operator followed by b==9 thinking that if b==9 it automatically handles b if it tries to go above 9, forgeting that the "||" operator only needs one condition to be true.
Topic archived. No new replies allowed.