need help, C++ set operations calculator - c++

I'm currently trying to make a set operations calculator using c++ and I am still quite new to this language, I've basically finished the code(with the help of a few friends and google ofc), but I am required to have at least five sets instead of two for every operation except the complement of a set, I don't really know how to edit this to include five sets without ruining the code, it would be great if you guys could help me, thank you so much
here's the code:
#include <iostream>
void menu ();
void unionofsets();
void intersectionofsets();
void differenceofsets();
void complementofset();
void exit ();
using namespace std;
int main (){
char ch='y';
while(ch=='y')
{
menu ();
int choice;
cout <<"Please enter the operation you want here:";
cin >>choice;
switch (choice){
case 1:
unionofsets();
break;
case 2:
intersectionofsets();
break;
case 3:
differenceofsets();
break;
case 4:
complementofset();
break;
case 5:
exit ();
break;
default :
cout <<"The system encountered some errors, please try again.......\n Thank you.";
}
cout<<"\n Do you want to continue(yes(y)/no(n))";
cin>>ch;
}
return 0;
}
void menu ()
{
cout <<"Please select an operation by entering it's corresponding number.\n";
cout <<"1.Union of Two Sets"<<endl;
cout <<"----------------------\n";
cout <<"2.Intersection of Two Sets"<<endl;
cout <<"----------------------\n";
cout <<"3.Difference of Two Sets"<<endl;
cout <<"----------------------\n";
cout <<"4.Complement of a Given Set"<<endl;
cout <<"----------------------\n";
cout <<"5.Exit the Program"<<endl;
}
void unionofsets()
{
int size1,size2;
int set1[30];
int set2[30];
cout <<"You've selected to find the Union of two sets.\n";
cout <<"Enter the number of elements in the first set:";
cin >>size1;
cout <<"Enter the number of elements in the second set:";
cin >>size2;
cout <<"Enter "<<size1 <<" elements for first set:";
for (int i=0;i<size1;i++)
{
cin >>set1[i];
}
cout <<"Enter "<<size2<<" elements for the second set:";
for (int j=0;j<size2;j++)
{
cin >>set2[j];
}
cout <<"The union of set 1 and set 2 = {";
int k;
for (int a=0;a<size1;a++)
{
cout <<set1[a]<<" ,";
}
for (int i=0;i<size2;i++)
{
for (k=0;k<size1;k++)
{
if (set2[i]==set1[k])
{
break;
}
}
if (k==size1)
{
cout<<set2[i]<<" ,";
}
}
cout <<"}";
cout<<"\n-------------------------------------------------------------------------- \n";
}
void intersectionofsets(){
int set1[20];
int set2[20];
int sizeofset1,sizeofset2;
cout <<"You've selected to find the intersection of two sets."<<endl;
cout <<"Enter the number of elements in first set:";
cin >>sizeofset1;
cout <<"Enter the number of elements in second set:";
cin >>sizeofset2;
cout <<"Enter "<<sizeofset1<<" elements for first set separated by spaces:";
cout <<endl;
for (int i=0;i<sizeofset1;i++)
{
cin>>set1[i];
}
cout <<"Now enter "<<sizeofset2<<" elements for second set precisely:";
cout <<endl;
for (int j=0;j<sizeofset2;j++)
{
cin>>set2[j];
}
cout<<"The intersection set of first and second set is :";
cout<<" { ";
for (int i=0;i<sizeofset1;i++)
{
for (int j=0;j<sizeofset2;j++)
{
if (set1[i]==set2[j])
cout <<set1[i]<<", ";
}
}
cout<<"}";
cout<<"\n-------------------------------------------------------------------------- \n";
}
void differenceofsets (){
int size1;
int size2;
int set1[34];
int set2[54];
cout <<"You've selected to find the difference of two sets.\n";
cout <<"Enter the number of elements in set1:";
cin >>size1;
cout <<"Enter the number of elements in set2:";
cin >>size2;
cout <<"Enter the elements for the first set:";
for (int i=0;i<size1;i++)
{
cin >>set1[i];
}
cout <<"Enter the elements for the second set:";
for (int k=0;k<size2;k++)
{
cin >>set2[k];
}
cout <<"The difference of two sets you have entered is {";
int l;
for (int i=0;i<size1;i++)
{
for (l=0;l<size2;l++)
{
if (set1[i]==set2[l]){
break;
}
}
if (l==size2)
cout <<set1[i]<<" ,";
}
cout<<"}";
cout<<"\n-------------------------------------------------------------------------- \n";
}
void complementofset(){
int sizefuni=30;
int sizefgiv;
int uniset[30];
int userset[30];
uniset[0]=0;
cout <<"You've selected to find the complement of a set.\n";
cout <<"The universal set for your set is suppose to be \n\n {";
for (int i=0;i<sizefuni;i++)
{
uniset[i]=uniset[0]+i;
cout <<uniset[i]<<" ,";
}
cout <<"}\n\n"<<endl;
cout<<"enter the size of set :";
cin>>sizefgiv;
cout <<"enter the element of the set to find the complement.";
for (int k=0;k<sizefgiv;k++)
{
cin >>userset[k];
}
int l;
cout <<"The complement of the given set is ={";
for (int i=0;i<sizefuni;i++)
{
for (l=0;l<sizefgiv;l++)
{
if (uniset[i]==userset[l]){
break;
}
}
if (l==sizefgiv)
cout <<uniset[i] <<" ,";
}
cout<<"}";
cout<<"\n-------------------------------------------------------------------------- \n";
}
void exit(){
cout <<"Thank you for using our software.";
}

Related

How to make a calculator in c++ using if else and functions?

I am a new contributor and here I am trying to make a simple calculator but having errors in my code. When I compile the code I get:
Error: C:\Users\IJLAL\Documents\collect2.exe [Error] ld returned 1 exit status while compiling
In case it could help, here is the screen shot of error when I pressed compile button or F11 in Dev C++:
Here is my code:
#include<iostream>
using namespace std;
void fun(float a, float b);
int main()
{
float a, b, sum, sub, mul, divide, mod;
char op;
//operands and operators are enterd by the user
cout<<"Enter any two operands with operator=";
cin>>a>>op>>b;
fun(a, b);
return 0;
}
void op(float a, float b)
{
if(a+b)
{
float sum=a+b;
cout<<"\nAddition of two numbers is="<<sum;
}
else if(a-b)
{
float sub=a-b;
cout<<"\nSubtraction of two numbers is="<<sub;
}
else if(a*b)
{
float mul=a*b;
cout<<"\nMultiplication of two numbers is="<<mul;
}
else if(a/b)
{
float divide=a/b;
cout<<"\nDivision of two number is="<<divide;
}
else
{
cout<<"\nInvalid operator.......";
}
}
Please tell me the solution of this problem, so that I can compile the code successfully. If there is any better solution to make a simple calculator on beginner level please mention it in answer.
You're not so far from a result. The problem is that you have not defined the function fun(). Furthermore, in the function op() that you have defined, you do not use the operator of the input.
So first thing to do is to change the signature of the function:
void fun(char op, float a, float b);
Then you need to invoke your function in main(), passing also the operation that was requested by the user:
fun(op, a, b);
Finally you need to change all your if to check if op is the matching operator:
void fun(char op, float a, float b)
{
if(op=='+')
{
...
}
else if(op=='-')
{
...
You should then get the expected result.
Online demo
Aditional infos
if (a+b) just calculates the expression using the two values of the user, and if it's non zero, it's considered as true.
once you got this program to work, you can look for the switch statement
You can use additional functions to make a better calculator. You can use this code. Hope this code will be helpful for you.
The header <iomanip> is part of the Input/output library of the C++ Standard Library and <math.h> is used when we perform mathematical operations.
#include<iostream>
#include<conio.h>
#include<math.h>
#include<iomanip>
char op;
using namespace std;
void sum()
{
int sum = 0;
int n;
int numberitems;
cout << "Enter number of items: \n";
cin >> numberitems;
for(int i=0;i<numberitems;i++)
{
cout<< "Enter number "<<i<<":\n\n" ;
cin>>n;
sum+=n;
}
cout<<"sum is: "<< sum<<endl<<endl;
}
void diff()
{
int diff;
int n1,n2;
cout<<"enter two numbers to find their difference:\n\n";
cout<<"enter first number:";
cin>>n1;
cout<<"\nenter second number:";
cin>>n2;
diff=n1-n2;
cout<<"\ndifference is:"<<diff<<endl<<endl;
}
void pro()
{
int pro=1;
int n;
int numberitems;
cout<<"enter number of items:\n";
cin>>numberitems;
for(int i=0;i<=numberitems;i++)
{
cout<<"\nenter item "<<i<<":";
cin>>n;
pro*=n;
}
cout<<"product is:"<<pro<<endl<<endl;
}
void div()
{
int div;
int n1;
int n2;
cout<<"enter 2 numbers to find their quotient\n\n";
cout<<"enter numerator:";
cin>>n1;
cout<<"\nenter denominator:";
cin>>n2;
div=n1/n2;
cout<<"\nquotient is:"<<div<<endl<<endl;
}
void power()
{
long int p;
int res=1,n;
cout<<"enter number:";
cin>>n;
cout<<"\nenter power:";
cin>>p;
for(int i=1;i<=p;i++)
{
res=n*res;
}
cout<<n<<"\n power "<<p<<" is :"<<res<<endl;
}
void sq()
{
float s;
int n;
cout<<"enter number to find its square root:";
cin>>n;
s=sqrt(n);
cout<<"\nsquare root of "<<n<<" is :"<<s<<endl;
}
void fact()
{
long int f=1;
int c=1,n;
cout<<"enter number to find its factorial:";
cin>>n;
while(c<=n)
{
f=f*c;
c+=1;
}
cout<<"\nfactorial of "<<n<<" is :"<<f<<endl;
}
void expo()
{
long double res=1,p;
double e=2.718281828;
cout<<"enter power of exponential function:";
cin>>p;
for(int i=1;i<=p;i++)
{
res=e*res;
}
cout<<" e^ "<<p<<" is :"<<res<<endl;
}
int main()
{
system("cls");
do
{
system("pause");
system("cls");
cout<<"***which operation you want to perform***\n";
cout<<"press 0 for exit\n";
cout<<"press 1 for addition \n";
cout<<"press 2 for subtraction\n";
cout<<"press 3 for multiplication\n";
cout<<"press 4 for division\n";
cout<<"press 5 for power calculation\n";
cout<<"press 6 for square root \n";
cout<<"press 7 for factorial calculation\n";
cout<<"press 8 for exponential calculation\n";
cout<<"press option:";
cin>>op;
switch(op)
{
case '1':
sum();
break;
case '2':
diff();
break;
case '3':
pro();
break;
case '4':
div();
break;
case '5':
power();
break;
case '6':
sq();
break;
case '7':
fact();
break;
case '8':
expo();
break;
case '0':
exit(0);
default:
cout<<"invalid input" ;
system("cls");
}
}
while(op!='0');
getch();
}
Hey I'm still a newbie on c++ but hope this would help, I used while loop to loop the calculator but I don't have a good error handler for example when users try to input a letter instead of numbers.
#include <iostream>
#include <cmath>
using namespace std;
int result (int a, int b, char op);
int main()
{
char optr, choice;
int nr1, nr2;
while (true){
cout << "Enter first number: ";
cin >> nr1;
cout << "Enter an operator (+ - / * %) : ";
cin >> optr;
cout << "Enter second number: ";
cin >> nr2;
result (nr1, nr2, optr);
cout<<"Would you like to perform other calculation?(Y/N): ";
cin >> choice;
if (choice =='N'||choice =='n'){
break;
}
}
}
int result (int a, int b, char op)
{
int result;
if (op == '+'){
result = a + b;
cout << "Result to " << a << " + " << b << " = " << result << endl;
} else if (op == '-'){
result = a - b;
cout << "Result to " << a << " + " << b << " = " << result << endl;
} else if (op == '*'){
result = a * b;
cout << "Result to " << a << " * " << b << " = " << result << endl;
} else if (op == '/'){
result = a / b;
a / b;
cout << "Result to " << a << " / " << b << " = " << result << endl;
} else if (op == '%'){
result = a % b;
cout << "Remainder to " << a << " % " << b << " = " << result << endl;
} else {
cout <<"Error 404: " << a << op << b <<" Wrong input format. Program terminated." << endl;
// i still dont know how to properly use error handling
}
}
I won't criticize your solution as I have a better solution as you mentioned "please mention a better solution in answer"
Here's my solution with comments to make you understand what each statement does.
#include <iostream>
using namespace std;
// I am going to show How to Design a program
// We have to break down our progress a bit by bit
// and do the progress of one thing with function
// For calculator we have to take input from user keyboard twice,
// And an opperator from user
int userInput() // this requires to get input from user keyboard
{
cout << "Enter a number: ";
int no{};
cin >> no;
return no;
}
char userOpr() // Using type char to store the ASCI Opperator means requires char to store +,*,/,-
{
cout << "Enter opperator: ";
char opr{};
cin >> opr;
return opr;
}
int calculate(int input1, char opper, int input2)
{
if (opper == '+')
{
return input1 + input2;
}
else if (opper == '-')
{
return input1 - input2;
}
else if (opper == '*')
{
return input1 * input2;
}
else if (opper == '/')
{
return input1 / input2;
}
return 0;
}
int main()
{
// get the first no. from user
// getUserInput();
// get the math oppperator from the user
// getMathOpperator();
// get the second no. from the user
// getUserInput();
// calculate the values
// calculateResults();
// print out the results
// printResults();
cout << "Hello This is a simple calculator program Designed by Shankhui!\n\n";
while (true)
{
int input1{ userInput() };
int input2{ userInput() };
char opper{ userOpr() }; // Using type char to store the ASCI Opperator means requires char to store +,*,/,-
cout << input1 << " " << opper << " " << input2 << " = " << calculate(input1, opper, input2) << '\n' << '\n';
}
return 0;
}
Just copy this code and modify however you want to.
#include <iostream>
int main() {
int math_ques_1, math_ques_2;
std::cout << "What is the base of the question?" << std::endl;
std::cin >> math_ques_1;
std::cout << "What do you want to add to the base?" << std::endl;
std::cin >> math_ques_2;
std::cout << "The answer is " << math_ques_1 + math_ques_2 << "." << std::endl; }

C++ swap function with array of records

I have just started to learn C++ and have been working on a few problems to hone my skills. Currently I am having a problem swapping some values of my array of records. The input validation works fine but then when I try to swap the values around the program stops responding and crashes. Here is how I have created it:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int grandprixnum;
struct DriverData {
string driver;
int car;
string team;
int grid;
int points;
};
DriverData * grand = new DriverData[grandprixnum];
int input()
{
cout << "How many drivers where there? ";
cin >> grandprixnum;
cin.sync();
DriverData * grand = new DriverData[grandprixnum];
for (int i=0; i<grandprixnum; i++)
{
cout << "Driver numbers: "<< i+1 << " \n";
cout << "What is the drivers name? \n";
getline (cin, grand[i].driver);
cin.sync();
cout << "What is the drivers car number? \n";
cin >> grand[i].car;
while (grand[i].car > 99 || grand[i].car < 1)
{
cout << "Please enter a value between 1 and 99! \n";
cin >> grand[i].car;
}
cin.sync();
cout << "What team is the driver racing for? \n";
getline (cin, grand[i].team);
cin.sync();
cout << "What grid are they in? \n";
cin >> grand[i].grid;
cin.sync();
while (grand[i].grid < 0 || grand[i].grid > 22)
{
cout << "Please enter a grid number between 1 and 22! \n";
cin >> grand[i].grid;
}
cin.sync();
cout << "What are their total points? \n";
cin >> grand[i].points;
cin.sync();
while (grand[i].points > 25 || grand[i].points < 0)
{
cout << "Please enter the drivers points between 0 and 25! \n";
cin >> grand[i].points;
}
}
}
int sorting ()
//This part _______________________________
{
for(int a=1; a<=grandprixnum; a++)
{
for(int b=0; b<=grandprixnum; b++)
{
if(grand[b].points < grand[b+1].points)
{
swap(grand[b].driver, grand[b+1].driver);
swap(grand[b].car, grand[b+1].car);
swap(grand[b].team, grand[b+1].team);
swap(grand[b].grid, grand[b+1].grid);
swap(grand[b].points, grand[b+1].points);
}
}
}
}
//To here_________________________________
int showtable ()
{
cout << "Driver Car Team Grid Points \n";
for(int c=0; c<grandprixnum; c++)
{
cout << grand[c].driver << grand[c].car << grand[c].team << grand[c].grid << grand[c].points << "\n";
}
}
int main()
{
input ();
sorting ();
showtable ();
}
I have looked around and cannot find an example or someone having the same problem as me. If someone could show me what is wrong with it. Thank you in advance.
EDIT: I have tested the swap before and it does work but it seems to struggle with the array of records.
You access to your array out of bounds. Your array has a length of grandprixnum so you can access to elments from 0 to grandprixnum-1
for(int a=1; a < grandprixnum; a++)
// ^
{
for(int b=0; b < grandprixnum-1; b++)
// ^ ^^ -1 because of b+1
{
if(grand[b].points < grand[b+1].points)
{
...
}
}
}
If ter is nothing to return in a function you dont need a return type. Use void sorting(), void input(), void showtable()
You declareted your array grand twice. On time global and second time local in function input. Declare it global and allocate it in function input.
DriverData * grand = NULL;
int input()
{
...
grand = new DriverData[grandprixnum];
Remove equal sign from this line:
for(int b=0; b<=grandprixnum; b++)
So it will be like this:
for(int b=0; b<grandprixnum; b++)
And replace this line
DriverData * grand = new DriverData[grandprixnum];
with
grand = new DriverData[grandprixnum]; // will store in global variable

How to store user input in an int array

I need to ask the user to input 8 zip codes and then store them in an array of integers and then to output them one by one , each being in a new line.These two things should be done in separate functions. But when it runs the code first time it shows only menu, then second time in loop when i enter L and input 8 zipcodes it shows this error
Enter your choice: libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string(in here optionInChar=optionInString.at(0);)
using namespace std;
void DisplayCityZipCodes();
int LoadCityZipCodes(int ZipCodes[],int SIZE);
void DisplayCityZipCodes(int ZipCodes[],int SIZE);
void DisplayMenu();
char GetOption();
int main(){
int const SIZE=8;
int ZipCodes[SIZE]={0};
bool moreWork=true;
option=GetOption();
while(moreWork) {
DisplayMenu();
option=GetOption();
switch(option){
case 'L':
ZipCodes[SIZE]= LoadCityZipCodes(ZipCodes, SIZE);
break;
cout<<"D";
case 'D': DisplayCityZipCodes(ZipCodes, SIZE);
break;
}
}
}
void DisplayMenu(){
cout<<" **********************\n\n";
cout<<" San Jose City Zip codes\n\n";
cout<<" **********************\n\n";
cout<<"1. Load City zip codes\n";
cout<<"2. Display all City zip codes\n";
cout<<"3. Search a City zip code\n";
cout<<"4. Reverse the City zip code List\n";
cout<<"5. Quit\n";
}
char GetOption(){
string optionInString="";
char optionInChar='a';
cout<<"\n\nEnter your choice: ";
getline(cin,optionInString);
optionInChar=optionInString.at(0);
cout<<"\n";
return optionInChar;
}
int LoadCityZipCodes(int ZipCodes[],int SIZE){
cout<<"PLease enter 8 city Zip Codes ";
int i=0;
for(;i<8;i=i+1){
cin >>ZipCodes[i];
}
return ZipCodes[i];
}
void DisplayCityZipCodes(int ZipCodes[],int SIZE){
int i=0;
for(;i<8;i=i+1){
cout<<ZipCodes[i]<<endl;
}
}
Now I have not had a hand on C++ for a long time, but this will work.
You should look on the variable declarations and some other things though.
#include<iostream>
#include<conio.h>
using namespace std;
void DisplayCityZipCodes();
void LoadCityZipCodes();
void DisplayMenu();
int const SIZE = 8;
int ZipCodes[SIZE];
int main()
{
DisplayMenu();
return 0;
}
void DisplayMenu()
{
int ch;
cout << " **********************\n\n";
cout << " San Jose City Zip codes\n\n";
cout << " **********************\n\n";
do
{
cout << "1. Load City zip codes\n";
cout << "2. Display all City zip codes\n";
cout << "3. Search a City zip code\n";
cout << "4. Reverse the City zip code List\n";
cout << "5. Quit\n";
cout << "\nPlease enter your choice:";
cin >> ch;
switch (ch)
{
case 1:
LoadCityZipCodes();
break;
case 2:
DisplayCityZipCodes();
break;
case 5:
exit(0); //You will need to include math.h for this.
default:
cout << "please enter a proper choice!";
break;
}
} while (1);
}
void LoadCityZipCodes()
{
cout << "PLease enter 8 city Zip Codes ";
for (int i=0; i<8; i++)
{
cin >> ZipCodes[i];
}
}
void DisplayCityZipCodes()
{
for (int i=0; i<8; i++)
{
cout << ZipCodes[i] << endl;
}
}
You can add remaining of your functions with case 3 and 4.
Try to debug the code and see how it work before you post :)
You need to look closer at the statements
return ZipCodes[i];
and
ZipCodes[SIZE]= LoadCityZipCodes(ZipCodes, SIZE);
In both cases you use ZipCodes[8] which is definitely out of range.
To solve both problems, don't return anything from LoadCityZipCodes (i.e. make it return void), because it already sets the values in the array.
You need to remeber that when programming we start counting at 0. That means for an array that has the size 8 all the valid indexes are: 0, 1, 2, 3, 4, 5, 6, 7. When you try to access your array with ZipCodes[8] you are asking for the 9th element which is out of range.

c++ memory leak on my data structure

So I've graduated from learning C and now learning C++. From what I understand C and C++ are similar in variety of ways so I'm trying to re-write my past C project files into C++. However I keep getting a memory leak when I try to print out my information. Can someone tell me why I am getting a memory leak in my code.
struct.h
typedef struct student_info {
char last[10];
char first[10];
int student_id;
int count_student;
} student;
typedef struct course_info {
char name[10];
int course_id;
int count_course;
student *students;
} course;
typedef struct gradebook_info {
course *courses;
} gradebook;
function.c
void new_course(gradebook *info) {
int i, loop=0;
cout << "Enter Number of Courses " ;
cin >> loop;
for(i=0; i<loop; i++) {
cout << "Enter Course ID ";
cin >> info->courses[info->courses->count_course].course_id;
cout << "Enter Course Name ";
cin >> info->courses[info->courses->count_course].name;
info->courses->count_course++;
}
}
void new_student(gradebook *info) {
int i, loop=0;
cout << "Enter Number of Students " ;
cin >> loop;
for(i=0; i<loop; i++) {
cout << "Enter Student ID ";
cin >> info->courses->students[info->courses->students->count_student].student_id;
cout << "Enter Last Name ";
cin >> info->courses->students[info->courses->students->count_student].last;
cout << "Enter First Name ";
cin >> info->courses->students[info->courses->students->count_student].first;
info->courses->students->count_student++;
}
}
void printCourse(gradebook *info) {
int i;
cout << "Course ID\tCourse Name\t" << endl;
for(i=0; i<info->courses->count_course; i++) {
cout << info->courses[i].course_id << "\t\t";
cout << info->courses[i].name << endl;
}
}
void printStudent(gradebook *info) {
int i;
cout << "Student ID\tLast Name\tFirst Name\t" << endl;
for(i=0; i<info->courses->students->count_student; i++) {
cout << info->courses->students[i].student_id << "\t\t";
cout << info->courses->students[i].last << "\t\t";
cout << info->courses->students[i].first << endl;
}
}
When I run new_course() function it works.
I run my printCourse() function it works.
I run new_student() function it works.
When I run printStudent() function it works.
Then I try to run printCourse() function again and when i=2, I get some information from my struct student.
I can't figure it out. Any help is appreciated.
main.c
gradebook *info = new gradebook; //allocate memory
do {
main_menu();
int option=0;
switch(option) {
case 1: new_course(info);
break;
case 2: new_student(info);
break;
case 3: printCourse(info);
break;
case 4: printStudent(info);
break;
}
}while(option < 13);
delete(info);
You need to allocate the pointers students and course using new.
After getting loop:
students = new student_info* [loop]
and
courses= new course_info* [loop]
You have not only to allocate memory for the gradebook, but also for the courses and the students. For example:
gradebook *info = new gradebook;
info->courses = new course_info[10];
for(int i = 0; i < 10; i++)
info->courses[i]->students = new student_info[10];
Beware, this example limits the number of courses and students per course to 10.
P.S.: Your code might work for one or two iterations if you don't allocate memory for the courses and the students. But in this case you screw up your memory.

How to use looping in C++

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
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;
#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;
}