Write a program in c++ that allows the user to enter amount in dollars and converts its value into PKR and vice versa.
Dani AI
Generated
Following 's reminder about homework, the following is a compact, learning-oriented explanation plus a minimal, practical C++ example that demonstrates a safe approach to a console USD <-> PKR converter: present a choice, validate numeric input, accept a current exchange rate (so the program stays useful as rates change), compute, round to two decimals, and format output.
Essential steps: show a menu for direction (USD->PKR or PKR->USD); read and validate the amount; read and validate the exchange rate (PKR per 1 USD); perform the multiplication or division; round the result to two decimal places; print with fixed two-decimal formatting.
#include <iostream>
#include <iomanip>
#include <limits>
#include <cmath>
int main() {
double amount = 0.0, rate = 0.0;
int choice = 0;
std::cout << "1) USD -> PKR\n2) PKR -> USD\nChoose (1 or 2): ";
while (!(std::cin >> choice) || (choice != 1 && choice != 2)) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Enter 1 or 2: ";
}
std::cout << "Enter amount: ";
while (!(std::cin >> amount) || amount < 0.0) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Enter a non-negative number: ";
}
std::cout << "Enter exchange rate (PKR per 1 USD): ";
while (!(std::cin >> rate) || rate <= 0.0) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Enter a positive rate: ";
}
double result = (choice == 1) ? amount * rate : amount / rate;
result = std::round(result * 100.0) / 100.0; // round to cents
std::cout << std::fixed << std::setprecision(2);
if (choice == 1) std::cout << amount << " USD = " << result << " PKR\n";
else std::cout << amount << " PKR = " << result << " USD\n";
return 0;
} Notes and cautions: for classroom practice double plus std::round and std::setprecision(2) is fine; for production finance use integer cents or a decimal library to avoid binary floating-point errors. Never hard-code a long-lived exchange rate — supply it at runtime or fetch from a reliable API when live accuracy is required. Per , learning comes from attempting the implementation first; the example above illustrates robust input handling, rounding, and formatting so the approach can be adapted or extended.
Schol-R-LEA 1,446 Commie Mutant Traitor Featured Poster
Once more with my standard reply for this sort of thing:
First off, we don't do other people's homework for them. Second, we don't do other people's homework for them. And third, we don't do other people's homework for them. Sensing a pattern here yet?
No one here will simply hand you a solution on a silver platter. If you show us what you've done, what you've tried to do, and what problems you've had with it, then we'll be happy to help. If you have specific questions, we can answer them, or at least point you in the right direction. If you have a program with a bug you can't swat on your own, we'll be glad to assist, so long as you pay attention to the forum rules and post sensible questions in an intelligent manner that we have some reasonable hope of answering.
But just cutting and pasting an assignment into a message, without even prefacing it with something like, "I have this homework problem that I can't solve...", is likely to get you booted from the message boards here and elsewhere - if you're lucky. What happens to you if you are unlucky is... well... let's just say that this guy probably won't be trying that again, on that forum or this one.
We take this issue seriously here. Very seriously. Asking us to do homework for you is a grave breach of academic ethics on your part, and actually doing so would be an even bigger breach on ours (not that this stops the many fine mercenaries at Freelancer.com, but still). Simply posting this here, in this way, could get you expelled from your school, if someone happens to notice it and blow the whistle on you. Furthermore, it does neither you nor us any good to help you cheat - especially since there's a good chance some day one of us will have to work with you, manage you, or, Eris forefend, fix code you've written. We have an obligation to our profession and our own future sanity to help you become a good programmer, and doing your coursework for you isn't going to do that.
And if you think you won't get caught by your professor... think again.
And please don't insult our intelligence by claiming that it isn't a class assignment. It's very easy to spot one, and we have a lot of practice at it. Trust me on this.
Now, if you actually don't know how to create a program that fits the requirements... hmmmn. Reading the book is definitely called for. As is speaking to the professor; while some can be a--holes about office hours, most are more than willing to give extra help, if only to keep their class grades from slipping to the point where they get re-assigned to teach remedial basketweaving.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.