How do you round up?

+1 Giorgio M · January 31, 2015
I've tried everything possible and the final answer won't round up. This is what I have so far and this is what it is telling me to do.

"Enter pounds: 7
Enter shillings: 17
Enter pence: 9
Result in Decimal pounds = £7.89

In ASCII the '\x9c' represents the pound sign. Also, display the result with two decimal places (display £7.89, not £7.88xxxx) by using the fixed, showpoint and setprecision() manipulators"

I got the code working perfectly, the one issue I am having is that I keep getting £7.88 not £7.89 

I got it rounded to 2 decimal places at least. If anyone can help that would be highly appericated. Thank You

-Giorgio




#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main(){

//Declare Variables
int Pounds;
int Shillings; 
int Pence;
string User_Answer;

//Initialize Variables
Pounds = 0;
Shillings = 0;
Pence = 0;
User_Answer = "C";

while ( User_Answer == "C" || User_Answer == "c" ){
cout << "Enter pounds     : ";
cin >> Pounds;
cout << "Enter shillings  : ";
cin >> Shillings;
cout << "Enter pence      : ";
cin >> Pence;
Pence = (((Shillings * 12) + Pence) * 100)/240;
//If statement (Pence will not exceed 100)
if (Pence >= 100){
//Calculating expressions
Shillings = Pence % 100;
Pounds = Pounds + ((Pence-Shillings) / 100);
Pence = Shillings;
}

cout << "Decimal pounds   = \x9c" << fixed << setprecision(2) << Pounds << "." << Pence << endl;
cout << "Press C to continue or E to exit.\t\t";
cin >> User_Answer;
}

system("pause");
return 0;

}

Post a Reply

Replies

Oldest  Newest  Rating
0 Jay Deshaun · February 1, 2015
I found a way to round for you.
Look at this: Here

Your code with the round:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main(){
//Declare Variables and Initialize Variables
int Pounds = 0;
int Shillings = 0; 
int Pence = 0;
string User_Answer = "C";

while ( User_Answer == "C" || User_Answer == "c" ){
cout << "Enter pounds     : ";
cin >> Pounds;
cout << "Enter shillings  : ";
cin >> Shillings;
cout << "Enter pence      : ";
cin >> Pence;
Pence = (((Shillings * 12) + Pence) * 100)/240;
//If statement (Pence will not exceed 100)
if (Pence >= 100){
//Calculating expressions
Shillings = Pence % 100;
Pounds = Pounds + ((Pence-Shillings) / 100);
Pence = Shillings;
}
cout << "Decimal pounds   = \x9c" << fixed << setprecision(2) << Pounds << "." << round(Pence) << endl;
cout << "Press C to continue or E to exit.\t\t";
cin >> User_Answer;
}
system("pause");
return 0;
}
0 Jay Deshaun · February 1, 2015
@Krootushas
Thanks for indicating that to me, I'm so use to Java.
+1 Mahmoud Abdel Ghani · February 1, 2015


This is what you want bro try it and enjoy!!!!

void main()
{
// Declare Variables
int Pounds;
int Shillings;
int Pence;
char User_Answer; //here you should declare a char because the user                      // will enter a character not string

//Initialize Variables
Pounds = 0;
Shillings = 0;
Pence = 0;
User_Answer = 'C';

while (User_Answer == 'C' || User_Answer == 'c')//here notice the ' 'from                       //" "because it makes difference with the loops
{
cout << "Enter pounds : ";
cin >> Pounds;
cout << "Enter shillings : ";
cin >> Shillings;
cout << "Enter pence : ";
cin >> Pence;
Pence = (((Shillings * 12) + Pence) * 100) / 240;
//If statement (Pence will not exceed 100)
if (Pence >= 100){
//Calculating expressions
Shillings = Pence % 100;
Pounds = Pounds + ((Pence - Shillings) / 100);
Pence = Shillings;
}

cout << "Decimal pounds = \x9c" << fixed<<setprecisions(2) << Pounds << "." << Pence << endl;
//i dont know what is this mean setprecisions because
//the compiler didnt run right until i removed it
cout << "Press C to continue or E to exit.\t\t";
cin >> User_Answer;
}
+1 Giorgio M · February 1, 2015
Thank You all for the help I actually spent so many hours and found a solution. Now I got it working fine. If anyone wants to see the code lmk and I'll happily put it on here just for your curiousity!
  • 1

C++

107,097 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator