我的代码有什么问题? [英] What is wrong about my code?

查看:88
本文介绍了我的代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试测试存储在名为number的varible中的值是否为字母时,我得到无限的消息行,甚至没有通过循环。我尝试用if语句,函数,开关测试,我也尝试过动态内存分配。

When I try to test if the value stored in varible called "number" is a letter, I get infinite message line, without even getting through the loop. I tried to test with if statement, function, switch, I also tried dynamic memory allocation.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>


int main()
{

	int key;
	int c = 0;
	int number;
	int coins;
	int coins2;
	char answear;
	int n;
    int *p;
	
	
	
	
	FILE *fp;
	fp=fopen("D://coins.txt", "r");
	PAR2:coins = 100;
	if(!fp) // daca fp nu exista;
	{
		printf("No previous data!.\n");
	}
	if (!feof(fp)) // the condition to be read line by line from the txt file.
	{
		printf("Taking values:\n");
		fscanf(fp, "%d", &c);
		coins = c;
	}
	fclose(fp);		
	for(n=1;n>0;)
	{		
		if(coins<=0)
		{
		PAR4:printf("Sorry but you have no dollars left. Try again Y/N?\n");
		PAR3:answear = getch();
		switch (answear)
			{
			case 'Y': 
				{	
				goto PAR2;
				}
			case 'y':
				{		
				goto PAR2;
				}
			case 'N': 
				{
				exit(1);
				}
			case 'n':
				{
				exit(1);
				}
			default: 
				{
				printf("Not an option. Answear only with Y/N!\n");
				goto PAR3;
				}
			}
		}
		PAR1:printf("You have: %d$\n", coins);
		printf("\nEnter your number! WARNING: it must be between 1 and 5 only.\n");
		p=(int*)malloc(sizeof (int));
		scanf("%d", p);
		if(*p>0 && *p<6)
		{
		printf("Your number is: %d\n", number);
		key=(rand ()%5)+1;
		printf("The extracted number is: %d\n", key);
		if(key==number)
		{
			printf("Congratulations! You have won 10$.\n");
			coins2 = coins+10;		
		}
		else 
		{
			printf("You are not so lucky this time!\n");
			coins2 = coins-10;
		}		
		coins=coins2;		
		printf("Rotate again? Y/N!\n");
		PAR5:answear = getch();
		switch (answear)
			{
				case 'Y':
				{
				system("cls");
				goto PAR4;
				
				}
				case 'y':
				{
				system("cls");
				goto PAR4;
				}
				case 'N':
				{
				fp=fopen("D://coins.txt", "w");
				fprintf(fp,"%d", coins);
				fclose(fp);
				exit(1);
				}
				case 'n':
				{
				fp=fopen("D://coins.txt", "w");
				fprintf(fp,"%d", coins);
				fclose(fp);
				exit(1);
				}
				default:
				{
				printf("Not an option. Answear only with Y/N!\n");
				goto PAR5;
				}
			}
		}
		else
		printf("Wrong number:\n");
		free (p);
	}
	
	getch();
	return 0;
}





我的尝试:



如果语句,函数,开关,动态内存分配。



What I have tried:

If statement, function, switch, dynamic memory allocation.

推荐答案

\ n,硬币);
printf( \ n输入您的号码!警告:它必须介于1到5之间。\ n);
p =( int *)malloc( sizeof int ));
scanf( %d,p);
if (* p> 0&& * p< 6)
{
printf(< span class =code-string> 您的号码是:%d \ n,号码);
key =(rand ()%5)+1;
printf( 提取的数字为:%d \ n ,key);
if (key == number)
{
printf( 恭喜!你赢了10
\n", coins); printf("\nEnter your number! WARNING: it must be between 1 and 5 only.\n"); p=(int*)malloc(sizeof (int)); scanf("%d", p); if(*p>0 && *p<6) { printf("Your number is: %d\n", number); key=(rand ()%5)+1; printf("The extracted number is: %d\n", key); if(key==number) { printf("Congratulations! You have won 10


。\ n);
coins2 =硬币+ 10;
}
else
{
printf( 你不是这样的幸运的是这次!\ n);
coins2 = coins- 10 ;
}
coins = coins2 ;
printf( 再次旋转?是/否!\ n);
PAR5:answear = getch();
switch (answear)
{
case ' Y'
{
system( cls);
goto PAR4;

}
case ' y'
{
system( cls);
goto PAR4;
}
case ' N '
{
fp = fopen( D:// coins。 txt w);
fprintf(fp, %d,硬币);
fclose(fp);
退出( 1 );
}
case ' n '
{
fp = fopen( D:// coins。 txt w);
fprintf(fp, %d,硬币);
fclose(fp);
退出( 1 );
}
默认
{
printf( 不是一个选项。仅限Y / N!\ n);
goto PAR5;
}
}
}
其他
printf( 错号:\ n);
free(p);
}

getch();
return 0 ;
}
.\n"); coins2 = coins+10; } else { printf("You are not so lucky this time!\n"); coins2 = coins-10; } coins=coins2; printf("Rotate again? Y/N!\n"); PAR5:answear = getch(); switch (answear) { case 'Y': { system("cls"); goto PAR4; } case 'y': { system("cls"); goto PAR4; } case 'N': { fp=fopen("D://coins.txt", "w"); fprintf(fp,"%d", coins); fclose(fp); exit(1); } case 'n': { fp=fopen("D://coins.txt", "w"); fprintf(fp,"%d", coins); fclose(fp); exit(1); } default: { printf("Not an option. Answear only with Y/N!\n"); goto PAR5; } } } else printf("Wrong number:\n"); free (p); } getch(); return 0; }





我的尝试:



如果语句,函数,开关,动态内存分配。



What I have tried:

If statement, function, switch, dynamic memory allocation.


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



您的代码

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Your code
switch (answear)
	{
	case 'Y': 
		{	
		goto PAR2;
		}
	case 'y':
		{		
		goto PAR2;
		}
	case 'N': 
		{
		exit(1);
		}
	case 'n':
		{
		exit(1);
		}
	default: 
		{
		printf("Not an option. Answear only with Y/N!\n");
		goto PAR3;
		}
	}



可以简化


can be simplifyed

switch (answear)
	{
	case 'Y': 
	case 'y':
		{		
		goto PAR2;
		}
	case 'N': 
	case 'n':
		{
		exit(1);
		}
	default: 
		{
		printf("Not an option. Answear only with Y/N!\n");
		goto PAR3;
		}
	}



使用标签 gotos 是一个坏习惯,应该不惜一切代价避免。它们应该保留给特殊情况并考虑速度。


Using labels and gotos is a bad habit and should be avoided at all cost. They should be reserved to special cases and where speed is considered.


这篇关于我的代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆