I want to code currency converter and I do it,
but, When I tried it, result was wrong than I expected,
Knowing that the code works, but the result is not correct and I tried all the methods I know
, I am a beginner, please a answer my question, and Thanks
#include <iostream>
using namespace std;
int main()
{
cout << "1- EGP TO Usd" << endl;
cout << "2- USD TO EGP" << endl;
cout << "3- EGP TO SAR" << endl;
cout << "4- USD TO SAR" << endl;
cout << "5- SAR TO EGP" << endl;
cout << "6- SAR TO USD" << endl;
int num;
double balance;
double egpToUsd = .064;
double usdToEgp = 15.70;
double egpToSar = .24;
double usdToSar = 3.75;
double sarToEgp = 4.19;
double sarToUsd = .27;
cout << "Enter a Num: ";
cin >> num;
cout << "Enter your balance: ";
cin >> balance;
if (num >= 1 && num <= 6)
{
switch (num)
{
case 1:
cout << (balance / egpToUsd);
break;
case 2:
cout << (balance * usdToEgp);
break;
case 3:
cout << (balance / egpToSar);
break;
case 4:
cout << (balance * usdToSar);
break;
case 5:
cout << (balance * sarToEgp);
break;
case 6:
cout << (balance / sarToUsd);
break;
}
}
else {
cout << "Error please select from list" << endl;
}
return 0;
}
#include <iostream>
int main()
{
std::cout << "1- EGP TO Usd\n";
std::cout << "2- USD TO EGP\n";
std::cout << "3- EGP TO SAR\n";
std::cout << "4- USD TO SAR\n";
std::cout << "5- SAR TO EGP\n";
std::cout << "6- SAR TO USD\n";
int num;
double balance;
double egpToUsd = .064;
double usdToEgp = 15.70;
double egpToSar = .24;
double usdToSar = 3.75;
double sarToEgp = 4.19;
double sarToUsd = .27;
std::cout << "Enter a Num: ";
std::cin >> num;
std::cout << "Enter your balance: ";
std::cin >> balance;
switch (num)
{
case 1:
std::cout << (balance * egpToUsd);
break;
case 2:
std::cout << (balance * usdToEgp);
break;
case 3:
std::cout << (balance * egpToSar);
break;
case 4:
std::cout << (balance * usdToSar);
break;
case 5:
std::cout << (balance * sarToEgp);
break;
case 6:
std::cout << (balance * sarToUsd);
break;
default:
std::cout << "Error please select from list\n";
break;
}
return 0;
}
Dont Use "using namespace std;" thats bad practice and why were you dividing in some cases, its a simple math. I hope you got your solution
Related
I am new to the coding community and I am here to ask on how can I store the calculations that have been done in my calculator. I am currently new in this space and I am currently looking for answers T-T.
my code goes like this thank you everyone! T-T pls be gentle on me.
#include<iostream>
#include<stdlib.h>
using namespace std;
int calculator();
void history();
void choice();
int main()
{
int x;
cout << "\t\t\t\tWhat do you want to do?\n\n";
cout << "\t\t\t\t[1] Standard calculator\n";
cout << "\t\t\t\t[2] History\n";
cout << "\t\t\t\t[3] Exit\n\n\n\n";
cout << "\t\t\t\tChoice: ";
cin >> x;
switch (x)
{
case 1:
calculator();
break;
case 2:
history();
break;
case 3:
cout << "\n\n\nThank you for using my calculator!";
exit(4);
break;
default:
cout << "Enter a correct choice";
main();
}
}
int calculator()
{
double x, y;
float sum = 0.0, dif = 0.0, prod = 1.0, quo = 1.0;
int i;
char op, back;
do {
system("CLS");
cout << "\t\t#CALCULATOR#" << endl;
cout << endl << endl;
cout << "Calculate 2 numbers: (example 1 + 1 or 2 * 2)\n";
cin >> x >> op >> y;
switch (op) {
case '+':
sum = x + y;
cout << "The sum of " << x << " and " << y << " is " << sum;
break;
case '-':
dif = x - y;
cout << "The difference of " << x << " and " << y << " is " << dif;
break;
case '*':
prod = x * y;
cout << "The product of " << x << " and " << y << " is " << prod;
break;
case '/':
if (y == 0)
cout << "Undefined";
else
quo = x / y;
cout << "The quotient of " << x << " and " << y << " is " << quo;
break;
default:
cout << "Invalid operator";
break;
}
cout << "\nContinue[Y/N]: ";
cin >> back;
cout << endl << endl;
if (back != 'Y' && back != 'y' && back != 'N' && back != 'n')
{
cout << "Please enter a correct choice" << endl;
choice();
}
else if (back == 'N' || back == 'n')
{
system("pause");
system("CLS");
main();
}
} while (back == 'y' || back == 'Y');
cout << "Thank you";
system("pause");
system("CLS");
}
void choice()
{
char c;
do
{
cout << "Do you want to continue? [Y/y or N/n]" << endl;
cin >> c;
if (c == 'Y' || c == 'y')
calculator();
else if (c == 'N' || c == 'n')
{
system("pause");
system("cls");
main();
}
else
cout << "Please enter a correct choice\n";
choice();
} while (c != 'y' || c != 'Y' || c != 'N' || c != 'n');
cout << "Enter a correct choice";
}
void history()
{
cout << "I still dont know how T - T";
}
I wanted to store the past calculations using arrays but i dont know how to actually put it in my code T-T pls help
You want to #include <vector> and make a history std::vector, that holds strings:
std::vector<std::string> history;
When you do the calculcation, don't output to cout, but to a std::ostringstream first (located in <sstream>):
std::ostringstream caclulation_stream;
switch (op) {
case '+':
sum = x + y;
caclulation_stream << "The sum of " << x << " and " << y << " is " << sum;
break;
case '-':
dif = x - y;
caclulation_stream << "The difference of " << x << " and " << y << " is " << dif;
break;
case '*':
prod = x * y;
caclulation_stream << "The product of " << x << " and " << y << " is " << prod;
break;
case '/':
if (y == 0)
caclulation_stream << "Undefined";
else
quo = x / y;
caclulation_stream << "The quotient of " << x << " and " << y << " is " << quo;
break;
default:
caclulation_stream << "Invalid operator";
break;
}
this way, you can save the string to history. push_back() adds the string to the vector. Don't forget to also print out the string, so it's still displayed in the console.
auto calculation_string = caclulation_stream.str();
history.push_back(calculation_string);
std::cout << calculation_string;
to display the history, you can loop over the vector and print out the elements:
void show_history()
{
for (const auto& entry : history) {
std::cout << entry << '\n';
}
}
this should give you the basic ideas on how to implement this.
Read here why you shouldn't be using namespace std;.
I need to ask the user in this way "Do you want to do it again?" and the user will choose Y or N whether they want to compute another set of numbers and go through the program all over again.
#include <iostream>
using namespace std;
int main()
{
float x, y;
float Sum, Difference, Product, Quotient;
char choice;
cout << "Enter x and y: ";
cin >> x >> y;
cout << "MENU" << endl;
cout << "A: Addition " << endl;
cout << "B: Subtraction" << endl;
cout << "C: Multiplication " << endl;
cout << "D: Division " << endl;
cout << "E: Exit " << endl;
cout << "Enter your choice :";
cin >> choice;
switch (choice)
{
case 'A':
Sum = x+y;
cout << x << "+" << y << "=" << Sum <<endl;
break;
case 'B':
Difference = x-y;
cout << x << "-" << y << "=" << Difference <<endl;
break;
case 'C':
Product = x*y;
cout << x << "*" << y << "=" << Product <<endl;
break;
case 'D':
Quotient = x/y;
cout << x << "/" << y << "=" << Quotient <<endl;
break;
case 'E':
break;
default:
cout << "Invalid input" << endl;
}
return 0;
}
It's actually pretty simple. You can use loops as such:
#include <iostream>
using namespace std;
void showChoices();
int main()
{
while (true) // This is a while loop
{
system("cls"); // Clear the screen. If you don't want to clear the screen you can remove this line
float x, y;
float Sum, Difference, Product, Quotient;
char choice;
cout << "Enter x and y: ";
cin >> x >> y;
showChoices();
cin >> choice;
switch (choice)
{
case 'A':
Sum = x + y;
cout << x << "+" << y << "=" << Sum << endl;
break;
case 'B':
Difference = x - y;
cout << x << "-" << y << "=" << Difference << endl;
break;
case 'C':
Product = x - y;
cout << x << "*" << y << "=" << Product << endl;
break;
case 'D':
Quotient = x - y;
cout << x << "/" << y << "=" << Quotient << endl;
break;
case 'E':
return 0; // Instead of break, use return here
default:
cout << "Invalid input" << endl;
}
}
}
void showChoices()
{
cout << "MENU" << endl;
cout << "A: Addition " << endl;
cout << "B: Subtraction" << endl;
cout << "C: Multiplication " << endl;
cout << "D: Division " << endl;
cout << "E: Exit " << endl;
cout << "Enter your choice :";
}
Also consider removing the line:
using namespace std
...because it's not considered as a good practice.
Also consider refactoring your code by moving the code inside the loop to a separate function, which returns a boolean something like this:
#include <iostream>
using namespace std;
void showChoices();
bool askUser();
int main()
{
while (askUser()) { /* Loop */ }
}
void showChoices()
{
cout << "MENU" << endl;
cout << "A: Addition " << endl;
cout << "B: Subtraction" << endl;
cout << "C: Multiplication " << endl;
cout << "D: Division " << endl;
cout << "E: Exit " << endl;
cout << "Enter your choice :";
}
bool askUser()
{
system("cls"); // Clear the screen. Can be removed if you don't want to do so.
float x, y;
float Sum, Difference, Product, Quotient;
char choice;
cout << "Enter x and y: ";
cin >> x >> y;
showChoices();
cin >> choice;
switch (choice)
{
case 'A':
Sum = x + y;
cout << x << "+" << y << "=" << Sum << endl;
break;
case 'B':
Difference = x - y;
cout << x << "-" << y << "=" << Difference << endl;
break;
case 'C':
Product = x - y;
cout << x << "*" << y << "=" << Product << endl;
break;
case 'D':
Quotient = x - y;
cout << x << "/" << y << "=" << Quotient << endl;
break;
case 'E':
return false;
default:
cout << "Invalid input" << endl;
}
return true;
}
I hope it was useful :).
I am working on the code below and trying to use switch statement instead of if/else.
The problem is that I cannot figure out how to get the switch to work.
A few things a tried:
I know that every time an expression is equal to the case constant, the code then will be executed. Example:
switch (expression)
{
case 1:
// code to be executed if
// expression is equal to 1;
break;
}
My code below has a similar concept, but I cannot get it display the calculation. There are no errors, but it does not display the total price.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
const int CHEESE_PIZZA = 11;
const int SPINACH_PIZZA = 13;
const int CHICKEN_PIZZA = 14;
cout << " *********** MENU ***********" << endl;
cout << setw (9) << "ITEM" << setw (20) << "PRICE" << endl;
cout << " (1) Cheese Pizza" << setw (8) << "$"
<< CHEESE_PIZZA << endl;
cout << " (2) Spinach Pizza" << setw (7) << "$"
<< SPINACH_PIZZA << endl;
cout << " (3) Chicken Pizza" << setw (7) << "$"
<< CHICKEN_PIZZA << endl;
cout << endl;
cout << "What would you like? ";
int option;
cin >> option;
cout << "You picked pizza option " << option << endl;
cout << "How many orders? ";
int quantity;
cin >> quantity;
cout << "You choose quantity " << quantity << endl;
int price;
switch (option)
{
case 1:
price = CHEESE_PIZZA;
break;
case 2:
price = SPINACH_PIZZA;
break;
case 3:
price = CHICKEN_PIZZA;
break;
default:
cout << "Please select valid item from menu. " << endl;
}
return 1;
int amount = price * quantity;
cout << "Your Bill: $ " << amount << endl;
cout << endl;
return 0;
}
I am confused about the output for any input other than 1, 2, and 3 in case 4.
The if/else statement works:
int price;
if (option == 1) price = CHEESE_PIZZA;
else if (option == 2) price = SPINACH_PIZZA;
else if (option == 3) price = CHICKEN_PIZZA;
else {
cout << "Please select valid item from menu. " << endl;
return 1;
}
The problem, from eyeballing, appears to be because you return 1 outside the switch statement, which is why the code after it never gets run.
You should replace your case 4 with a default: label and move the return 1; into that case as well as add a break statement under case 3.
Here I have made a program that asks how old the user is and how much money they have. Lets say for example the user puts that he/she has $500 and buys a bat for $50. How do I make the $500 go down to $450?
Here is the code:
#include <iostream>
using namespace std;
int main()
{
int age;
int money;
cout << "How old are you?" << endl;
cin >> age;
if (age <= 12) {
cout << "Not for kids. Sorry!" << endl;
}
else {
cout << "How much money do you have?" << endl;
}
cin >> money;
if (money <= 50) {
cout << "Sorry not enough" << endl;
}
else {
cout << "Here are the items you can buy" << endl;
cout << " a = $50 Bat\n b = $100 Beats\n c = $500 Xbox One\n d = $500 PS4\n";
}
int a;
int b;
int c;
int d;
if (cin >> a) {
money -= 50; //This is the part where I tried to make the $500 go down
}
cout << "You have " << money << "left" << endl;
return 0;
}
This is the part where I tried to make the $500 go down to $450:
if (cin >> a) {
money -= 50; //This is the part where I tried to make the $500 go down
}
cout << "You have " << money << "left" << endl;
return 0;
}
Also if there is a way to make my program have the same output as my code but shorter, that would also be great!
You must modify your code to make it logical and readable.
I write this for you.
#include <iostream>
using namespace std;
int main()
{
int age, money;
int priceA = 50, priceB = 100, priceC = 500, priceD = 500;
char choice;
cout << "How old are you?" << endl;
cin >> age;
if (age <= 12) {
cout << "Not for kids. Sorry!" << endl;
return 0;
}
cout << "How much money do you have?" << endl;
cin >> money;
if (money <= 50) {
cout << "Sorry not enough" << endl;
return 0;
}
cout << "Here are the items you can buy" << endl;
cout << " a = $ " << priceA << " Bat" << endl;
cout << " b = $ " << priceB << " Bat" << endl;
cout << " c = $ " << priceC << " Bat" << endl;
cout << " d = $ " << priceD << " Bat" << endl;
cin >> choice;
switch (choice) {
case 'a':
if(money >= priceA) {
money -= priceA;
break;
} else {
cout << "You have not enough money!" << endl;
return 0;
}
case 'b':
if(money >= priceB) {
money -= priceB;
break;
} else {
cout << "You have not enough money!" << endl;
return 0;
}
case 'c':
if(money >= priceC) {
money -= priceC;
break;
} else {
cout << "You have not enough money!" << endl;
return 0;
}
case 'd':
if(money >= priceD) {
money -= priceD;
break;
} else {
cout << "You have not enough money!" << endl;
return 0;
}
default:
cout << "Wrong input" << endl;
return 0;
}
cout << "You have " << money << " left" << endl;
return 0;
}
If you are representing your user input's choice with a character, read it into a char.
Dispatch based on their choice:
char choice;
if (cin >> choice) {
switch (choice) {
case 'a':
money -= 50;
break;
case 'b':
money -= 100;
break;
case 'c':
money -= 500;
break;
case 'd':
money -= 500;
break;
default:
std::cerr << "invalid choice";
}
} else {
std::cerr << "invalid choice";
}
Here's the proper way to solve this question.
#include <iostream>
using namespace std;
int main()
{
int age, money;
int priceA = 50, priceB = 100, priceC = 500, priceD = 500;
char choice;
cout << "How old are you?" << endl;
cin >> age;
if (age <= 12) {
cout << "Not for kids. Sorry!" << endl;
return 0;
}
cout << "How much money do you have?" << endl;
cin >> money;
if (money <= 50) {
cout << "Sorry not enough" << endl;
return 0;
}
cout << "Here are the items you can buy" << endl;
cout << " a = $ " << priceA << " Bat" << endl;
cout << " b = $ " << priceB << " Bat" << endl;
cout << " c = $ " << priceC << " Bat" << endl;
cout << " d = $ " << priceD << " Bat" << endl;
cin >> choice;
if(choice=='a' && money >=50)
{
money-=50;
}
if(choice=='b' && money >=100)
{
money-=100;
}
if(choice=='c' && money >=500)
{
money-=500;
}
if(choice=='d' && money>=500)
{
money-=500;
}
cout << "You have " << money << " left" << endl;
return 0;
}
I am working on an assignment. The problem I am having is every time I try to run my program to see what it displays, nothing shows up on the command prompt. However, if I press any key and then enter, the program starts looping uncontrollably. The program doesn't even display the initial cout message, just a blinking "_". Thanks
#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
void PizzaMenu();
void SizePrices();
int main()
{
double personal = 10.00;
double medium = 14.50;
double large = 19.00;
double xlarge = 23.50;
double FlavorChoice=0;
int SizeChoice;
int PizzaCountP=(cin >> PizzaCountP, PizzaCountP);
int PizzaCountM = (cin >> PizzaCountM, PizzaCountM);
int PizzaCountL = (cin >> PizzaCountL, PizzaCountL);
int PizzaCountXL = (cin >> PizzaCountXL, PizzaCountXL);
double orderTotal = (personal * PizzaCountP) + (medium * PizzaCountM) + (large * PizzaCountL) + (xlarge * PizzaCountXL);
cout << "Welcome to Joes pizza place!" << endl;
do{
PizzaMenu();
cout << "\nPlease chose a pizza from the menu(1-6): ";
cin >> FlavorChoice;
SizePrices();
cin >> SizeChoice;
if (SizeChoice > 0 && SizeChoice < 5)
{
switch (SizeChoice)
{
case 1:
cout << "How many personal pizzas? "; cin >> PizzaCountP;
break;
case 2:
cout << "How many medium pizzas?"; cin >> PizzaCountM;
break;
case 3:
cout << "How many large pizzas?"; cin >> PizzaCountL;
break;
case 4: cout << "How many extra large pizzas?"; cin >> PizzaCountXL;
break;
default: cout << "please enter a choice (1-4)"; cin >> SizeChoice;
break;
}
}
if (PizzaCountP > 0 || PizzaCountM > 0 || PizzaCountXL > 0 || PizzaCountL > 0)
{
printf("Your total is: %a", orderTotal);
}
} while (FlavorChoice != 6);
cout << "Thank you for visiting Joes place pizza! "<<endl;
}
void PizzaMenu()
{
cout << "\nSpecialty Pizza Menu" << endl;
cout << "\n1)Pizza 1" << endl << "\n2)Pizza 2" << endl << "\n3)Pizza 3" <<endl << "\n4)Pizza 4" << endl << "\n5)Pizza 5" << endl << "\n6)Pizza 6" << endl;
}
void SizePrices()
{
cout << "1) 10'' Personal" << "\t" << "- $10.00" << endl;
cout << "2) 14'' Medium" << "\t" << "- $14.50" << endl;
cout << "3) 16'' Large" << "\t" << "- $19.00" << endl;
cout << "4) 18'' Extra Large" << "\t" << "- $23.50" << endl;
cout << "Your choice (1-4)? ";
}
There were a few logical errors in the program. Right now, it should work fine...
#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
void PizzaMenu()
{
cout << "Specialty Pizza Menu:" << endl;
cout << "1) Pizza 1" << endl << "2) Pizza 2" << endl << "3) Pizza 3" << endl << "4) Pizza 4" << endl << "5) Pizza 5" << endl << "6) Exit" << endl;
}
void SizePrices()
{
cout << "Size Prices:" << endl;
cout << "1) 10'' Personal" << "\t" << "- $10.00" << endl;
cout << "2) 14'' Medium" << "\t" << "- $14.50" << endl;
cout << "3) 16'' Large" << "\t" << "- $19.00" << endl;
cout << "4) 18'' Extra Large" << "\t" << "- $23.50" << endl;
cout << "Your choice (1-4)? ";
}
int main()
{
double personal = 10.00;
double medium = 14.50;
double large = 19.00;
double xlarge = 23.50;
int FlavorChoice = 0;
int SizeChoice = 0;
int PizzaCountP = 0;
int PizzaCountM = 0;
int PizzaCountL = 0;
int PizzaCountXL = 0;
double orderTotal = 0.0;
cout << "Welcome to Joes pizza place!" << endl;
cout << "Please choose from the main menu(1-6): " << endl;
PizzaMenu();
cin >> FlavorChoice;
while(FlavorChoice != 6) {
SizePrices();
cin >> SizeChoice;
if (SizeChoice > 0 && SizeChoice < 5)
{
switch (SizeChoice)
{
case 1:
cout << "How many personal pizzas? ";
cin >> PizzaCountP;
orderTotal += personal * PizzaCountP;
break;
case 2:
cout << "How many medium pizzas?";
cin >> PizzaCountM;
orderTotal += medium * PizzaCountM;
break;
case 3:
cout << "How many large pizzas?";
cin >> PizzaCountL;
orderTotal += large * PizzaCountL;
break;
case 4: cout << "How many extra large pizzas?";
cin >> PizzaCountXL;
orderTotal += xlarge * PizzaCountXL;
break;
default: cout << "please enter a choice (1-4)";
cin >> SizeChoice;
break;
}
}
// orderTotal = (personal * PizzaCountP) + (medium * PizzaCountM) + (large * PizzaCountL) + (xlarge * PizzaCountXL);
if (PizzaCountP > 0 || PizzaCountM > 0 || PizzaCountXL > 0 || PizzaCountL > 0)
{
// printf("Your total is: %a", orderTotal);
cout << "Your total is: $" << orderTotal << endl;
}
cout << "Please choose from the main menu(1-6): " << endl;
PizzaMenu();
cin >> FlavorChoice;
}
cout << "Thank you for visiting Joes place pizza! " << endl;
// system("pause");
return 0;
}