首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当我在循环之外时,如何使用在for循环中声明的变量?

当我在循环之外时,如何使用在for循环中声明的变量?
EN

Stack Overflow用户
提问于 2022-07-18 13:07:24
回答 1查看 85关注 0票数 1

我正在学习C,我需要帮助。我只是在编写我的课程练习中的一个小程序,我需要在循环之外使用我的变量(即for循环中的变量)。我知道这是个很蠢的问题,但我需要你的帮助。下面是我在CS50 IDE中编写的代码:

代码语言:javascript
运行
复制
#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <ctype.h>
#include <string.h>

int main(void)
{
    // Verifies if the card_number is between 13 and 16 digits
    long card_number = 0;
    for (int i = 0; i < 20; i++)
    {
        card_number = get_long("Insert the card's number: ");
        int reach_zero = 0; 
        int digit_count = 0; 
        do
        {
            reach_zero = (card_number /= 10);
            digit_count++;
        }
        while (reach_zero != 0); 
        if (digit_count >= 13 && digit_count <= 16)
        {
            break; 
        }
    }
    // Prints the card_number 
    printf("%li\n", card_number);
}

我只需要printf card_number。

EN

回答 1

Stack Overflow用户

发布于 2022-07-18 13:13:42

如果我已经理解了这个问题,那就写吧

代码语言:javascript
运行
复制
    card_number = get_long("Insert the card's number: ");
    long reach_zero = card_number; 
    int digit_count = 0; 
    do
    {
        reach_zero /= 10;
        digit_count++;
    }
    while (reach_zero != 0); 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73022805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档