Home > Net >  Code in C where it prints from 0 to 100 then asks if user wants to see it again
Code in C where it prints from 0 to 100 then asks if user wants to see it again

Time:10-11

I'm trying to make a program in C where it prints from 0 to 100 then asks the user to print it again. It's not working.

#include <stdio.h>
#include <string.h>

int main()
{
    int num;
    char resposta[0];

    resposta[0] = 's';
    num = 0;

    do
    {
        for (int i = 0; i < 100; i  )
        {
            num  = 1;
            printf("%i\n", num);
            printf("Repetir?s/n\n");
            scanf("%c", resposta[0]);
        }

    } while ((strcmp(resposta[0], "S") == 0) || (strcmp(resposta[0], "s") == 0));

    return 0;
}

CodePudding user response:

when declaring an array in C, the number in the brackets [] is not the index number, it is the total number of elements contained within the array. Setting char resposta[0]; means that resposta contains no elements.

This is different from when accessing elements, which uses a zero-index. So running scanf("%c", resposta[0]); is trying to access the first element, but there are zero elements within resposta, so that's likely why this isn't working.

I also figure I should add that it is entirely possible to just create a char variable that is not an array, since it seems you only need the one character. Just do char resposta; without the brackets.

Some more info on Arrays if you're interested: W3Schools

CodePudding user response:

There are many issues.

  1. You have an array that can contains 0 elements which is rather pointless, you want char resposta[1].
  2. But you don't want an array of chars anyway. You're using the %c specifier you need a single char instead of an array of chars.
  3. You're mixing up strings and chars. Change strcmp(resposta[0], "S")==0 to resposta == 'S', provided you have declared char resposta;.
  4. You're asking the user if he wants to start over again in the for loop which is probably not what you want.

Basically you want this (untested code):

int main() {
  char resposta;
  resposta = 's';
  int num = 0;

  do {
    num = 0;
    for (int i = 0; i < 100; i  ) {
      num  = 1;
      printf("%i\n", num);
    }

    printf("Repetir?s/n\n");
    scanf("%c", &resposta);    // not the useage of the & operator here
  } while (resposta == 'S' || resposta == 's');
}

CodePudding user response:

One of the reasons it is not working is that the program stops to ask the user to continue inside the loop. Another failure is that the counter being used is not reset to zero for the second and subsequent runs. Another failure is the scanf format specifier that does not clear the newline out of the input buffer. The next 'get input' will not find an S/s to continue, but will pickup a LF and terminate the loop.

Things can be simplified if they are separated. The interaction with the user is very clearly defined and should be factored-out into its own function:

#include <stdio.h>

int again(void) {
    puts( "Repetir? " );
    char ch;
    return scanf( " %c", &ch ) == 1 && (ch == 'S' || ch == 's' );
}

// Edit: Just noticed the OP title says "0 to 100"
void out(void) { 
    int i = 0; 
    while( i <= 100 ) printf( "%i\n", i   ); 
}

int main() {
    do out(); while( again() );
    return 0;
}
  • Related