Build error in C++

Asked

Viewed 319 times

-1

I need to make a loop (Repeat-Until), but is giving error.

#include <iostream>
#include <cstdlib>
using namespace std;

int main (void){
    char deseja=1, valordomes1, valordomes2, valordomes3, totalFilia, totalEmpresa;
    do  
    {
        printf "valor do mes 1:";
        scanf >> ("valordomes1");
        printf "valor do mes 2:";
        scanf >> ("valordomes2;")
        printf "valordomes3:  ";
        scanf >> valordomes3;
        scanf >>  totalfilia := (valordomes1 + valordomes2 + valordomes3);
        printf "Valor total da filial:" totalfilia;
        printf "------------------------------------";
        totalempresa = (totalfilia);
        printf "Total do restaurante:" totalempresa;
        printf "se nao deseja calcular o proximo valor de vendas de outra filial digite o numero zero";

        while ( ! deseja=0 );
    }
    return 0;
}
  • 2

    Your code has such a large number of syntax errors, I ask: What led you to write it this way?

  • People just left portugol, have kkk patience.. Giving this error: [Error] invalid operands of types 'int(const char*, ...)' and 'const char [12]' to Binary 'Operator>>'

  • And why are you trying to use things C decided to program in C++? The first thing you need to decide is what language you’re going to use, that might be the reason for the initial confusion.

3 answers

2


The code has a lot of errors, and although it will be able to deliver the work it does not seem that you will be learning to solve what comes up and this will bring you problems in the future. A lot of errors occur by mixing C with C++. Even when written correctly you should not do this. As it was said that it should be C++ this is how I will demonstrate.

#include <iostream>
using namespace std;

int main(void) {
    int totalempresa = 0;
    while (1) {
        int valordomes1, valordomes2, valordomes3;
        cout << "valor do mes 1:";
        cin >> valordomes1;
        if (valordomes1 == 0) break;
        cout << endl << "valor do mes 2:";
        cin >> valordomes2;
        cout << endl << "valor do mes 3:";
        cin >> valordomes3;
        int totalfilia = valordomes1 + valordomes2 + valordomes3;
        cout << endl << "Valor total da filial: " << totalfilia << endl;
        cout << "------------------------------------" << endl;
        totalempresa += totalfilia;
        cout << "Total do restaurante: " << totalempresa << endl;
        cout << "se nao deseja calcular o proximo valor de vendas de outra filial digite o numero zero" << endl;
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

All things are printf must be cout, is correct for C++. And where it is scanf must use cin. And note well the operators of stream << used in the cout since you take data and send it to the exit, the cout, and >> where you send the entrance that comes from cin for a variable. So it has a logic of when to use each and where.

Beyond that I put endl at the exits to decorate better and skip lines where relevant. Each different die should be a stream different. In some cases using a ` n within the text itself is well acceptable and correct.

If the exit criterion is to type zero in the first input then the loop logic is wrong. It should have an infinite loop and right after someone type the number of the first month should check if it is 0 and if it is leaving the loop.

Only one variable should be outside the loop that is the one that calculates the total of the restaurants, after all this variable can not be zeroed in each passage of the loop, the others can and even one should be zeroed to work, and anyway declaring a variable closer to where it is used is the most suitable, so I passed all the other variables that remained (eliminated one) into the loop, and doing the initialization. If you don’t initialize, you can pick up garbage and there’s a lot of things wrong.

I eliminated the unnecessary parentheses, but mostly eliminated the := which is a non-existent operator in C++ (or C).

I did sum up the affiliate in the company which is something that seems to me that was what I wanted. Without making the sum has a logic error.

Another mistake is that some places that are as text were the names of variables, I removed the quotation marks. How he had case that did right does not seem ignorance but lack of care.

I didn’t change the variable names but I should adopt a style pattern and give full meaningful names, not letters, it just has disadvantages to do this. Then a valorDoMes1 would be better, just as totalFilial. Standardizing the style holds for other things that I realized there wasn’t much worry. Had some variables with wrong name and so the code would not compile.

Programming is understanding what each character of the code does, even the blank space, not playing a lot of text anyway and hoping it works. First understand what is happening in each character and next you can do it knowing what it is.

  • The criterion of leaving was at the end of the total company the user choose whether to continue or not. I was in doubt even as to the parentheses, but the question of the names of the variables was as the college itself asked, rs. I’ll pay more attention next time, thanks for the help.

  • 1

    It’s not what’s in your code, it says something else. If college asked to use bad variable names I’d get out of it now.

1

There are a lot of errors. This is just the first one that the compiler found. In particular, the use of >> is something C++ uses cout. Already the scanf that is C does not use >>. By the way, this error that you had looks like C++ and not C. I must remember that C and C++ are very different languages, and that you are probably trying to learn C, but your code is closer to C++.

First, scanf and printf are functions. As such, you MUST always use parentheses in them.

Second, keep in mind that printf and scanf use format specifiers such as %d, %c, %s and %f. Although there are many similarities, the specifiers of the printf are different from those used in scanf. See those of scanf here and those of printf here.

Third, that in C the practice of declaring all variables in the beginning is not seen with a good eye, being preferred to declare them when they are used. However, this detail is more a matter of code writing style and will not cause errors in the program.

Trying to change his program quickly, I think he gets like this:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int deseja = 1, valordomes1, valordomes2, valordomes3, totalFilial, totalEmpresa = 0;
    do {
        printf("valor do mes 1: ");
        scanf("%d", &valordomes1);
        printf("valor do mes 2: ");
        scanf("%d", &valordomes2);
        printf("valor do mes 3: ");
        scanf("%d", &valordomes3);
        totalFilial = valordomes1 + valordomes2 + valordomes3;
        totalEmpresa += totalFilial;
        printf("Valor total da filial: %d\n", totalFilial);
        printf("------------------------------------n");
        printf("Total do restaurante: %d\n", totalEmpresa);
        printf("Se nao deseja calcular o proximo valor de vendas de outra filial digite o numero zero.\n");
        scanf("%c", &deseja)
    } while (deseja != '0');
    return 0;
}
  • 1

    Now it ran, thanks for the tips. And yes, the work was to be done in C++, I was confusing. thanks!

0

You are mixing C++ with C by the way, follows below the correct code of what I understood.

#include <stdio.h>
#include <stdlib.h>

int main (void){
    int deseja;
    float valordomes1, valordomes2, valordomes3, totalFilia, totalEmpresa;
    do{
            printf("valor do mes 1: ");
            scanf("%f", &valordomes1);
            printf("valor do mes 2: ");
            scanf("%f", &valordomes2);
            printf("valordomes3: ");
            scanf("%f", &valordomes3);
            totalFilia = (valordomes1 + valordomes2 + valordomes3);
            printf("Valor total da filial: %f \n", totalFilia);
            printf("------------------------------------\n");
            totalEmpresa = (totalFilia);
            printf("Total do restaurante: %f \n", totalEmpresa);
            printf("se nao deseja calcular o proximo valor de vendas de outra filial digite o numero zero\n");
            scanf("%d", deseja);
    }while(deseja == 1);
    return 0;
}

Browser other questions tagged

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