Beginner Language Error C Code Blocks

Asked

Viewed 92 times

-4

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

int main();
{int dinheiro = 200};
int r$100, r$50, r$20, r$10, r$5, r$2, r$1;
int inter;

while(dinheiro > 0) {

        if (dinheiro >= 100) {
        r$100 = dinheiro/100;
            dinheiro = dinheiro - r$100*100;
        }
        else{r$100 = 0;}
        if((dinheiro >= 50) && (dinheiro < 100)) {
        r$50 = dinheiro/50;
            dinheiro = dinheiro - r$50*50;
        }
        else{r$50 = 0;}
        if ((dinheiro >= 20) && (dinheiro < 50)) {
        r$20 = dinheiro/20;
            dinheiro = dinheiro - r$20*20 ;
        }
        else{r$20 = 0;}
        if ((dinheiro >= 10) && (dinheiro < 20)) {
        r$10 = dinheiro/10;
            dinheiro = dinheiro - r$10*10;
        }
        else{r$10 = 0;}
        if ((dinheiro >= 5) && (dinheiro < 10)) {
        r$5 = dinheiro/5;
            dinheiro = dinheiro - r$5*5;
        }
        else{r$5 = 0;}
        if ((dinheiro >= 2) && (dinheiro < 10)) {
        r$2 = dinheiro/2;
            dinheiro = dinheiro - r$2*2;
        }
        else{r$2 = 0;}
        if ((dinheiro >= 1) && (dinheiro < 1)) {
        r$1 = dinheiro/1;
            dinheiro = dinheiro - r$1*1;
        }
        else{r$1 = 0;}

        printf("notas de r$100 = %d\n", r$100);
        printf("notas de r$50 = %d\n", r$50);
        printf("notas de r$20 = %d\n", r$20);
        printf("notas de r$10 = %d\n", r$10);
        printf("notas de r$5 = %d\n", r$5);
        printf("notas de r$2 = %d\n", r$2);
        printf("notas de r$1 = %d\n", r$1);
        return 0;




}
  • There is no sense in this {} in the money statement. The $ character is not a valid character to use in an identifier. Next time report what error is occurring. The operator = is the assignment operator, it seems to me that you want to check an equality and in this case you should use the ==.

  • Caro @anonimo the $ there is part of the variable names, in gcc 6.3 it worked normally https://ideone.com/Vck8bN.

  • From the gcc 9.1 manual: "GCC Allows the ː$' Character in Identifiers as an Extension for Most targets. This is true regardless of the Std= switch, Since this Extension cannot Conflict with standards-conforming Programs. When preprocessing Assembler, However, dollars are not Identifier characters by default. "

3 answers

2

You didn’t use { correctly. While ta out of main function. You put int main(); That’s why the function gave error and did not execute.

Solution to the problem.

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

int main()
{
    int dinheiro = 200;
int r$100, r$50, r$20, r$10, r$5, r$2, r$1;
int inter;
    while(dinheiro > 0) {
        if (dinheiro >= 100) {
        r$100 = dinheiro/100;
            dinheiro = dinheiro - r$100*100;
        }
        else{r$100 = 0;}
        if((dinheiro >= 50) && (dinheiro < 100)) {
        r$50 = dinheiro/50;
            dinheiro = dinheiro - r$50*50;
        }
        else{r$50 = 0;}
        if ((dinheiro >= 20) && (dinheiro < 50)) {
        r$20 = dinheiro/20;
            dinheiro = dinheiro - r$20*20 ;
        }
        else{r$20 = 0;}
        if ((dinheiro >= 10) && (dinheiro < 20)) {
        r$10 = dinheiro/10;
            dinheiro = dinheiro - r$10*10;
        }
        else{r$10 = 0;}
        if ((dinheiro >= 5) && (dinheiro < 10)) {
        r$5 = dinheiro/5;
            dinheiro = dinheiro - r$5*5;
        }
        else{r$5 = 0;}
        if ((dinheiro >= 2) && (dinheiro < 10)) {
        r$2 = dinheiro/2;
            dinheiro = dinheiro - r$2*2;
        }
        else{r$2 = 0;}
        if ((dinheiro >= 1) && (dinheiro < 1)) {
        r$1 = dinheiro/1;
            dinheiro = dinheiro - r$1*1;
        }
        else{r$1 = 0;}

        printf("notas de r$100 = %d\n", r$100);
        printf("notas de r$50 = %d\n", r$50);
        printf("notas de r$20 = %d\n", r$20);
        printf("notas de r$10 = %d\n", r$10);
        printf("notas de r$5 = %d\n", r$5);
        printf("notas de r$2 = %d\n", r$2);
        printf("notas de r$1 = %d\n", r$1);
        return 0;
    }
}
  • Maury edits your reply, puts the codes and error messages as text and not as image!

  • 1

    Ok. But it was because I used mobile browser. I’m sorry.

1

You do a lot of unnecessary operations. Just:

#include <stdio.h>
int main() {
    int dinheiro = 200;
    int r$100, r$50, r$20, r$10, r$5, r$2, r$1;
    int inter;
    r$100 = dinheiro / 100;
    dinheiro %= 100;
    r$50 = dinheiro / 50;
    dinheiro %= 50;
    r$20 = dinheiro / 20;
    dinheiro %= 20;
    r$10 = dinheiro / 10;
    dinheiro %= 10;
    r$5 = dinheiro / 5;
    dinheiro %= 5;
    r$2 = dinheiro / 2;
    dinheiro %= 2;
    r$1 = dinheiro;
    printf("notas de r$100 = %d\n", r$100);
    printf("notas de r$50 = %d\n", r$50);
    printf("notas de r$20 = %d\n", r$20);
    printf("notas de r$10 = %d\n", r$10);
    printf("notas de r$5 = %d\n", r$5);
    printf("notas de r$2 = %d\n", r$2);
    printf("notas de r$1 = %d\n", r$1);
    return 0;
}

If you use array you can simplify even more.

1

I can give you a LIST of mistakes that were made there.

To begin with, there is no function main(),which makes the code not even executable (if, and only if, the compiler accepts it).

Instead of doing all those conditional ones (if's and else's), along with that while there, why not replace by code like this:

dinheiro %= x;
r$x = dinheiro / x;
dinheiro %= y;
r$y = dinheiro/y;

just as the "anonymous" user did?

The code is confusing and strange, besides having points like this

int main();
{int dinheiro = 200};
int r$100, r$50, r$20, r$10, r$5, r$2, r$1;
int inter;

where I have no idea what I was trying to be done.

My tip: take a look at a basic course C/C++ in the YouTube, accesses websites such as Apostilando, or the Programação Progressiva that you will understand better what is happening (even because simply putting the code here does not help me in much if I do not know your doubt).

Browser other questions tagged

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