Conditional structure and repetition For simple in C

Asked

Viewed 98 times

1

This is a conditional structure code and repetition For simple in C. However, the output is being the sequence: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20. The expected output was a sequence of 1 to 10 if the number 1 was read, or a sequence of 1 to 20 if the number 2 was read. Could you please point out the error? Thanks in advance!

#include <stdio.h>
int main()
{
    int c1,c2,ops;
    printf("Digite um opcao:");
    scanf("%d",&ops);
    if(ops=1)
    {
        for(c1=1;c1<=10;c1++)
           printf("%d ",c1);
    }
    if(ops=2)
    {
        for(c2=1;c2<=20;c2++)
           printf("%d ",c2);
    }
    return 0;
}

3 answers

3


#include <stdio.h>
int main()
{
    int c1,c2,ops;
    printf("Digite um opcao:");
    scanf("%d",&ops);
    if(ops==1)
    {
        for(c1=1;c1<=10;c1++)
           printf("%d ",c1);
    }
    if(ops==2)
    {
        for(c2=1;c2<=20;c2++)
           printf("%d ",c2);
    }
    return 0;
}

On parole if by placing only one equal Voce assigns this value, when you put two equal (==) Voce validates the value, for example if(i == 1) ie if i was = a 1, if if(i = 1) Voce is assigning this value to i valley 1. Understood?

  • 1

    Thank you very much!

  • You’re welcome, we need you ;)

2

You used the assignment operator = at the place of the comparison operator == us if, for this reason happens the problem.

#include <stdio.h>
int main()
{
    int c1,c2,ops;
    printf("Digite um opcao:");
    scanf("%d",&ops);
    if(ops==1)
    {
        for(c1=1;c1<=10;c1++)
           printf("%d ",c1);
    }
    if(ops==2)
    {
        for(c2=1;c2<=20;c2++)
           printf("%d ",c2);
    }
    return 0;
}

0

As already clarified, you used the assignment operator = and not that of equality ==. Common mistake, actually. Anyway, I wrote a little algorithm to contribute in some way. ^_^

#define LC_CTYPE 2

// Protótipos
char* setlocale(int, const char*);
int printf(const char*, ...);
int scanf(const char*, ...);
int putchar(int);

int _cdecl main(void)
{
    setlocale(LC_CTYPE, "PtB"); // Para escrever em português do Brasil

    printf("Digite um número: ");
    unsigned int num = 0;

    if ((!scanf("%u", &num)) || (num != 1 && num != 2)) {
        printf("\nErro: digite um 1 ou 2.\n");
        return 1;
    }

    unsigned y = 0;

    if (num == 1) {
        y = 10;
    }
    else {
        y = 20;
    }

    putchar('\n');

    unsigned int x;
    for (x = 1; x <= y; x++) {
        printf("%u ", x);
    }

    putchar('\n');

    return 0;
}

This code is more efficient and complete because it uses iteration-for only once. In addition, it has a basic error control, which should be part of any program, and contemplates our language, allowing accents and things like.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Browser other questions tagged

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