简体   繁体   中英

cs50 sandbox for-loop issue

I want to make a program that, when I give it a number, it counts from it downwards.

Here is my code:

#include <stdio.h>
#include <cs50.h>
 
int main(void) {
    int i = get_char("Choose a number.\n");
    for (int a = 0; a < i; a++) {
         printf("a\n");
    }
}

A few things:

  1. You're printf ing "a", not the value of the variable a. Try:

     print("%d\n", a);
  2. You're not counting down from i to 1; you're counting up from 0 to i-1 . You want something more like:

     for (int a = i; a >= 1; a--)

    (And if you want to count down to 0, obviously, change that 1 to 0.)

  3. You're reading a character, but you want to read an integer:

     int i = get_int("Choose a number.\n");
  4. And you're not returning anything from a function that is supposed to return an int .

     return 0; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM