+ 5

Explain please how to output double numbers in 'cent' ?

I need to set outputing value in cent in double zeros.. i got only one zero in outout. When input is 300 it may be outputing '$3 00 cent'. But how to do that? #include <iostream> #include <locale.h> #include <stdlib.h> using namespace std; int main() { setlocale(0,""); int k; // user's cent value int s; // value in $ int d; // value in cent cout << "Input value in cent" << endl; cin >> k; s = (k / 100); d = k % 100; cout << endl << "

quot;<< s << " "<< d << " cent" << endl; return 0; }

21st Sep 2017, 1:11 PM
Oleksandr Rubtsov
Oleksandr Rubtsov - avatar
2 Answers
+ 4
I GOT IT!! #include <iostream> #include <locale.h> #include <stdlib.h> using namespace std; int main() { setlocale(0,""); int k; // user's cent value int s; // value in $ int d; // value in cent1 int c; // value in cent2 cout << "Input value in cent" << endl; cin >> k; s = (k / 100); d = (k / 10) % 10; c = k % 10; cout << endl << "
quot;<< s << " "<< d << c << " cent" << endl; return 0; }
21st Sep 2017, 1:21 PM
Oleksandr Rubtsov
Oleksandr Rubtsov - avatar
+ 2
Glad you figured it out! I would have done it a little differently. It can't hurt to have a new perspective on things, right? #include <iostream> #include <iomanip> int main() { int k;//user's cent value float a;//variable to hold resulting dollars and cents cout << "Enter value, in cents: "; cin >> k; a = (float)k / 100.0f; cout << fixed; cout << "\n\n
quot; << setprecision(2) << a << endl; return 0; } Happy Coding! :)
21st Sep 2017, 10:45 PM
Zeke Williams
Zeke Williams - avatar