How to execute a another c++ program when an option is chosen? - c++

I have this code when executed it will let the user choose an option. once the user enters an option the program will be cleared and execute another program. below is the sample. at the bottom is another program of option 1
#include <iostream>
using namespace std;
int main()
{
int a;
cout<<"Please choose an option below: \n";
cout<<"1. Area of Shapes\n";
cout<<"2. Cost of your items\n";
cout<<"3. Flood Control\n";
cout<<"4. Fibonacci Numbers\n";
cout<<"5. Addition Table\n";
cout<<"6. Exit\n";
cin>> a;
system("pause");
return 0;
}
Here is the program for option 1:
#include <iostream>
using namespace std;
float circle (float a)
{
float z;
z = 3.141593*(a*a);
return (z);
}
float square (float b)
{
float y;
y = b * b;
return (y);
}
float rectangle (float c, float d)
{
float x;
x = c * d;
return (x);
}
float triangle (float e, float f)
{
float w;
w = (e * f) / 2;
return (w);
}
void exit ()
{
cout << "THANK YOU! GOODBYE!" <<endl;
}
int main()
{
float n;
float l;
float m;
float radius;
float side;
float length;
float width;
float base;
float height;
do
{
cout << "1 => Area of Circle" <<endl;
cout << "2 => Area of Square" <<endl;
cout << "3 => Area of Rectangle" <<endl;
cout << "4 => Area of Triangle" <<endl;
cout << "0 => Exit" <<endl;
cout << "Please enter number of your choice: ";
cin >> n;
{
if (n==0)
{
exit ();
system("pause");
return 0;
}
else if (n==1)
{
cout << "Enter radius of the circle: ";
cin >> radius;
l = circle (radius);
cout << "Area of the circle is: " <<l <<endl;
}
else if (n==2)
{
cout << "Enter side of the square: ";
cin >> side;
cout << "Area of the square is: " <<square (side) <<endl;
}
else if (n==3)
{
cout << "Enter length of the rectangle: ";
cin >> length;
cout << "Enter width of the rectangle: ";
cin >> width;
m = rectangle (length, width);
cout << "Area of the rectangle is: " <<m <<endl;
}
else if (n==4)
{
cout << "Enter base of the triangle: ";
cin >> base;
cout << "Enter height of the triangle: ";
cin >> height;
cout << "Area of the triangle is: " <<triangle (base, height) <<endl;
}
else
cout << "Invalid number. Please enter a valid number below" <<endl;
}
}
while (n!=0);
cout <<endl <<endl;
system("pause");
return 0;
}

If you truely want to replace the current program with another, look to the exec family of system calls.

Put each "program" in its own file (or not, but it's a good idea to keep them separate).
Rename each "program"'s main to something meaningful, like "areas".
Declare that function in a header.
Include that header in your "controller" program.
Call the corresponding "program function" from the "controller" based on the input you read.

Related

Why is my calculator not responding to my if statements?

Newbie programmer here. I'm finished with this project I was working on, and all of my if and else statements function properly, but only when they are placed at the very top of the code I'm writing.
For example, I started writing this code from if(number == 1) and worked my way down, but I eventually had to cut and paste them at the top, because when I entered a value that wasn't 1 (the first if statement, and thus the top statement that functions), the calculator failed to print anything at all when inputting a value for anything else except the top one. Because 4 is at the top, it works fine, but 1-3 don't work, and if I was to put 1 at the top then 2-4 would not print anything.
Here is a screeshot of the if statements on the bottom that do not print, and also have no debugging signs:
I'm sure it's a small issue that I managed to turn big.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int number;
float area1, area2, area3, area4;
int R1;
int trib;
int trih;
int top;
int bott;
int traph;
int majxis;
int minxis;
const float PI= 3.14159;
cout<< "I'm starting my program ";
cout<<"\n1.Circle";
cout<<"\n2.triangle";
cout<<"\n3.trapezoid";
cout<<"\n4.ellipse ";
cout<<"\nEnter the corresponding number of your choice(1-4):";
cin>> number;
if(number <= 0)
{
cout<<"Integer is invalid, try again ";
cin>> number;
}
if(number == 4)
{
cout<<"\nWhat is the length of the semi-major axis of the ellipse? ";
}
cin>> majxis;
if(majxis <=0)
{
cout<<"Invalid value for the semi-major axis, try again: ";
}
else
{
cout<<"What is the length for the semi-minor axis of the ellipse? ";
}
cin>> minxis;
area4= PI*minxis*majxis;
if(minxis <=0)
{
cout<<"Invalid value for the semi-minor axis, try again: ";
}
else
{
cout<<"The area of an ellipse with a semi-major axis of " << majxis<< "and a semi-minor axis of " << minxis << "is " << area4;
}
if(number == 3)
{
cout<<"\nWhat is the length of the top of the trapezoid? ";
}
cin>> top;
if(top <=0 )
{
cout<<"\n Invalid value for top length of trapezoid, try again";
}
else
{
cout<<"\nWhat is the length of the bottom of the trapezoid? ";
}
cin>> bott;
if (bott <= 0)
{
cout<<"Invalid value for bottom length of trapezoid, try again: ";
}
else
{
cout<<"What is the height of the trapezoid? ";
}
cin>>traph;
area3 = (top+bott)*0.5+traph;
if (traph <= 0)
{
cout<<"Invalid value for height of trapezoid";
}
else
{
cout<<"The area of a trapezoid with a bottom of " << bott << "and a top of " << top << "is " << area3;
}
if(number == 2)
{
cout<<"\nWhat is the length of the base of the triangle?";
}
cin>> trib;
if(trib<=0)
{
cout<<"\nInvalid Base of triangle, try again:";
}
else
{
cout<<"\nWhat is the height of the triangle? ";
}
cin>> trih;
area2 = (0.5*trib)*trih;
if(trih<=0)
{
cout<<"\nInvalid height of triangle, try again:";
}
else
{
cout<<"\nThe area of a trangle with a base of " << trib << "and a height of "<< trih <<" is " << area2;
}
if(number == 1)
{
cout<<"\nWhat is the radius of the circle?";
}
cin>> R1;
area1 = PI*(R1 *R1);
if( R1 <=0)
{cout<<"\nRadius must be greater than 0, try again";
cin>> R1;
}
else
{
cout<< "\nThe area of a circle with a radius of " << R1 << " is" << area1;
}
}
It indeed is a small issue. Here is the correct program:
#include <iostream>
#include <iomanip>
int main()
{
int number;
float area1, area2, area3, area4;
int R1;
int trib;
int trih;
int top;
int bott;
int traph;
int majxis;
int minxis;
const float PI = 3.14159;
std::cout << "I'm starting my program ";
std::cout << "\n1.Circle";
std::cout << "\n2.triangle";
std::cout << "\n3.trapezoid";
std::cout << "\n4.ellipse ";
std::cout << "\nEnter the corresponding number of your choice(1-4):";
std::cin >> number;
if (number <= 0)
{
std::cout << "Integer is invalid, try again ";
std::cin >> number;
}
else if (number == 1)
{
std::cout << "\nWhat is the radius of the circle?";
std::cin >> R1;
area1 = PI * (R1 * R1);
if (R1 <= 0)
{
std::cout << "\nRadius must be greater than 0, try again";
std::cin >> R1;
}
else
{
std::cout << "\nThe area of a circle with a radius of " << R1 << " is" << area1;
}
}
else if (number == 2)
{
std::cout << "\nWhat is the length of the base of the triangle?";
std::cin >> trib;
if (trib <= 0)
{
std::cout << "\nInvalid Base of triangle, try again:";
}
else
{
std::cout << "\nWhat is the height of the triangle? ";
}
std::cin >> trih;
area2 = (0.5 * trib) * trih;
if (trih <= 0)
{
std::cout << "\nInvalid height of triangle, try again:";
}
else
{
std::cout << "\nThe area of a trangle with a base of " << trib << "and a height of " << trih << " is " << area2;
}
}
else if (number == 3)
{
std::cout << "\nWhat is the length of the top of the trapezoid? ";
std::cin >> top;
if (top <= 0)
{
std::cout << "\n Invalid value for top length of trapezoid, try again";
}
else
{
std::cout << "\nWhat is the length of the bottom of the trapezoid? ";
}
std::cin >> bott;
if (bott <= 0)
{
std::cout << "Invalid value for bottom length of trapezoid, try again: ";
}
else
{
std::cout << "What is the height of the trapezoid? ";
}
std::cin >> traph;
area3 = (top + bott) * 0.5 + traph;
if (traph <= 0)
{
std::cout << "Invalid value for height of trapezoid";
}
else
{
std::cout << "The area of a trapezoid with a bottom of " << bott << "and a top of " << top << "is " << area3;
}
}
else if (number == 4)
{
std::cout << "\nWhat is the length of the semi-major axis of the ellipse? ";
std::cin >> majxis;
if (majxis <= 0)
{
std::cout << "Invalid value for the semi-major axis, try again: ";
}
else
{
std::cout << "What is the length for the semi-minor axis of the ellipse? ";
}
std::cin >> minxis;
area4 = PI * minxis * majxis;
if (minxis <= 0)
{
std::cout << "Invalid value for the semi-minor axis, try again: ";
}
else
{
std::cout << "The area of an ellipse with a semi-major axis of " << majxis << "and a semi-minor axis of " << minxis << "is " << area4;
}
}
}
Explanation: Let's take an example of the area of eclipse (number == 4). In your code, you have made the following if condition:
if(number == 4)
{
cout<<"\nWhat is the length of the semi-major axis of the ellipse? ";
}
This means if number == 4 then print - "\nWhat is the length of the semi-major axis of the ellipse? " but all of the code after it is outside the if condition's {} brackets, and thus is not considered as a part of the if statement. It will be called anyways even if number != 4. Now, in my code, I've done this:
else if (number == 4)
{
std::cout << "\nWhat is the length of the semi-major axis of the ellipse? ";
std::cin >> majxis;
if (majxis <= 0)
{
std::cout << "Invalid value for the semi-major axis, try again: ";
}
else
{
std::cout << "What is the length for the semi-minor axis of the ellipse? ";
}
std::cin >> minxis;
area4 = PI * minxis * majxis;
if (minxis <= 0)
{
std::cout << "Invalid value for the semi-minor axis, try again: ";
}
else
{
std::cout << "The area of an ellipse with a semi-major axis of " << majxis << "and a semi-minor axis of " << minxis << "is " << area4;
}
}
Here, all of the code related to the area of the eclipse is all part of the if (number == 4) statement because it is present between the brackets {} of the if (number == 4) statement.
Apply this to all 4 cases and you get the correct program.
Also, it would be better to refactor your code as such:
#include <iostream>
#include <iomanip>
const float PI = 3.14159;
void areaof_circle()
{
float area;
int R1;
std::cout << "\nWhat is the radius of the circle?";
std::cin >> R1;
area = PI * (R1 * R1);
if (R1 <= 0)
{
std::cout << "\nRadius must be greater than 0, try again";
std::cin >> R1;
}
else
{
std::cout << "\nThe area of a circle with a radius of " << R1 << " is" << area;
}
}
void areaof_triangle()
{
float area;
int trib, trih;
std::cout << "\nWhat is the length of the base of the triangle?";
std::cin >> trib;
if (trib <= 0)
{
std::cout << "\nInvalid Base of triangle, try again:";
}
else
{
std::cout << "\nWhat is the height of the triangle? ";
}
std::cin >> trih;
area = (0.5 * trib) * trih;
if (trih <= 0)
{
std::cout << "\nInvalid height of triangle, try again:";
}
else
{
std::cout << "\nThe area of a trangle with a base of " << trib << "and a height of " << trih << " is " << area;
}
}
void areaof_trapezoid()
{
float area;
int top, bott, traph;
std::cout << "\nWhat is the length of the top of the trapezoid? ";
std::cin >> top;
if (top <= 0)
{
std::cout << "\n Invalid value for top length of trapezoid, try again";
}
else
{
std::cout << "\nWhat is the length of the bottom of the trapezoid? ";
}
std::cin >> bott;
if (bott <= 0)
{
std::cout << "Invalid value for bottom length of trapezoid, try again: ";
}
else
{
std::cout << "What is the height of the trapezoid? ";
}
std::cin >> traph;
area = (top + bott) * 0.5 + traph;
if (traph <= 0)
{
std::cout << "Invalid value for height of trapezoid";
}
else
{
std::cout << "The area of a trapezoid with a bottom of " << bott << "and a top of " << top << "is " << area;
}
}
void areaof_ellipse()
{
float area;
int majxis, minxis;
std::cout << "\nWhat is the length of the semi-major axis of the ellipse? ";
std::cin >> majxis;
if (majxis <= 0)
{
std::cout << "Invalid value for the semi-major axis, try again: ";
}
else
{
std::cout << "What is the length for the semi-minor axis of the ellipse? ";
}
std::cin >> minxis;
area = PI * minxis * majxis;
if (minxis <= 0)
{
std::cout << "Invalid value for the semi-minor axis, try again: ";
}
else
{
std::cout << "The area of an ellipse with a semi-major axis of " << majxis << "and a semi-minor axis of " << minxis << "is " << area;
}
}
int main()
{
int number;
const float PI = 3.14159;
std::cout << "I'm starting my program ";
std::cout << "\n1.Circle";
std::cout << "\n2.triangle";
std::cout << "\n3.trapezoid";
std::cout << "\n4.ellipse ";
std::cout << "\nEnter the corresponding number of your choice(1-4):";
std::cin >> number;
if (number <= 0)
{
std::cout << "Integer is invalid, try again ";
std::cin >> number;
}
else if (number == 1)
{
areaof_circle();
}
else if (number == 2)
{
areaof_triangle();
}
else if (number == 3)
{
areaof_trapezoid();
}
else if (number == 4)
{
areaof_ellipse();
}
}
Also, don't use the following in your programs:
using namespace std;
...as it's considered as bad practice. Use std:: every time instead.
There is a problem with your if statement..
as the well defined answer is already given, I am simplifying your code's readability and suggesting more better way to do this..
NOTE: Considering you're new bie.
Create Functions to handle different quadrilaterals
#include <iostream>
#include <iomanip>
using namespace std;
float area1, area2, area3, area4;
int R1;
int trib;
int trih;
int top;
int bott;
int traph;
int majxis;
int minxis;
const float PI= 3.14159;
void handle_circle();
void handle_ellipse();
void handle_triangle();
void handle_trapizoid();
int main()
{
int number;
cout<< "I'm starting my program ";
cout<<"\n1.Circle";
cout<<"\n2.triangle";
cout<<"\n3.trapezoid";
cout<<"\n4.ellipse ";
cout<<"\nEnter the corresponding number of your choice(1-4):";
cin>> number;
if(number <= 0)
{
cout<<"Integer is invalid, try again ";
cin>> number;
}
if(number == 4)
{
handle_ellipse();
}
if(number == 3)
{
handle_trapizoid();
}
if(number == 2)
{
handle_triangle();
}
if(number == 1)
{
handle_circle();
}
}
void handle_trapizoid()
{
cout<<"\nWhat is the length of the top of the trapezoid? ";
cin>> top;
if(top <=0 )
{
cout<<"\n Invalid value for top length of trapezoid, try again";
}
else
{
cout<<"\nWhat is the length of the bottom of the trapezoid? ";
cin>> bott;
}
if (bott <= 0)
{
cout<<"Invalid value for bottom length of trapezoid, try again: ";
}
else
{
cout<<"What is the height of the trapezoid? ";
cin>>traph;
area3 = (top+bott)*0.5+traph;
}
if (traph <= 0)
{
cout<<"Invalid value for height of trapezoid";
}
else
{
cout<<"The area of a trapezoid with a bottom of " << bott << "and a top of " << top << "is " << area3;
}
}
void handle_ellipse()
{
cout<<"\nWhat is the length of the semi-major axis of the ellipse? ";
cin>> majxis;
if(majxis <=0)
{
cout<<"Invalid value for the semi-major axis, try again: ";
}
else
{
cout<<"What is the length for the semi-minor axis of the ellipse? ";
cin>> minxis;
area4= PI*minxis*majxis;
}
if(minxis <=0)
{
cout<<"Invalid value for the semi-minor axis, try again: ";
}
else
{
cout<<"The area of an ellipse with a semi-major axis of " << majxis<< "and a semi-minor axis of " << minxis << "is " << area4;
}
}
void handle_triangle()
{
cout<<"\nWhat is the length of the base of the triangle?";
cin>> trib;
if(trib<=0)
{
cout<<"\nInvalid Base of triangle, try again:";
}
else
{
cout<<"\nWhat is the height of the triangle? ";
cin>> trih;
area2 = (0.5*trib)*trih;
}
if(trih<=0)
{
cout<<"\nInvalid height of triangle, try again:";
}
else
{
cout<<"\nThe area of a trangle with a base of " << trib << "and a height of "<< trih <<" is " << area2;
}
}
void handle_circle()
{
cout<<"\nWhat is the radius of the circle?";
cin>> R1;
area1 = PI*(R1 *R1);
if( R1 <=0)
{cout<<"\nRadius must be greater than 0, try again";
cin>> R1;
}
else
{
cout<< "\nThe area of a circle with a radius of " << R1 << " is" << area1;
}
}
Also you can use switch statement as it is more faster than if statements
#include <iostream>
#include <iomanip>
using namespace std;
float area1, area2, area3, area4;
int R1;
int trib;
int trih;
int top;
int bott;
int traph;
int majxis;
int minxis;
const float PI= 3.14159;
void handle_circle();
void handle_ellipse();
void handle_triangle();
void handle_trapizoid();
int main()
{
int number;
cout<< "I'm starting my program ";
cout<<"\n1.Circle";
cout<<"\n2.triangle";
cout<<"\n3.trapezoid";
cout<<"\n4.ellipse ";
cout<<"\nEnter the corresponding number of your choice(1-4):";
cin>> number;
switch(number)
{
case 1:
handle_circle();
break;
case 2:
handle_triangle();
break;
case 3:
handle_ellipse();
break;
case 4:
handle_trapizoid();
break;
default:
{
cout<<"Integer is invalid, try again ";
}
}
}
void handle_trapizoid()
{
cout<<"\nWhat is the length of the top of the trapezoid? ";
cin>> top;
if(top <=0 )
{
cout<<"\n Invalid value for top length of trapezoid, try again";
}
else
{
cout<<"\nWhat is the length of the bottom of the trapezoid? ";
cin>> bott;
}
if (bott <= 0)
{
cout<<"Invalid value for bottom length of trapezoid, try again: ";
}
else
{
cout<<"What is the height of the trapezoid? ";
cin>>traph;
area3 = (top+bott)*0.5+traph;
}
if (traph <= 0)
{
cout<<"Invalid value for height of trapezoid";
}
else
{
cout<<"The area of a trapezoid with a bottom of " << bott << "and a top of " << top << "is " << area3;
}
}
void handle_ellipse()
{
cout<<"\nWhat is the length of the semi-major axis of the ellipse? ";
cin>> majxis;
if(majxis <=0)
{
cout<<"Invalid value for the semi-major axis, try again: ";
}
else
{
cout<<"What is the length for the semi-minor axis of the ellipse? ";
cin>> minxis;
area4= PI*minxis*majxis;
}
if(minxis <=0)
{
cout<<"Invalid value for the semi-minor axis, try again: ";
}
else
{
cout<<"The area of an ellipse with a semi-major axis of " << majxis<< "and a semi-minor axis of " << minxis << "is " << area4;
}
}
void handle_triangle()
{
cout<<"\nWhat is the length of the base of the triangle?";
cin>> trib;
if(trib<=0)
{
cout<<"\nInvalid Base of triangle, try again:";
}
else
{
cout<<"\nWhat is the height of the triangle? ";
cin>> trih;
area2 = (0.5*trib)*trih;
}
if(trih<=0)
{
cout<<"\nInvalid height of triangle, try again:";
}
else
{
cout<<"\nThe area of a trangle with a base of " << trib << "and a height of "<< trih <<" is " << area2;
}
}
void handle_circle()
{
cout<<"\nWhat is the radius of the circle?";
cin>> R1;
area1 = PI*(R1 *R1);
if( R1 <=0)
{cout<<"\nRadius must be greater than 0, try again";
cin>> R1;
}
else
{
cout<< "\nThe area of a circle with a radius of " << R1 << " is" << area1;
}
}

switch cases not opening, getting choice through return on a menu function

I am having trouble using a switch inside a do-while loop. menu is displaying, but after selection it is just displaying the menu again rather than opening up the proper switch case. and help would be greatly appreciated. I have tried looking for help and could not seem to find much.
#include <iostream>
using namespace std;
//function prototypes
int DisplayMenu(); //shows menu and returns input
double CalcAreaCircle(double radius ); //returns the area of the circle
double CalcAreaRectangle(double length, double width ); //returns the area of a rectangle
double CalcAreaTriangle(double base, double height ); //returns the area of a triangle
int Choice;
double AreaOfCircle;
double radius;
double AreaOfRectangle;
double length;
double width;
double AreaOfTriangle;
double base;
double height;
//function main
int main()
{
Choice = -1;
while (Choice != 4)
{
Choice = DisplayMenu();
switch (Choice)
{
case '1':
{
cout << "What is the radius of the circle?" << endl;
cin >> radius;
cout << endl;
AreaOfCircle = CalcAreaCircle(radius);
cout << endl << "The area of your circle is " << AreaOfCircle << endl;
break;
}
case '2':
{
cout << "what is the length of the rectangle?" << endl;
cin >> length;
cout << endl << "What is the width of the rectangle?" << endl;
cin >> width;
cout << endl;
AreaOfRectangle = CalcAreaRectangle(length, width);
cout << endl << "The area of your rectangle is " << AreaOfRectangle << endl;
break;
}
case '3':
{
cout << "What is the base of the triangle?" << endl;
cin >> base;
cout << endl << "What is the height of the triangle?" << endl;
cin >> height;
cout << endl;
AreaOfTriangle = CalcAreaTriangle(base, height);
cout << endl << "The area of your triangle is " << AreaOfTriangle << endl;
break;
}
}
}
system ("pause");
return 0;
}
//function DisplayMenu
int DisplayMenu()
{
int selection;
cout << "What would you like to know the area of?" << endl;
cout << "\t1. Area of a Circle." << endl;
cout << "\t2. Area of a Rectangle." << endl;
cout << "\t3. Area of a Triangle." << endl;
cout << "\t4. Quit." << endl;
cin >> selection;
while (selection < 1 || selection > 4)
{
cout << "Please enter a valid option." << endl;
cin >> selection;
cout << endl;
}
return selection;
}
//function CalcAreaCircle
double CalcAreaCircle(double radius)
{
double area;
const double PI = 3.14159;
area = PI * (area * area);
return area;
}
//function CalcAreaRectangle
double CalcAreaRectangle(double length, double width)
{
double area;
area = length * width;
return area;
}
//function CalcAreaTriangle
double CalcAreaTriangle(double base, double height)
{
double area;
area = base * height;
return area;
}
DisplayMenu() returns int. But your case statements are using char literals. When comparing char to int, it uses the character's code, e.g. case '1': is equivalent case 49:. Change your cases to use integer literals.
case 1:
and so on.

Basic Console Calculator (Storing a string in a variable) C++

I'm trying to create a basic console calculator in C++. I'm having a bit of trouble storing a string in a variable from a cin command.
Here is the program for some clarification:
#include <iostream>
using namespace std;
int main()
{
string type_cal;
cout << "Please enter the type of calculation you would like to use: \n";
cout << "1. Addition \n";
cout << "2. Subtraction \n";
cout << "3. Multiplication \n";
cout << "4. Division \n \n";
cin >> type_cal;
if (type_cal = "Addition" or "1")
{
int a;
int b;
int sum;
cout << "Please enter a number to add: \n";
cin >> a;
cout << "Please enter another number: \n";
cin >> b;
sum = a + b;
cout << "The sum of those numbers is: " << sum << endl;
return 0;
}
}
Currently I am in the addition phase since I recently ran into this problem. Quick answers would be appreciated, thank you!
if(type_cal = "Addition" or "1") simply does not make sense.
if(type_cal == "Addition" || type_cal == "1") {
}
Ok I found the problem, or is actually used as || in c++ (thanks aerkenemesis), and = is not the same as == which means equal to (another thanks to Lorehed). Program is working fine now.
For those who are curious, here is the new and revised version of my simple calculator:
#include <iostream>
using namespace std;
float addition();
float subtraction();
float multiplication();
float division();
int main()
{
string type_cal;
cout << "Please enter the type of calculation you would like to use: \n";
cout << "1. Addition " << endl;
cout << "2. Subtraction " << endl;
cout << "3. Multiplication " << endl;
cout << "4. Division" << endl << endl;
cin >> type_cal;
if(type_cal == "Addition")
{
addition();
}
if(type_cal == "Subtraction")
{
subtraction();
}
if(type_cal == "Multiplication")
{
multiplication();
}
if(type_cal == "Division")
{
division();
}
return 0;
}
float addition()
{
float a;
float b;
float sum;
cout << "Please enter a number to add: " << endl;
cin >> a;
cout << "Please enter another number: " << endl;;
cin >> b;
sum = a + b;
cout << "The sum of those numbers is: " << sum << endl;
}
float subtraction()
{
float c;
float d;
float difference;
cout << "Please enter a number to subtract: \n";
cin >> c;
cout << "Please enter another number: \n";
cin >> d;
difference = c - d;
cout << "The difference of those numbers is " << difference << endl;
}
float multiplication()
{
float e;
float f;
float product;
cout << "Please enter a number to multiply: \n";
cin >> e;
cout << "Please enter another number: \n";
cin >> f;
product = e * f;
cout << "The product of those numbers is " << product << endl;
}
float division()
{
float g;
float h;
float quotient;
cout << "Please enter a number to divide: \n";
cin >> g;
cout << "Please enter another number: \n";
cin >> h;
quotient = g / h;
cout << "The quotient of those numbers is " << quotient << endl;
}

Why do my stream input operations get skipped over?

I have this code where in option lists will display when run. my problem is when I enter number 2, the option 2 program doesn't work well. It just go directly to asking the amount paid instead of asking first the cost of purchase.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
float circle (float a)
{
float z;
z = 3.141593 * (a * a);
return (z);
}
float square (float b)
{
float y;
y = b * b;
return (y);
}
float rectangle (float c, float d)
{
float x;
x = c * d;
return (x);
}
float triangle (float e, float f)
{
float w;
w = (e * f) / 2;
return (w);
}
void exit ()
{
cout << "THANK YOU! GOODBYE!" << endl;
}
int main()
{
int x;
do
{
cout << "Please choose an option below: \n";
cout << "1. Area of Shapes\n";
cout << "2. Cost of your items\n";
cout << "3. Flood Control\n";
cout << "4. Fibonacci Numbers\n";
cout << "5. Addition Table\n";
cout << "6. Exit\n";
cin >> x;
if (x == 1)
{
system("cls");
float n;
float l;
float m;
float radius;
float side;
float length;
float width;
float base;
float height;
do
{
cout << "1 => Area of Circle" << endl;
cout << "2 => Area of Square" << endl;
cout << "3 => Area of Rectangle" << endl;
cout << "4 => Area of Trian1gle" << endl;
cout << "5 => Return to Main Menu" << endl;
cout << "0 => Exit" << endl;
cout << "Please enter number of your choice: ";
cin >> n;
system("cls");
{
if (n == 0)
{
exit ();
system("pause");
return 0;
}
else if (n == 1)
{
cout << "Enter radius of the circle: ";
cin >> radius;
l = circle (radius);
cout << "Area of the circle is: " << l << endl;
system("pause");
system("cls");
}
else if (n == 2)
{
cout << "Enter side of the square: ";
cin >> side;
cout << "Area of the square is: " << square (side) << endl;
system("pause");
system("cls");
}
else if (n == 3)
{
cout << "Enter length of the rectangle: ";
cin >> length;
cout << "Enter width of the rectangle: ";
cin >> width;
m = rectangle (length, width);
cout << "Area of the rectangle is: " << m << endl;
system("pause");
system("cls");
}
else if (n == 4)
{
cout << "Enter base of the triangle: ";
cin >> base;
cout << "Enter height of the triangle: ";
cin >> height;
cout << "Area of the triangle is: " << triangle (base, height) << endl;
system("pause");
system("cls");
}
else if (n == 5)
{
exit ();
}
else
cout << "Invalid number. Please enter a valid number below" << endl;
}
}
while (n != 0 && n != 5);
cout << endl << endl;
system("pause");
system("cls");
}
else if (x == 2)
{
system("cls");
string mystr;
float cost = 0;
float amount = 0;
float total;
cout << "Total Cost: P";
getline (cin, mystr);
stringstream(mystr) >> cost;
cout << endl;
total = cost * .06;
cout << "Sales Tax Value: P" << total << endl;
cout << endl;
cout << "Cost of Item: P" << cost + total << endl;
cout << endl;
cout << "Amount Paid: P";
getline (cin, mystr);
stringstream(mystr) >> amount;
cout << endl;
cout << "Total Amount Purchased: P" << cost << endl;
cout << "Sales Tax Value: P" << total << endl;
cout << "Total Amount + Sales Tax: P" << cost + total << endl;
cout << "Total Amount Paid: P" << amount << endl;
cout << "Change: P" << amount - (cost + total) << endl;
system("pause");
cout << endl;
cout << "THANK YOU! ENJOY YOUR MEAL!" << endl;
system("pause");
system("cls");
}
else if (x > 6)
cout << "Invalid Input";
else
{
system("pause");
return 0;
}
}
while (x != 6);
system("pause");
return 0;
}
EDIT
For the posters education
You do
switch (n) {
case 1:
//... Code for n == 1 - If long put into another function. If using local variables put code bloc in braces
break;
case 2:
// Diitto for n==2
default: // No match
// All other values of n not listed above
}
What went wrong
Say you type your menu selection:
2<Enter>
Then the content of the std::cin stream will be:
2\n
When your menu selection runs...
cin >> x;
...it reads a number off the line but doesn't consume any trailing whitespace nor the newline, so the remaining state content could be denoted like this:
\n
Then your code for menu option 2 starts running:
cout << "Total Cost: P";
getline (cin, mystr);
...the getline looks at std::cin and finds the left over \n mentioned above, and says "hey, an empty line - I'll set mystr to an empty string". Notice that it did not do what you'd hoped: namely wait for you to type some more input and read that into mystr.
How to fix it
Before calling getline(cin, mystr) you want to remove the left-over \n typed when entering the menu selection. The code changes for that (adding error handling too):
#include <limits>
...
cout << "Total Cost: P";
std::cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
if (!std::getline(std::cin, mystr))
{
std::cerr << "unable to read mystr\n";
exit(1);
}
std::istringstream iss(mystr);
iss >> cost;
if (!iss)
{
std::cerr << "mystr doesn't contain a valid cost number\n";
exit(1);
}
How you could have found the problem
When you get stuck like this, try adding some "trace" statements to print out the values of variables and find where they differ from your expectation... that can at least give you a better idea how to isolate and describe the problem, and what to google for to fix it.
std::out << "mystr '" << mystr << "'\n";`
Try to use error handling like I've illustrated so the program stops (or prompts for better input) when there's a problem parsing the user's input.

C++ - Simple shape area calculator returns erroneous calculations

#include <iostream>
#include <iomanip>
using namespace std;
class Rectangle
{
float x, y;
public:
void value (float,float);
float area () {return (x*y);}
};
void Rectangle::value (float a,float b)
{
x = a;
y = b;
}
class Circle
{
float x;
public:
void value (float);
float area () {return (3.14*x*x);}
};
void Circle::value (float a)
{
x = a;
}
int main ()
{
float q,a,b;
char reply;
cout << "\t\tArea Calculator";
do
{
cout << "\n\nPlease select from the following: ";
cout << "\n1. Rectangle";
cout << "\n2. Cirlce";
cout << "\n3. Exit";
cout << "\n\n";
cin >> q;
if (q==3)
break;
if (q==1)
{
system("cls");
Rectangle rect;
cout << "\nPlease enter length: ";
cin >> a;
cout << "\nPlease enter width: ";
cin >> b;
cout << "\nArea: " << rect.area();
cin.get();
cout << "\n\nDo you want to continue y/n: ";
cin >> reply;
if (toupper(reply) == 'N')
{
cout << "\n\n";
cout << "Goodbye!";
break;
}
}
if (q==2)
{
system("cls");
Circle circ;
cout << "\nPlease enter radius: ";
cin >> a;
cout << "\nArea: " << circ.area();
cin.get();
cout << "\n\nDo you want to continue y/n: ";
cin >> reply;
if (toupper(reply) == 'N')
{
cout << "\n\n";
cout << "Goodbye!";
break;
}
}
} while (toupper(reply!='Y'));
{
cout << "\n\n";
system("pause");
}
}
The code above, debugs with the following warning:
"warning C4244: 'return' : conversion from double to float, possible loss of data"
... I am pretty sure this is the reason for the mis-calculations when the code is run (for e.g. it returns the area of a 5x5 square as 1.15292e+016) - Please will anyone explain the correct method for resolving this, I can't seem to get my rather-dopey-head around it :(
The wrong result is not a result of double to float conversion but rather a result of not initializing the dimensions members of the Rectangle/Circle objects.
You need to call rect.value(a,b) after reading the values from the user.
Not doing this leaves the object's x and y members uninitialized, therefore containing some arbitrary values - leading to wrong result of the calculation.
Same goes for calling circ.value(a) for the circle.
cout << "\nPlease enter length: ";
cin >> a;
cout << "\nPlease enter width: ";
cin >> b;
rect.value(a,b);
cout << "\nArea: " << rect.area();
The warning on double to float conversion is probably the result of the "cin >> b" style lines. The cin stream >> operator handles reading double, not floats.
When you attempt to read from cin into a float, you first get a double value which is then implicitly cast to float. As float has lesser accuracy than double, the compiler warns you that you may lose precision doing this. Assuming float accuracy is enough - this poses no problem. You can solve this simply by declaring the classes and variable to use double instead of float.
You're not initializing the data members of the class
rect.area(); and circ.area(); are calculating area on some garbage values
Use constructors:
Rectangle(float a, float b):x(a),y(b){}
Circle(float a):x(a){}
Then
cin >> a;
cin >> b;
Rectangle rect(a,b);
rect.area();
and
cin >> a;
Circle circ(a);
circ.area();