以前的值仍然存储在 do while 循环中的下一次迭代中

如何解决以前的值仍然存储在 do while 循环中的下一次迭代中

在这个将数字转换为单词的程序中。我已经使用 do while 循环重新启动(继续)程序。 但它正在制造问题。第一次程序成功运行,但是当条件满足 while 并且程序继续时,它会打印新值和以前的值。 下一次迭代中的类似条件。以前的值保持存储并每次打印新的值。 可能是什么错误?我找不到那个。

#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
    int n,i,r,p; /*r for remainder. i for increment. n for ruppes in number taken from user.*/
    char str1[100]={0},str2[100]={0},str3[100]={0},str4[100]={0},str5[100]={0},str6[100]={0},str7[100]={0},str8[100]={0},str9[100]={0},cont;
    do
    {
        clrscr();
        printf("\nvalid till 99 crore 99 lakhs 99 thousand 9 hundred 99");
        printf("\n\nwrite your ruppes in integer: ");
        scanf("%d",&n);
        p = n;
        if (n == 0)
            printf("\nYou don't have any ruppe.");

        i = 0;
        while (n > 0)
        {
            r = n % 10;
            n = n / 10;
            if (i == 0 && n % 10 != 1)
            {
                if (r == 1)
                    strcpy(str1,"one");
                else if (r == 2)
                    strcpy(str1,"two");
                else if (r == 3)
                    strcpy(str1,"three");
                else if (r == 4)
                    strcpy(str1,"four");
                else if (r == 5)
                    strcpy(str1,"five");
                else if (r == 6)
                    strcpy(str1,"six");
                else if (r == 7)
                    strcpy(str1,"seven");
                else if (r == 8)
                    strcpy(str1,"eight");
                if (r == 9)
                    strcpy(str1,"nine");
            }
            if (i == 1)
            {
                if (r == 1)
                {
                    if (p % 10 == 0)
                        strcpy(str2,"ten");
                    else if (p % 10 == 1)
                        strcpy(str2,"eleven");
                    else if (p % 10 == 2)
                        strcpy(str2,"twelve");
                    else if (p % 10 == 3)
                        strcpy(str2,"thirteen");
                    else if (p % 10 == 4)
                        strcpy(str2,"fourteen");
                    else if (p % 10 == 5)
                        strcpy(str2,"fifteen");
                    else if (p % 10 == 6)
                        strcpy(str2,"sixteen");
                    else if (p % 10 == 7)
                        strcpy(str2,"seventeen");
                    else if (p % 10 == 8)
                        strcpy(str2,"eighteen");
                    else if (p % 10 == 9)
                        strcpy(str2,"nineteen");
                }
                else if (r == 2)
                    strcpy(str2,"twenty ");
                else if (r == 3)
                    strcpy(str2,"thirty ");
                else if (r == 4)
                    strcpy(str2,"fourty ");
                else if (r == 5)
                    strcpy(str2,"fifty ");
                else if (r == 6)
                    strcpy(str2,"sixty ");
                else if (r == 7)
                    strcpy(str2,"seventy ");
                else if (r == 8)
                    strcpy(str2,"eighty ");
                if (r == 9)
                    strcpy(str2,"ninty ");
            }
            if (i == 2)
            {
                if (r == 1)
                    strcpy(str3,"one hundred ");
                else if (r == 2)
                    strcpy(str3,"two hundred ");
                else if (r == 3)
                    strcpy(str3,"three hundred ");
                else if (r == 4)
                    strcpy(str3,"four hundred ");
                else if (r == 5)
                    strcpy(str3,"five hundred ");
                else if (r == 6)
                    strcpy(str3,"six hundred ");
                else if (r == 7)
                    strcpy(str3,"seven hundred ");
                else if (r == 8)
                    strcpy(str3,"eight hundred ");
                if (r == 9)
                    strcpy(str3,"nine hundred ");
            }
            if (i == 3)
            {
                if (r == 0)
                    strcpy(str4,"thousand ");
                else if (r == 1)
                    strcpy(str4,"one thousand ");
                else if (r == 2)
                    strcpy(str4,"two thousand ");
                else if (r == 3)
                    strcpy(str4,"three thousand ");
                else if (r == 4)
                    strcpy(str4,"four thousand ");
                else if (r == 5)
                    strcpy(str4,"five thousand ");
                else if (r == 6)
                    strcpy(str4,"six thousand ");
                else if (r == 7)
                    strcpy(str4,"seven thousand ");
                else if (r == 8)
                    strcpy(str4,"eight thousand ");
                if (r == 9)
                    strcpy(str4,"nine thousand ");
            }
            if (i == 4)
            {
                if (r == 1)
                    strcpy(str5,"ten ");

                else if (r == 2)
                    strcpy(str5,"twenty ");
                else if (r == 3)
                    strcpy(str5,"thirty ");
                else if (r == 4)
                    strcpy(str5,"fourty ");
                else if (r == 5)
                    strcpy(str5,"fifty ");
                else if (r == 6)
                    strcpy(str5,"sixty ");
                else if (r == 7)
                    strcpy(str5,"seventy ");
                else if (r == 8)
                    strcpy(str5,"eighty ");
                if (r == 9)
                    strcpy(str5,"ninty ");
            }
            if (i == 5)
            {
                if (r == 0)
                    strcpy(str6,"lakh ");
                else if (r == 1)
                    strcpy(str6,"one lakh ");
                else if (r == 2)
                    strcpy(str6,"two lakh ");
                else if (r == 3)
                    strcpy(str6,"three lakh ");
                else if (r == 4)
                    strcpy(str6,"four lakh ");
                else if (r == 5)
                    strcpy(str6,"five lakh ");
                else if (r == 6)
                    strcpy(str6,"six lakh ");
                else if (r == 7)
                    strcpy(str6,"seven lakh ");
                else if (r == 8)
                    strcpy(str6,"eight lakh ");
                if (r == 9)
                    strcpy(str6,"nine lakh ");
            }
            if (i == 6)
            {
                if (r == 1)
                    strcpy(str7,"ten ");

                else if (r == 2)
                    strcpy(str7,"twenty ");
                else if (r == 3)
                    strcpy(str7,"thirty ");
                else if (r == 4)
                    strcpy(str7,"fourty ");
                else if (r == 5)
                    strcpy(str7,"fifty ");
                else if (r == 6)
                    strcpy(str7,"sixty ");
                else if (r == 7)
                    strcpy(str7,"seventy ");
                else if (r == 8)
                    strcpy(str7,"eighty ");
                if (r == 9)
                    strcpy(str7,"ninty ");
            }
            if (i == 7)
            {
                if (r == 0)
                    strcpy(str8,"crore ");
                else if (r == 1)
                    strcpy(str8,"one crore ");
                else if (r == 2)
                    strcpy(str8,"two crore ");
                else if (r == 3)
                    strcpy(str8,"three crore ");
                else if (r == 4)
                    strcpy(str8,"four crore ");
                else if (r == 5)
                    strcpy(str8,"five crore ");
                else if (r == 6)
                    strcpy(str8,"six crore ");
                else if (r == 7)
                    strcpy(str8,"seven crore ");
                else if (r == 8)
                    strcpy(str8,"eight crore ");
                if (r == 9)
                    strcpy(str8,"nine crore ");
            }

            if (i == 8)
            {
                if (r == 1)
                    strcpy(str9,"ten ");

                else if (r == 2)
                    strcpy(str9,"twenty ");
                else if (r == 3)
                    strcpy(str9,"thirty ");
                else if (r == 4)
                    strcpy(str9,"fourty ");
                else if (r == 5)
                    strcpy(str9,"fifty ");
                else if (r == 6)
                    strcpy(str9,"sixty ");
                else if (r == 7)
                    strcpy(str9,"seventy ");
                else if (r == 8)
                    strcpy(str9,"eighty ");
                if (r == 9)
                    strcpy(str9,"ninty ");
            }

            i++;
        }
        if (str9[0] != '\0')
        {
            strcat(str9,str8);
            strcat(str9,str7);
            strcat(str9,str6);
            strcat(str9,str5);
            strcat(str9,str4);
            strcat(str9,str3);
            strcat(str9,str2);
            strcat(str9,str1);
            printf("\n\n%s",str9);
        }
        else if (str9[0] == '\0')
        {
            strcat(str8,str7);
            strcat(str8,str6);
            strcat(str8,str5);
            strcat(str8,str4);
            strcat(str8,str3);
            strcat(str8,str2);
            strcat(str8,str8);
        }

        else if (str8[0] == '\0')
        {
            strcat(str7,str6);
            strcat(str7,str5);
            strcat(str7,str4);
            strcat(str7,str3);
            strcat(str7,str2);
            strcat(str7,str7);
        }

        else if (str7[0] == '\0')
        {
            strcat(str6,str5);
            strcat(str6,str4);
            strcat(str6,str3);
            strcat(str6,str2);
            strcat(str6,str6);
        }

        else if (str6[0] == '\0')
        {
            strcat(str5,str4);
            strcat(str5,str3);
            strcat(str5,str2);
            strcat(str5,str5);
        }
        else if (str5[0] == '\0')
        {
            strcat(str4,str3);
            strcat(str4,str2);
            strcat(str4,str4);
        }
        else if (str4[0] == '\0')
        {
            strcat(str3,str2);
            strcat(str3,str3);
        }
        else if (str3[0] == '\0')
        {
            strcat(str2,str2);
        }
        else
            printf("\n\n%s",str1);

        printf("\n\n press y or j for continue or any other key to exit:  ");
        scanf(" %c",&cont);

    } while (cont == 'y' || cont == 'j');
    printf("\nThanking You");
    return 0;
}


以下是代码编译后的输出

valid till 99 crore 99 lakhs 99 thousand 9 hundred 99

write your ruppes in integer: 121


one hundred twenty one

 press y or j for continue or any other key to exit:  y

valid till 99 crore 99 lakhs 99 thousand 9 hundred 99

write your ruppes in integer: 456


one hundred twenty onefour hundred fifty six

 press y or j for continue or any other key to exit:

这里在第一次程序中给出了正确的结果。但是在下一次迭代中,它给出了正确的结果,但结果会沿着上一次迭代的结果打印出来。表示在 do while 循环中的每次下一次迭代中,之前的值都会保留。

请看下面一个基于上述程序概念的小程序。很容易理解上述程序的概念。

#include <stdio.h>
#include <string.h>
//program to convert entered number to word from 21 to 99
int main()
{
    char str1[30] = {0},str2[30] = {0},c;
    int n,r;
    do
    {
        printf("write the value of integer within 21 to 99: ");
        scanf("%d",&n);
        for (i = 0; n > 0; i++)
        {
            r = n % 10; //for remainder
            n = n / 10;

            if (i == 0)
            {
                if (r == 1)
                    strcpy(str1," one");
                else if (r == 2)
                    strcpy(str1," two");
                else if (r == 3)
                    strcpy(str1," three");
                else if (r == 4)
                    strcpy(str1," four");
                else if (r == 5)
                    strcpy(str1," five");
                else if (r == 6)
                    strcpy(str1," six");
                else if (r == 7)
                    strcpy(str1," seven");
                else if (r == 8)
                    strcpy(str1," eight");
                else if (r == 9)
                    strcpy(str1," nine");
            }
            else if (i == 1)
            {
                if (r == 2)
                    strcpy(str2,"twenty");
                else if (r == 3)
                    strcpy(str2,"thirty");
                else if (r == 4)
                    strcpy(str2,"fourty");
                else if (r == 5)
                    strcpy(str2,"fifty");
                else if (r == 6)
                    strcpy(str2,"sixty");
                else if (r == 7)
                    strcpy(str2,"seventy");
                else if (r == 8)
                    strcpy(str2,"eighty");
                else if (r == 9)
                    strcpy(str2,"ninty");
            }
        }
        strcat(str2,str1);
        printf("\n%s",str2);

        printf("\nwrite y to continue or any other key to exit: ");
        scanf(" %c",&c);
    } while (c == 'y');
    printf("\nThanking You");
    return 0;
}

以上程序运行没有任何问题。只有第一个询问的程序有问题。抱歉这个概念,我不知道解决这个问题的任何其他概念。你可以推荐我。

解决方法

这里至少有一个错误:

第一次运行循环时,所有字符串都未初始化。因此,当您执行 if (str9[0] != '\0') 时,您很可能访问了一个非常糟糕的未初始化值。

此外,第二次运行循环时,所有字符串都具有第一次的值。这会产生奇怪的结果。

老实说,我不明白你的程序的逻辑,但要解决字符串问题更改:

char str1[100],str2[100],str3[100],str4[100],str5[100],str6[100],str7[100],str8[100],str9[100],cont;
do
{

char cont;
do
{
    char str1[100] = {0};
    char str2[100] = {0};
    ...
    char str9[100] = {0};

这部分看起来也很奇怪:

        n = n / 10;
        if (i == 0 && n % 10 != 1)

确定要在测试 n 之前更改 n % 10 != 1 吗?好像不对。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-