Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
C++
#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.
Posted
Updated 5-Mar-16 2:54am
v2
Comments
jeron1 4-Mar-16 17:48pm    
Have you stepped through the code with a debugger?
Sergey Alexandrovich Kryukov 4-Mar-16 20:15pm    
Why are you not telling us what you really tried?
What message? In what line?
You hard-code everything; this is bad...
—SA

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
C++
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
C++
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;
		}
	}

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.
 
Share this answer
 
I have found a solution, I was needed to place a fflush(stdin) function before the scanf function.
 
Share this answer
 
A lot is wrong with your code....

It seems that you don't know the DRY (Don't repeat yourself) principle.

You should also split the code in multiple functions (SRP - single responsability principle).

Gotos should almost never be used. And when used labels should stand out (usually by putting them at column 1 and even on their own line). It would improve code readability. And by the way your labels names are very poorly chosen. This lead to unmaintainable code. This is also the case for variable name. Do you really think that c, n and p are well chosen name?

You don't properly handle memory and have memory leaks. By the way, in that case there are no reason to use dynamic allocation. You get memory leak because in some paths you call exit and in some cases your gotos (in particular goto PAR4) skip over free function call.

Also in some case the indentation is wrong. This is the case for an else for which the statement under it is at the same level. It can cause humans to improperly interpret the code...

That code is probably nearer to win an obfuscation contest than to be accepted in production code! At school, it would typically get evaluated according to what you should have learn at this point...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900