Can anyone identify the error in this code?

Asked

Viewed 86 times

-3

Guys I’m sorry for the kinds of questions I’m starting

#include <iostream>
using namespace std;
int main(){
  //essa parte até o "for(Nm1==Fim)" o geany encontrou erro
  string  Nm1;
  float PrC=0, PrV=0, Lcr=0, ToC=0, ToV=0;
  int Qt1=0, Qt2=0, Qt3=0;
  for(Nm1== FIM)
  {
      cout<<"Nome da mercadoria: ";
      cin>>Nm1;
      if(Nm1!="FIM")
      {
        cout<<"informe o  preco de compra: ";cin>>PrC;
        cout<<"informe o  preco de v enda: ";cin>>PrV;
        Lcr=100*(PrV-PrC)/PrC;
        if(Lcr<10)
        {
          Qt1++;
        }else{
          if(Lcr>=10 && Lcr <=20)
          {
            Qt2++;
          }
          else
          {
            Qt3++;
          }ToC=Toc+PrC;
          ToV=ToV+PrV;
        }
        Lcr=100*(ToV-ToC)/ToC;

      }
      cout<<"Quantidade de mercadorias com lucro < 10%: "<<Qt1;
      cout<<"Quantidade de mercadorias com lucro < 20%: "<<Qt2;
      cout<<"Quantidade de mercadorias com lucro < 20%: "<<Qt3;
      cout<<"Valor total das compras: "<<ToC;
      cout<<"Valor Total das vendas: "<<ToV;
      cout<<"Lucro total (%) "<<Lcr;
  }
}
  • 1

    Wrong syntax of the for command. The syntax is: for (initialization; condition; Increase) statement;. Maybe you were thinking about while and not for.

  • To use string you need: #include <string>.

  • OK I still don’t know how to use the string, thanks for the tips I’ll apply

1 answer

2


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;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.