Reputation: 33
I am new to C++ and I am making a program to generate a multiplication table. Here is he code.
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter a number to find Multiplication Table ";
cin >>num;
for(int a=1;a<=10;a++)
{
cout<<num<<" x "<<a<<" = "<<num*a<<endl;
}
cout << "Press ENTER to continue...\n";
cin.get();
getchar();
return 0;
}
I want that after the multiplication table of one number is displayed , the user should have an option of entering another number or exiting. Like press "n" to enter a new number or "e" to exit
Upvotes: 1
Views: 98
Reputation: 2721
#include <iostream>
using namespace std;
int main()
{
int num;
char ch;
do{
cout << "Enter a number to find Multiplication Table";
cin >> num;
for(int a=1;a<=10;a++)
{
cout<<num<<" x "<<a<<" = "<<num*a<<endl;
}
cout << "Press \"n\" to enter a new number or \"e\" to exit\n";
}while(cin>>ch && ch=='n');
return 0;
}
Upvotes: 0
Reputation: 7370
This may be what you want. (this is the implementation of main function)
int num;
char command;
bool exit=false;
while(!exit)
{
cout << "Enter a number to find Multiplication Table ";
cin >>num;
for(int a=1;a<=10;a++)
{
cout<<num<<" x "<<a<<" = "<<num*a<<endl;
}
cout << "Press n to continue or e to exit\n";
cin >> command;
while(command != 'n' && command != 'e')
{
cout << "Just press n to continue or e to exit!\n";
cin >> command;
}
if (command == 'e')
{
exit=true;
}else
{
exit=false;
}
}
return 0;
Upvotes: 1