#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int number;
char re;
cout<<"multiplication table[1]"<<endl;
cout<<"EXIT[2]"<<endl;
cin>>number;
do{
switch(number)
{
case 1:
int number;
cout<<"Enter the number 5:"<<endl;
cin>>number;
if (5 == number);
cout<<"5 multiplied by 0 is equal to 0"<<endl;
cout<<"5 multiplied by 1 is equal to 5"<<endl;
cout<<"5 multiplied by 2 is equal to 10"<<endl;
cout<<"5 multiplied by 3 is equal to 15"<<endl;
cout<<"5 multiplied by 4 is equal to 20"<<endl;
cout<<"5 multiplied by 5 is equal to 25"<<endl;
cout<<"5 multiplied by 6 is equal to 30"<<endl;
cout<<"5 multiplied by 7 is equal to 35"<<endl;
cout<<"5 multiplied by 8 is equal to 40"<<endl;
cout<<"5 multiplied by 9 is equal to 45"<<endl;
cout<<"5 multiplied by 10 is equal to 50"<<endl;
else;
default:
cout<<"Invalid Output"<<endl;
break;
}
cout<<"Do you want to continue?[Y/N]"<<endl;
cin>>re;
}while(re=='Y'||re=='y');
getch();
return 0;
}
the problem here is that the invalid output are not working properly as I want
it suppose to be invalid when you type other number except for the number 5 and the exit should have the termination of "do you want to continue?[Y/N]" when I press y and enter it looped again to the same statement of "Do you want to continue?[Y/N].
#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int number;
char re;
cout<<"multiplication table[1]"<<endl;
cout<<"EXIT[2]"<<endl;
cin>>number;
do{
switch(number)
{
case 1:
int number;
cout<<"Enter the number 5:"<<endl;
cin>>number;
if (5 == number);
{ //Braces added
cout<<"5 multiplied by 0 is equal to 0"<<endl;
cout<<"5 multiplied by 1 is equal to 5"<<endl;
cout<<"5 multiplied by 2 is equal to 10"<<endl;
cout<<"5 multiplied by 3 is equal to 15"<<endl;
cout<<"5 multiplied by 4 is equal to 20"<<endl;
cout<<"5 multiplied by 5 is equal to 25"<<endl;
cout<<"5 multiplied by 6 is equal to 30"<<endl;
cout<<"5 multiplied by 7 is equal to 35"<<endl;
cout<<"5 multiplied by 8 is equal to 40"<<endl;
cout<<"5 multiplied by 9 is equal to 45"<<endl;
cout<<"5 multiplied by 10 is equal to 50"<<endl;
}
else;
default:
cout<<"Invalid Output"<<endl;
break;
}
cout<<"Do you want to continue?[Y/N]"<<endl;
cin>>re;
}while(re=='Y'||re=='y'); //If you want to exit if y is pressed, then replace (Y,y) with (N.n)
getch();
return 0;
}
And as for the exit thing. It says Do you want to continue(Y/N) and if you press Y, it continues. what is the problem then? Do you want it to exit if Y is pressed? If yes then, do as commented in the while statement.
If it works for you then I need you to find out why it worked. Consider it as homework. ;)
a) No ';' after your if statement (line 21 in Pravesh' code snippet).
b) No empty 'else'. An if can be by itself. If you don't need an 'else' case, don't use one.
c) If you need your cases to be strictly separated, make sure to add a 'break;' at the end of each case.