如何解决以前的值仍然存储在 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 举报,一经查实,本站将立刻删除。