So I have written a small program to work out the perimeter of simple shapes (very new to coding so keeping it simple). I am now stuck though
the code works up too triangle and I can't for the life of me work out how to get the last logical operator to work!!!? As always greatly appreciate your time and advice.
Best regards, Jake.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int length;
int Diameter;
float Pi = 3.14;
string Shape;
string Square = "Square";
string Triangle = "Triangle";
string Circle = "Circle";
cout <<"=======================" << endl;
cout <<"=Welcome to Perimeters=" << endl;
cout <<"#######################" << endl;
cout <<"###|Select A Shape|####" << endl;
cout <<"=======================" << endl;
cout <<"= | Circle | =" << endl;
cout <<"= | Triangle | =" << endl;
cout <<"= | Square | =" << endl;
cout <<"=======================" << endl;
cout <<"Enter Shape >; ";
cin >> Shape;
if (Shape == "Square") {
cout << "Enter Length of Side >: ";
cin >> length;
cout << "Perimeter = " ;
cout << length * 4 <<endl;
} else {
(Shape == "Triangle"){
cout << "Enter Length of Side >: ";
cin >> length;
cout << "Perimeter = " ;
cout << length * 3 <<endl;
}
}
else {
(Shape == "Circle") {
cout << "Enter Diameter >: ";
cin >> Diameter;
cout << "Perimeter = " ;
cout << Diameter * Pi <<endl;
}
}
return 0;
}
You are not writing the else-if statement correctly. It should be in the form:
if(boolean expression) {}
else if (boolean expression) {} // as many else ifs as you need
else {} // optional
Therefore your else if conditionals should be:
if (Shape == "Square") {
cout << "Enter Length of Side >: ";
cin >> length;
cout << "Perimeter = " ;
cout << length * 4 <<endl;
} else if (Shape == "Triangle"){
// and so on...
} else {
cout << "Invalid shape entered.";
}
Additionally, PI isn't 3.14. Include <math.h> and use M_PI.
You need to work on your code formatting...
Use else if insead of else.
if (Shape == "Square") {
cout << "Enter Length of Side >: ";
cin >> length;
cout << "Perimeter = " ;
cout << length * 4 <<endl;
}
else if (Shape == "Triangle"){
cout << "Enter Length of Side >: ";
cin >> length;
cout << "Perimeter = " ;
cout << length * 3 <<endl;
}
else if (Shape == "Circle") {
(Shape == "Circle")
cout << "Enter Diameter >: ";
cin >> Diameter;
cout << "Perimeter = " ;
cout << Diameter * Pi <<endl;
}
else{
cout << "invalid shape name" << endl;
}
Hello You have to write else if as below.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int length;
int Diameter;
float Pi = 3.14;
string Shape;
string Square = "Square";
string Triangle = "Triangle";
string Circle = "Circle"; **strong text**
cout <<"=======================" << endl;
cout <<"=Welcome to Perimeters=" << endl;
cout <<"#######################" << endl;
cout <<"###|Select A Shape|####" << endl;
cout <<"=======================" << endl;
cout <<"= | Circle | =" << endl;
cout <<"= | Triangle | =" << endl;
cout <<"= | Square | =" << endl;
cout <<"=======================" << endl;
cout <<"Enter Shape >; ";
cin >> Shape;
if (Shape == "Square") {
cout << "Enter Length of Side >: ";
cin >> length;
cout << "Perimeter = " ;
cout << length * 4 <<endl;
} else if (Shape == "Triangle"){
cout << "Enter Length of Side >: ";
cin >> length;
cout << "Perimeter = " ;
cout << length * 3 <<endl;
} else if (Shape == "Circle") {
cout << "Enter Diameter >: ";
cin >> Diameter;
cout << "Perimeter = " ;
cout << Diameter * Pi <<endl;
}
return 0;
}
Hope this will help you
Related
Good day, I'm having difficulty on the last two parts of my program where it's supposed to only output players who got maximum/minimum scores, I need help on how to do it because I'm really confused. If it's also alright to provide some explanations I'd really appreciate it.
I tried this approach:
#include <iostream>
using namespace std;
int main() {
double lrgst, lrgst2, lrgst3;
int numbers[5];
lrgst = lrgst2 = lrgst3;
for (int i = 0; i < 5; i++) {
cin >> numbers[i];
}
for (int i = 0; i < 5; i++) {
if (numbers[i] > lrgst) {
lrgst3 = lrgst2;
lrgst2 = lrgst;
lrgst = numbers[i];
} else if (numbers[i] > lrgst2) {
lrgst3 = lrgst2;
lrgst2 = numbers[i];
} else if (numbers[i] > lrgst3) {
lrgst3 = numbers[i];
}
}
cout << "largest are: " << lrgst << " " << lrgst2 << " " << lrgst3;
}
this is my actual code:
#include <iostream>
using namespace std;
struct playerdata {
char name[50];
int age, score1, score2;
double average;
};
int main() {
int choice, i = 1, j = 1, z = 1, backtomain2;
char backtomain;
playerdata p1[10];
do {
for (int a = 0; a < 47; a++) {
cout << "=";
}
cout << "\n";
for (int b = 0; b < 22; b++) {
cout << " ";
if (b == 21) {
cout << "MENU \n";
}
}
for (int c = 0; c < 47; c++) {
ocut << "=";
}
cout << " "
"\n1. Add record\n"
"2. View players records\n"
"3. Compute for the average\n"
"4. Show the player(s) who gets the max average.\n"
"5. Show the player(s) who gets the min average.\n"
"6. Exit\n"
"Enter your choice:";
cin >> choice;
if (choice == 1) {
cout << "Add player data" << endl;
do {
cout << "Enter player " << i << " nickname:";
cin >> p1[i].name;
cout << "Enter player " << i << " age:";
cin >> p1[i].age;
cout << "Enter player " << i << " score 1:";
cin >> p1[i].score1;
cout << "Enter player " << i << " score 2:";
cin >> p1[i].score2;
cout << "Enter again? (Y/N)";
cin >> backtomain;
i++;
}
while (backtomain != 'N' && backtomain != 'n' && i < 7);
if (choice == 2) {
cout << "Player records" << endl;
cout << "Player nickname "
<< "Player age "
<< " player score 1"
<< "
player score 2\n ";
for (z = 1; z <= i - 1; z++) {
cout << p1[z].name << " " << p1[z].age << "" << p1[z].score1 << ""
<< p1[z].score2 << "\n";
}
cout << "Press 1 to go back to main menu\n";
cin >> backtomain;
}
if (choice == 3) {
cout << "Computing for average...\n";
for (int d = 1; d <= i - 1; d++) {
p1[d].average = (p1[d].score1 + p1[d].score2) / 2.0;
cout << "\n" << p1[d].average << "\n";
}
cout << "Press 1 to go back to main menu\n";
cin >> backtomain;
}
if (choice == 4) {
cout << "Player(s) who got the max average:\n";
cout << "\nPress 1 to go back to main menu";
cin >> backtomain;
}
if (choice == 5) {
cout << "player(s) who got the min average: \n";
cout << "Press 1 to go back to main menu";
cin >> backtomain;
}
}
while (choice != 6);
}
You can simply sort the array of players for that
int n = sizeof(p1)/ sizeof(p1[0]);
sort(p1, p1+n, compPlayer);
//larget at pl[0]
//smallest at pl[9]
where
bool compPlayer(playerdata p1, playerdata p2) {
return (p1.score1+p1.score2) > (p2.score1+p2.score2);
//use score incase average has not been calculated for all players yet
}
So I am trying to write a program that takes the total cost of a set of books given a certain value and then loop back to the beginning in order to get the total of a new set of books until the user tells it to stop. At the end of the program a total of the total cost of all books will be displayed. I am stuck trying to write this, thanks for any help with this, and sorry for my messy code as I am still learning.
Code Listed Here:
#include <iostream>
using namespace std;
const float kReqNew = 0.90;
const float kReqOld = 0.65;
const float kOptNew = 0.40;
const float kOptOld = 0.20;
class Book {
private:
float singleCost_;
int NeworOld_;
int ReqorOpt_;
int TotalBooksNeeded_;
int BookNumber_;
int NumOnHand_;
int prospectiveEnrole;
double totalCost_;
public:
void buildData();
void determineBooks();
void totalCosts();
void layOut();
void totalCostAdd();
};
void Book::buildData() {
cout << "Please enter: " << endl;
cout << "Book Number:";
cin >> BookNumber_;
cout << "Book Price: ";
cin >> singleCost_;
cout << "How many are in stock: ";
cin >> NumOnHand_;
cout << "Prospective Enrolment: ";
cin >> prospectiveEnrole;
cout << "Required(1), Optional(0)";
cin >> ReqorOpt_;
cout << "New(1), Old(0): ";
cin >> NeworOld_;
}
void Book::determineBooks(){
if(NeworOld_ == 1 && ReqorOpt_ == 1){
TotalBooksNeeded_ = NumOnHand_ * kReqNew;
}
else if(NeworOld_ == 1 && ReqorOpt_ == 0){
TotalBooksNeeded_ = NumOnHand_ * kOptNew;
}
else if(NeworOld_ == 0 && ReqorOpt_ == 1){
TotalBooksNeeded_ = NumOnHand_ * kReqOld;
}
else if(NeworOld_ == 0 && ReqorOpt_ == 0){
TotalBooksNeeded_ = NumOnHand_ * kOptOld;
}
cout << "Total Number of Books Needed: " << "$" << TotalBooksNeeded_ << endl;
}
void Book::totalCosts(){
totalCost_ = TotalBooksNeeded_ * singleCost_;
cout << "The total cost of the books is: " << totalCost_ << endl;
}
void Book::layOut(){
cout << BookNumber_<<endl;
cout << "$" << singleCost_ << endl;
cout << NumOnHand_ << endl;
}
void Book::totalCostAdd(){
double* newTotalCost;
*newTotalCost += totalCost_;
cout << newTotalCost << endl;
}
int main() {
Book books;
int exitNum;
cout << "\t\t\t ****************\n" << "\t\t\t Book Calculator\n" << "\t\t\t ****************\n";
do{
cout << "*******************************************\n";
books.buildData();
cout << "*******************************************\n";
books.layOut();
cout << "*******************************************\n";
books.determineBooks();
books.totalCosts();
cout << "*******************************************\n";
cout << "Would you like to continue: Yes(1), No(0)" << endl;
cin >> exitNum;
cout << "*******************************************\n";
books.totalCostAdd();
} while(exitNum != 0);
}
Thanks again for any help.
I have a Problem. I'm Trying to make a regular Calculator and Shape's Area and Perimeter finder.It's a combination. I didn't start on my Shape's Area and Perimeter Finder. This is My main.cpp.
#include <iostream>
#include <string>
#include "AAPO.h" // Its a Header File.
using namespace std;
void Calculators_Operation();
int main()
{
string opera;
cout << "Do you want Arithmetic Calculator or Area and Perimeter Calculator"
<< endl;
cin >> opera;
if (opera == "Arithmetic Calculator" or "arithmetic calculator" or "AC")
{
Calculators_Operation();
}
return 0;
}
This is my Operation Chooser.
#include <iostream>
#include <string>
#include "Arithmetic Chooser.h"
using namespace std;
void Calculators_Addition();
void Calculators_Subtraction();
void Calculators_Multiplication();
void Calculators_Division();
void Calculators_Operation()
{
string answera;
cout << "What Operation do you Want?" << endl;
cin >> answera;
if (answera == "Addition" or "addition" or "+");
{
Calculators_Addition();
};
if (answera == "Subtraction" or "subtraction" or "-");
{
Calculators_Subtraction();
};
if (answera == "Multiplication" or "multiplication" or "*" or "x" or "X")
{
Calculators_Multiplication();
};
if (answera == "Division" or "division" or "/")
{
Calculators_Division();
};
return;
}
This is my AAPO.h.
#ifndef AAPO_H_INCLUDED
#define AAPO_H_INCLUDED
void Calculators_Operation();
#endif // AAPO_H_INCLUDED
My Addition.
#include <iostream>
#include <string>
using namespace std;
void calculators_Addition_2();
void calculators_Addition_3();
void calculators_Addition_4();
void calculators_Addition_5();
void Calculators_Addition()
{
//ADDITION COMPLETE
string numberadd;
cout << "How much numbers do you want?" << endl;
cin >> numberadd;
if (numberadd == "2")
{
calculators_Addition_2();
return;
};
if (numberadd == "3")
{
calculators_Addition_3();
return;
};
if (numberadd == "4")
{
calculators_Addition_4();
return;
};
if (numberadd == "5")
{
calculators_Addition_5();
return;
}
}
void calculators_Addition_2()
{
int add11;
int add12;
int sum;
cout << "Enter the first number" << endl;
cin >> add11;
cout << "Enter the second number" << endl;
cin >> add12;
sum = add11 + add12;
cout << "The sum of the numbers are " << sum << endl;
return;
}
void calculators_Addition_3()
{
int add13;
int add23;
int add33;
int sum2;
cout << "Enter the First Number" << endl;
cin >> add13;
cout << "Enter the Second Number" << endl;
cin >> add23;
cout << "Enter the Third Number" << endl;
cin >> add33;
sum2 = add13 + add23 + add33;
cout << "The Sum of the Numbers are " << sum2 << endl;
return;
}
void calculators_Addition_4()
{
int add14;
int add24;
int add34;
int add44;
int sum3;
cout << "Enter the First Number" << endl;
cin >> add14;
cout << "Enter the Second Number" << endl;
cin >> add24;
cout << "Enter the Third Number" << endl;
cin >> add34;
cout << "Enter the Fourth Number" << endl;
cin >> add44;
sum3 = add14 + add24 + add34 + add44;
cout << "The Sum of the Numbers are " << sum3 << endl;
return;
}
void calculators_Addition_5()
{
int a15;
int a25;
int a35;
int a45;
int a55;
int sum4;
cout << "Enter the First Number" << endl;
cin >> a15;
cout << "Enter the Second Number" << endl;
cin >> a25;
cout << "Enter the Third Number" << endl;
cin >> a35;
cout << "Enter the Fourth Number" << endl;
cin >> a45;
cout << "Enter the Fifth Number" << endl;
cin >> a55;
sum4 = a15 + a25 + a35 + a45 + a55;
cout << "The Sum of the Numbers are " << sum4 << endl;
return;
}
My Subtraction.
#include <iostream>
using namespace std;
void Calculators_Subtraction()
{
int subractify;
int subracta;
int differencea;
cout << "Type in the First Number!" << endl;
cin >> subractify;
cout << "Type in the Second Number!" << endl;
cin >> subracta;
differencea = subractify - subracta;
cout << "The Difference is " << differencea << endl;
return;
}
My Multiplication.
#include <iostream>
#include <string>
using namespace std;
void Calculators_Multiplication_2();
void Calculators_Multiplication_3();
void Calculators_Multiplication_4();
void Calculators_Multiplication_5();
void Calculators_Multiplication()
{
string multicipia;
cout << "How much numbers do you want?" << endl;
cin >> multicipia;
if (multicipia == "2" or "Two" or "two")
{
Calculators_Multiplication_2();
};
if (multicipia == "3" or "Three" or "three")
{
Calculators_Multiplication_3();
};
if (multicipia == "4" or "Four" or "four")
{
Calculators_Multiplication_4();
};
if (multicipia == "5" or "Five" or "five")
{
Calculators_Multiplication_5();
};
return;
}
void Calculators_Multiplication_2()
{
int multi2a;
int multi2b;
int product2;
cout << "Type in the First Number." << endl;
cin >> multi2a;
cout << "Type in the Second Number." << endl;
cin >> multi2b;
product2 = multi2a * multi2b;
cout << "The Product is " << product2 << "." << endl;
return;
}
void Calculators_Multiplication_3()
{
int multi3a;
int multi3b;
int multi3c;
int product3;
cout << "Enter the First Number!" << endl;
cin >> multi3a;
cout << "Enter the Second Number!" << endl;
cin >> multi3b;
cout << "Enter the Third Number!" << endl;
cin >> multi3c;
product3 = multi3a * multi3b * multi3c;
cout << "The Product is" << product3 << "." << endl;
return;
}
void Calculators_Multiplication_4()
{
int multi4a;
int multi4b;
int multi4c;
int multi4d;
int product4;
cout << "Enter the First Number!" << endl;
cin >> multi4a;
cout << "Enter the Second Number!" << endl;
cin >> multi4b;
cout << "Enter the Third Number!" << endl;
cin >> multi4c;
cout << "Enter the Fourth Number!" << endl;
cin >> multi4b;
product4 = multi4a * multi4b * multi4c * multi4d;
cout << "The Product of the Numbers are " << product4 << "!" << endl;
return;
}
void Calculators_Multiplication_5()
{
int multi5a;
int multi5b;
int multi5c;
int multi5d;
int multi5e;
int product5;
cout << "Enter the First Number!" << endl;
cin >> multi5a;
cout << "Enter the Second Number!" << endl;
cin >> multi5b;
cout << "Enter the Third Number!" << endl;
cin >> multi5c;
cout << "Enter the Fourth Number!" << endl;
cin >> multi5d;
cout << "Enter the Fifth Number!" << endl;
cin >> multi5e;
product5 = multi5a * multi5b * multi5c * multi5d * multi5e;
cout << "The Product of the Numbers are" << product5 << "!" << endl;
return;
}
My Division.
#include <iostream>
using namespace std;
void Calculators_Division()
{
float divisia;
float divisiab;
float quotient;
cout << "Enter the Divisor" << endl;
cin >> divisia;
cout << "Enter the Dividend" << endl;
cin >> divisiab;
quotient = divisia / divisiab;
cout << "The Quotient of the Numbers are " << quotient << endl;
return;
}
The Problem right now is that when addition finishes , subtraction starts. After Subtraction, Multiplication. After Multiplication, Division. After That the program ends. I'm sorry. Its just that I'm new to programming (Like one month).
There are many things to improve (some of them already mentioned in comments), but exact answer to your question is:
you have semicolon after your if-statement lines in Calculators_Operation() function body, which makes if-statements useless and your "operation" functions are being called every time (what you already noticed).
you use "or" operator in incorrect way, if you use (answera == "addition" or "+"), you basically say: ((answera == "addition) or "+") and that evaluates to true (see here)
EDIT:
Your function Calculators_Operation may be changed to:
void Calculators_Operation()
{
string answera;
cout << "What Operation do you Want?" << endl;
cin >> answera;
if (answera == "Addition" || answera == "addition" || answera == "+")
{
Calculators_Addition();
}
else if (answera == "Subtraction" || answera == "subtraction" || answera == "-")
{
Calculators_Subtraction();
}
else if (answera == "Multiplication" || answera == "multiplication" || answera == "*" || answera == "x" || answera == "X")
{
Calculators_Multiplication();
}
else if (answera == "Division" || answera == "division" || answera == "/")
{
Calculators_Division();
} else {
cout << "Unknown operation entered!" << endl;
}
}
NOTE: all your if statements need to be changed according to this function.
If anything does not work, please either put in comments what exactly does not work or edit your question with new code posting and question.
I am new to c++ programming, I would like to create a program to find area, volume of shapes I used switch case to select shapes and used else if inside switch for selecting area or volume but it executes only one switch statement.
#include <iostream>
#include<cmath>
#define _USE_MATH_DEFINES
#include<math.h>
#define PI 3.14159
using namespace std;
int main() {
double radius;
double square;
double qube;
double quboid;
double cylinder;
double sphere;
double a;
double length;
double breadth;
double height;
double width;
char userchoice1;
char userchoice;
cout << "Select any shape by typing the no correctly\n";
cout << "1-square\n";
cout << "2-Cube\n";
cout << "3-Cuboid\n";
cout << "4-Circle\n";
cout << "5-Sphere\n";
cout << "6-rectangle\n";
cout << "Select any shape by typing the no correctly\n";
cin >> userchoice;
switch (userchoice) {
case '1':
cout << "3-area\n";
cout << "4-perimeter\n";
if (userchoice = 3) {
cout << "Enter side a\n";
cin >> a;
cout << "Area of square is " << a * a << "sq.units" << endl;
} else if (userchoice = 4) {
cout << "Enter side\n";
cin >> a;
cout << "Perimeter of square is " << 4 * a << "sq.units" << endl;
}
break;
case '2':
cout << "1-area\n";
cout << "2-volume\n";
if (userchoice = 1) {
cout << "Enter side a\n";
cin >> a;
cout << "Area of cube is " << 6 * a * a << "sq.units" << endl;
} else if (userchoice = 2) {
cout << "Enter side a\n";
cin >> a;
cout << "Volume of cube is " << a * a * a << "cu.units" << endl;
}
break;
case '3':
cout << "Enter length, breadth, height\n";
cin >> breadth;
cin >> length;
cin >> height;
cout << "Area of cuboid is " << (length * breadth * height) << "sq.units" << endl;
break;
case '4':
cout << "1-circumference\n";
cout << "2-Area\n";
if (userchoice = 1) {
cout << "Enter the radius of circle\n";
cin >> radius;
cout << "Circumference of circle is " << 2 * PI * radius << endl;
} else if (userchoice = 2) {
cout << "Enter the radius of circle\n";
cin >> radius;
cout << "Area of circle is " << PI * radius * radius << "sq.units" << endl;
}
break;
case '5':
cout << "1-Area\n";
cout << "2-volume\n";
if (userchoice = 1) {
cout << "Enter the radius of Sphere\n";
cin >> radius;
cout << "Area of Sphere is " << 4 * PI * radius * radius << "sq.units" << endl;
} else if (userchoice = 2) {
cout << "Enter the radius of Sphere\n";
cin >> radius;
cout << "Volume of Sphere is " << (4 / 3 * PI * radius * radius) << "cu.units" << endl;
}
break;
case '6':
cout << "1-Area\n";
cout << "2-perimeter\n";
if (userchoice = 1) {
cout << "Enter length width\n";
cin >> length;
cin >> width;
cout << "Area of rectangle: " << length * width << "sq.units" << endl;
} else if (userchoice = 2) {
cout << "Enter side\n";
cin >> a;
cout << "Perimeter of rectangle is " << 4 * a << "sq.units" << endl;
}
break;
}
}
switch statements can only execute through one case. It can "fall through" if you don't use breaks though. If you mean that it's not executing any of the if statements in the switch statement, it's because you're using the "assignment" operator (=) in the condition. You should be using the "is equal to" operator (==) instead.
So your if statements should be something like this:
if (userchoice == 1) {
// ... do something ...
}
else if (userchoice == 2) {
// ... do something else ...
}
Even if you do that, you have another problem that I notice. You're using the same variable in the condition of the if and switch statements. Doing it that way will make it impossible to execute some routes. You should use separate variables for both and prompt the user for both. Either that, or reassign "userchoice" to be used in the if statements.
Also, you're declaring "userchoice" as a char. If you declare it as a char, you should test for a char in the conditions. If not, declare it as an int.
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.