Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Apr 2, 2015
1 parent c8b821e commit acc5734
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
19 changes: 1 addition & 18 deletions ch06/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,7 @@ int main()
}
```

##Exercise 6.4

```cpp
int func()
{
int n, ret = 1;
std::cout << "input a number: ";
std::cin >> n;
while (n > 1) ret *= n--;
return ret;
}

int main()
{
std::cout << func() << std::endl;
return 0;
}
```
## [Exercise 6.4](ex6_04.cpp)

##Exercise 6.5

Expand Down
36 changes: 36 additions & 0 deletions ch06/ex6_04.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
=================================================================================
C++ Primer 5th Exercise Answer Source Code
Copyright (C) 2014-2015 github.com/pezy/CppPrimer
Write a function that interacts with the user, asking for a
number and generating the factorial of that number. Call this function from
main.
If you have questions, try to connect with me: pezy<urbancpz@gmail.com>
=================================================================================
*/

#include <iostream>

void factorial_with_interacts() {
int num;
std::cout << "Please input a positive number: ";
while ( std::cin >> num && num < 0 )
std::cout << "Please input a positive number again: ";
std::cout << num;

unsigned long long result = 1;
while (num > 1) result *= num--;

std::cout << "! is ";
if ( result ) std::cout << result << std::endl;
else std::cout << "too big" << std::endl;
}

int main()
{
factorial_with_interacts();
}

0 comments on commit acc5734

Please sign in to comment.