would you kindly help me with the following program:-
The program must input an amount in pounds and shillings, and convert it to rands and cents and display the result. I must use a While loop that repeats until an amount of 0 pounds and 0 shillings is entered.
I have tried to come up with the following program, but it display an incorrect result.
That's because of the way you've implemented this loop. You want this program to display the converted result each time values are entered for Pounds and Shillings, right? (Of course, otherwise there's no point in using a loop at all!) Alright, here's your code posted back with comments which show the problems.
#include <iostream>
usingnamespace std;
int main()
{
float rand, cent, pounds, shilling;
pounds = 0; //pounds and shillings should be initialized to something other than 0 so that the body of the loop is entered.
shilling = 0;
cout << "Enter Pounds " << endl; //There's no point in prompting the user for values when they'll just be prompted again
cin >> pounds; //as soon as the loop is entered.
cout << "Enter Shilling " << endl;
cin >> shilling;
while (pounds != 0 && shilling != 0)
{
rand = (pounds * 2 ); //The calculation of rand and cent should come AFTER the user has entered a value for conversion.
cent = (shilling * 10);
cout << "Enter Pounds " << endl;
cin >> pounds;
cout << "Enter Shilling " << endl;
cin >> shilling;
pounds++ && shilling++; //What is this for? This increment operation is the reason you're getting incorrect values.
//Get rid of it.
}
cout << "The pound is equell to R "<<rand << endl; //The statements diplaying the output should be contained within the loop
cout <<"Shilling equells to "<< cent<< " C "<< endl; //or the results will never be displayed until the loop is exited.
return 0;
}
The best thing to do when you end up with a problem like this is to think through your code step by step and see what the result of various entries would be. I think if you do that, you'll understand exactly why you get wrong values from your code. (I'm assuming your conversions are right.)