How to make exit button???
I wrote an factorial calculator and I did an if statement in do loop .
It works but it won't close!!
here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int fact=1;
int num;
bool hhh = false;
cout << " Factorial Calc type -1 to exit" << endl;
do{
cout << "Please enter a positive number: " ;
cin >> num;
if (num > 0)
{hhh = true;}
else
{cout << "";
hhh = false;}
if (num == -1)
{ system("pause");}
}
while (!hhh);
for (int i = 1; i <=num; i++)
{fact = fact * i;}
cout << "the factor is: " << fact << endl;
do{
cout << "Please enter another positive number: " ;
cin >> num;
if (num > 0)
{ for (int i = 1; i <=num; i++)
{fact = fact * i;}
cout << "the factor is: " << fact << endl;}
else
{cout << "";
hhh = false;}
if (num == -1)
{ system("pause");}
}
while (num == -1);
}
|
Your code is far to complicated
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(void)
{
int fact = 1, num;
cout << " Factorial Calc type -1 to exit" << endl;
cout << "Please enter a positive number: " ;
cin >> num;
while (num != -1)
{
for (int i = 1; i <=num; i++)
{
fact = fact * i;
}
cout << "the factor is: " << fact << "\n\n";
cout << "Please enter another positive number: " ;
cin >> num;
}
system("pause");
}
|
i did it like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int fact=1;
int num;
bool hhh = false;
cout << " Factorial Calc type -1 to exit" << endl;
do{
cout << "Please enter a positive number: " ;
cin >> num;
if (num < 1)
cout << endl <<"Don't enter an negative number!" << endl<<endl;
}
while (num == 0 || num < -1);
if (num == -1){
hhh = true;
num = -1;}
else
{for (int i = 1; i <=num; i++)
{fact = fact * i;}
cout << "the factor is: " << fact << endl;
fact = 1;}
while (num !=-1){
cout << "Please enter another positive number: " ;
cin >> num;
if (num > 0)
{ for (int i = 1; i <=num; i++)
{fact = fact * i;}
cout << "the factor is: " << fact << endl;
fact = 1;}
else if ( num <= 0 && num !=-1)
{cout << endl <<"Don't enter an negative number!" << endl<<endl;
hhh = false;}
}
system("pause");
}
|
Topic archived. No new replies allowed.