y/n function
My y/n function is not working. If you run it you would understand how it looks.
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 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int a, b, c;
double m1, m2, m3, total;
cout<<"Enter viewers aged 2>5: "<<endl;
cin>>a;
cout<<"Enter viewers aged 7>17: "<<endl;
cin>>b;
cout<<"Enter adult viewers: "<<endl;
cin>>c;
cout<<endl;
int viewer[]={a, b, c};
cout<<"Numbers of viewers for each category: "<<endl;
cout<<"-------------------------------------"<<endl;
cout<<"Category 1: ";
cout<<viewer[0]<<endl;
cout<<"Category 2: ";
cout<<viewer[1]<<endl;
cout<<"Category 3: ";
cout<<viewer[2]<<endl<<endl;
m1 = viewer[0] * 2;
m2 = viewer[1] * 3;
m3 = viewer[2] * 5;
total = m1 + m2 + m3;
double money[]={m1, m2, m3};
cout<<"Collection Obtained ($): "<<endl;
cout<<"-------------------------------------"<<endl;
cout<<"Category 1: ";
cout<<money[0]<<endl;
cout<<"Category 2: ";
cout<<money[1]<<endl;
cout<<"Category 3: ";
cout<<money[2]<<endl;
cout<<"*****Total= "<< total<<endl<<endl;
char selection;
cout<<"Calculate Again? (y/n)";
cin>>selection;
if (selection == 'y')
if (selection == 'Y')
{
main();
}
if (selection == 'n')
if (selection == 'N')
{
exit(0);
}
}
| |
1 2 3 4 5 6 7 8 9 10
|
if (selection == 'y')
if (selection == 'Y')
{
main();
}
if (selection == 'n')
if (selection == 'N')
{
exit(0);
}
| |
This is just plain all wrong. In many ways.
Here it is, less wrong:
1 2 3 4 5 6 7 8 9
|
if (selection == 'y' || selection == 'Y')
{
main();
}
if (selection == 'n' || selection == 'N')
{
exit(0);
}
| |
but this is still wrong.
You must NEVER EVER call
main()
. It's forbidden in C++. You are not allowed to call
main()
.
You should not be calling
exit()
. You should be ending this program by
return
from
main()
.
Last edited on
1 2 3 4
|
if (selection == 'y')
if (selection == 'Y')
try
if (selection == 'Y' || selection == 'y')
| |
and simillar for N and n.
1 2 3 4 5 6 7 8 9 10 11
|
int main()
{
while ( true )
{
int a, b, c;
// etc.
// etc.
cin>>selection;
if (selection == 'n' || selection == 'N' ) break;
}
}
| |
Topic archived. No new replies allowed.