Issue
I don't know how to make my program compile, I'm new at programming.
I would like to know if I can compile just by adapting printf
or if I need some other function.
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int V = 0;
scanf("%d", &V);
printf("NOTAS: \n");
printf("%d Nota(s) de R$ 100.00\n", V / 100);
printf("%d Nota(s) de R$ 50.00\n", V % 100 / 50);
printf("%d Nota(s) de R$ 20.00\n", V % 100 % 50 / 20);
printf("%d Nota(s) de R$ 10.00\n", V % 100 % 50 % 20 / 10);
printf("%d Nota(s) de R$ 5.00\n", V % 100 % 50 % 20 % 10 / 5);
printf("%d Nota(s) de R$ 2.00\n", V % 100 % 50 % 20 % 10 % 5 / 2);
printf("MOEDAS: \n");
printf("%d Moeda(s) de R$ 1.00\n", V % 100 % 50 % 20 % 10 % 2 / 1);
printf("%.2lf Moeda(s) de R$ 0.50\n", V % 100 % 50 % 20 % 10 % 2 % 1 / 0.50);
printf("%.2lf Moeda(s) de R$ 0.25\n", V % 100 % 50 % 20 % 10 % 2 % 1 % 0.50 / 0.25);
return 0;
}
Solution
In the line
printf("%.2lf Moeda(s) de R$ 0.25\n", V % 100 % 50 % 20 % 10 % 2 % 1 % 0.50 / 0.25);
^^^^^^
You cannot use modulus with decimal value, it must be an integer.
Note that the .50
and .25
cannot work as intended because if you input a decimal value it'll be truncated and stored as an integer since V
is an int
.
One thing you can do is to parse values separately, for integer values and for decimal values and take it from there.
Something like:
#include <iostream>
#include <sstream>
#include <string>
int main()
{
int value[2], i = 0;
std::string V, temp;
getline(std::cin, V);
std::stringstream ss(V);
while (getline(ss, temp, '.') && i < 2) //tokenize stream by '.'
{
value[i++] = std::stoi(temp); //convert to integer
}
printf("NOTAS: \n"); //you can replace all these with std::cout
printf("%d Nota(s) de R$ 100.00\n", value[0] / 100);
printf("%d Nota(s) de R$ 50.00\n", value[0] % 100 / 50);
printf("%d Nota(s) de R$ 20.00\n", value[0] % 100 % 50 / 20);
printf("%d Nota(s) de R$ 10.00\n", value[0] % 100 % 50 % 20 / 10);
printf("%d Nota(s) de R$ 5.00\n", value[0] % 100 % 50 % 20 % 10 / 5);
printf("%d Nota(s) de R$ 2.00\n", value[0] % 100 % 50 % 20 % 10 % 5 / 2);
printf("MOEDAS: \n");
printf("%d Moeda(s) de R$ 1.00\n", value[0] % 100 % 50 % 20 % 10 % 5 % 2);
printf("%d Moeda(s) de R$ 0.50\n", value[1] % 100 / 50);
printf("%d Moeda(s) de R$ 0.25\n", value[1] % 100 % 50 / 25);
return 0;
}
Answered By - anastaciu Answer Checked By - Katrina (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.