Errors that prevent compilation are:
- The expression
for
is used in another format, e.g.. for(int i, i < 5, i++)
. In case you want to use a while
.
- In condition of
while
, FIM
must be between ""
because it’s a string.
- In
ToC = Toc + PrC
, the second Toc
is written with c
lower case. The C++ language differentiates lower case.
- Once you use strings, you should include the string library:
#include <string>
.
At this point your code becomes compilable, but it still doesn’t work. The errors that prevent it from working are:
- The stopping condition of the
while
must be Nm1 != "FIM"
and not ==
.
- The variable
Lcr
shall be used for the profit of the product and for the total profit, and two separate variables shall be created.
- In the clauses
if
were used <=
and =>
, but it should be <
and >
.
ToC
and Tov
shall be calculated outside the else
, straight into the nesting while
.
At this point your code works, but there are other relevant aspects to consider:
- Code formatting is important, adopt a style and be consistent.
- The name of variables should help understand the code.
- Formatting the output data should take care of aspects such as spacing and line breaking (special attention to the summary at the end of the loop).
I implemented a variant of your program with some of the changes described above.
#include <string>
#include <iostream>
using namespace std;
int main()
{
string Nm1;
float PrC, PrV, ToC, ToV, PrLcr, ToLcr;
int Qt1, Qt2, Qt3;
PrC = PrV = ToC = ToV = PrLcr = ToLcr = 0;
Qt1 = Qt2 = Qt3 = 0;
cout << "Nome da mercadoria: ";
cin >> Nm1;
while (Nm1 != "FIM")
{
cout << "Informe o preco de compra: "; cin >> PrC;
cout << "Informe o preco de venda: "; cin >> PrV;
PrLcr = 100 * (PrV - PrC) / PrC;
if (PrLcr < 10)
Qt1++;
else if (PrLcr < 20)
Qt2++;
else
Qt3++;
ToC = ToC + PrC;
ToV = ToV + PrV;
ToLcr = 100 * (ToV - ToC) / ToC;
cout << "\n";
cout << "Quantidade de mercadorias com lucro < 10%: " << Qt1 << "\n";
cout << "Quantidade de mercadorias com lucro < 20%: " << Qt2 << "\n";
cout << "Quantidade de mercadorias com lucro < 20%: " << Qt3 << "\n";
cout << "Valor total das compras: " << ToC << "\n";
cout << "Valor Total das vendas: " << ToV << "\n";
cout << "Lucro total (%) " << ToLcr << "\n";
cout << "\n";
cout << "Nome da mercadoria: ";
cin >> Nm1;
}
}
Wrong syntax of the for command. The syntax is: for (initialization; condition; Increase) statement;. Maybe you were thinking about while and not for.
– anonimo
To use string you need: #include <string>.
– anonimo
OK I still don’t know how to use the string, thanks for the tips I’ll apply
– cristovanlopes