Skip to content

Commit

Permalink
fixed ex6_04 from an email issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Oct 7, 2016
1 parent e33512d commit e6d9631
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions ch06/ex6_04.cpp
Original file line number Diff line number Diff line change
@@ -1,39 +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.
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>
About the magic number(13): https://github.com/Mooophy/Cpp-Primer/pull/172
If you have questions, try to connect with me: pezy<[email protected]>
=================================================================================
*/

#include <iostream>
#include <string>

void factorial_with_interacts()
int fact(int val)
{
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--;
int ret = 1;
while (val > 1)
ret *= val--;
return ret;
}

std::cout << "! is ";
if (result)
std::cout << result << std::endl;
else
std::cout << "too big" << std::endl;
void factorial_with_interacts() {
for (int val = 0; std::cout << "Enter a number within [0, 13): ", std::cin >> val; ) {
if (val < 0 || val > 12) continue;
std::cout << val << "! =" << fact(val) << std::endl;
}
}

int main()
{
factorial_with_interacts();
}
factorial_with_interacts();
}

0 comments on commit e6d9631

Please sign in to comment.