Προς το περιεχόμενο

giati vgazei error aftos o kodikas?


Toufas

Προτεινόμενες αναρτήσεις

Δημοσ.

re paidia sto bloodshed mou kanei compile mia xara...sto linux vgazei error akoma kai stin 6i grammi....ena comment einai kai to svino kai sinexizei na vgazei error

o kodikas einai o eksis,einai ena game kremalas:

>#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>


// this returns the length of the string
int strLength(char* s)
{
// temporary counter variable
int i = 0;

// temporary char* 
char* start;

// set the start address to the s address
start = s;

//while start is not empty continue to increment i
while(*start++) 
	i++;

// i now has the number of chars in s, simply return this
return i;
}

int hangMan(char c, char* str, char* tempStr, int* count)
{
// the dup array (size 20) stores the characters that user inputted 
// which match the hidden word, this is used to see if user has inputted
// the same character more then once. It is declared static becuase we do
// not want to create a new array every time this function is called
static char dup[20] = {0} ;

// this is used to keep place in the dup array, so next time this function is 
// called we continue from the next position we left off at
static int i = 0;

// used to traverse the array
int index = 0;

// temp variable used for traversing 
int temp = 0;

// bool type variable used to denote if hidden word contains more then one
// instance of the same character
int found = 0;

// main loop - while hidden word is not empty
while(str[index])
{

	// if  the position we're at (index) matches the input
	if(str[index] == c)
	{
		// check for duplicates in the array to see if user has typed
		// this already
		while(dup[temp])
		{
			// if we find the character in the array that means user
			// has already typed this letter before - set found to 1
			if(dup[temp] == c)
			    found = 1;
			temp++;
		}

		// store this letter in our dup array so we can find it next time
		dup[i++] = c;
	
		// if found was set to 1 - we detected user has inputted same letter twice
		// thus immiadiately return from this function
		if(found)
			return -1;

		// temp variable 
		int k = 0;

		// this is where we fill in the dashes from what the user has inputted
		while(str[k])
		{
			if(str[k] == c)
			{
				// the '-' are replaced by the contects of c
				tempStr[k] = c;

				// increment counter because we filled in a character
				(*count)++;
			}
			// increment counter to the next element 
			k++;
		}
		// if we're here then everything went ok, return 1	
		return 1;
	}
	// we'll be here if the user input char doesnt match the current position
	// in the array, which means increment index so we're looking at the next element
	else
		index++;
}

// we really shouldnt get here, but if you're then return 0 which is failure.
return 0;
}

int main()
{
// this is our hidden word
char* srcWord = "computer"; 

// this is our string that we'll work with ( ex: --a-s--)
char* str;

// get the length of the hidden word to use in our dynamic array
int len = strLength(srcWord);

// check to see if proper allocation was successfuly
// if not exit the program
if ( ( str = (char *)malloc(len*sizeof(char)) ) == NULL )
   {
    printf("\nError, memory not allocated.\n");
    exit(1);
   }

// allocated proper memmory, time to initialize the array with '-' 
int y;
for (y=0; y < len; y++ )
	    str[y] = '-';

str[y] = '\0';

// used to catch user input
char inBuf[5] = {};

// user input variable
char input = 0;

// generic variable used for counting
int i;

// variable used to catch the return value 
int result = 0;

// used to count the number of char user guessed correctly
int counter = 0;

// count the number of times user has tried to guess the word 
int loopCount = 0;

// display heading for the game
printf("GUESS THE WORD GAME\n");
printf("Input letters until all characters are known, or use * to stop\n\n");

// print out the dashes 
printf("The word: ");
for (y=0; y < len; y++ )
	    printf("%c", str[y]);
printf("\n");


// checks to see if user inputs correctly - no strings or numbers
while(1)
{
	// ask user for a character
	printf("Input a character > ");
	scanf("%s", inBuf);

	// check to see if user inputted more then one char
	if(1 < strLength(inBuf))
		printf("Error: Input must be a single char\n\n");

	// is the number a digit?
	else if (isdigit(inBuf[0]))
		printf("Error: Only characters allowed!\n\n");

	// else no errors found - valid input
	else
		break;
}

// store the response in variable input 
input = inBuf[0];

// since we read in a char, increment loopCount
loopCount++;

int done = 0;
// while the user doesnt input a *
while(input != '*')
{
	// pass the function: char, hidden word, current string, counter
	result = hangMan(input, srcWord, str, &counter); 

	// if results 1 - a match has been found print out the current string
	// that the user has guessed correctly so far
	if(result == 1)
	{ 
		i = 0;
		while(str[i])
			printf("%c", str[i++]);

		printf("\n\n");
	}

	// this means user tried to guess the same letter as before
	else if(result == -1)
		printf("You already tried the letter '%c'\n\n", input);	

	// this is default case in which the char doesnt appear in the hidden word
	else
		printf("'%c' does not occur in the word\n\n", input);


	// this senses that user has guessed the entire word correctly
	// thus prompts the user with appropriate message then breaks from loop
	if(counter == len)
	{
		printf("*** Congratulations, you guessed the word. ");
		printf("You had %i guesses ***\n", loopCount);
		printf("The word was: ");

		i = 0;
		while(str[i])
			printf("%c", str[i++]);
		printf("\n");
		break;	
	}

	// check to see if to allow user any more guess 
	if (loopCount > 9)
	{
		printf("The max allowed guesses are %i - which you already used up!\n",loopCount);
		printf("The word was: %s\n", srcWord);
		break;
	}

	// while not done, continue loopin until user input is valid
	// no numbers or strings
	done = 0;
	while(!done)
	{
		// ask user for a character
		printf("Input a character > ");
		scanf("%s", inBuf);

		// check to see if user inputted more then one char
		if(1 < strLength(inBuf))
			printf("Error: Input must be a single char\n\n");

		// is the input a number?
		else if (isdigit(inBuf[0]))
			printf("Error: Only characters allowed!\n\n");

		// else no errors occured - valid input
		else
			done = 1;
	}

	// store the response in variable input 
	input = inBuf[0];

	// keep track of how many times we cycled this loop
	loopCount++;
}

return 0;
}

Δημοσ.

Loipon file Toufas prwta kapoies parathrhseis . Den einai swsto na dhlwneis metavlhtes afou exeis grapsei kwdika . O compiler gcc tou Linux den nomizw na to dexetai auto (prepei na einai eguro gia tous compilers pou einai sumvatoi me to suntaktiko C99 alla den pairnw kai orko gia auto ) . E3allou den einai kai omorfo auto . Prwta prepei na akolo8oun oi dhlwseis olwn twn metavlhtwn pou 8a xrhsimopoihseis sthn sunarthsh sou kai meta o kwdikas. Twra egw etre3a to programma sou sthn VC++ 6.0 kai fainetai na exeis provlhma me thn dhlwsh :

 

char inBuf[5] = {};

 

Den mporw na katalavw ti kaneis edw . Dhmiourgeis ena pinaka 5 stoixeiwn kai afto pou epakoloyuei tou = den nomizw na einai swsto . Egw 8a proteina na ekanes thn dhlwsh auth ws e3hs :

 

char *inBuf = (char *)malloc(5*sizeof(char)) ;

 

Otan egw afairesa auto to {} ekane compile kanonika . Epishs 8a h8ela na kanw mia epishmansh . Eida oti xrhsimopoieis ton inBuff gia na diavaseis ena xarakthra . Giati loipon na mhn xrhsimopoihseis thn sunarthshs getchar() ( h getc() den 8umamai ) pou exeis etoimh ???

Δημοσ.

Emena pantws egine compile kanonika se gcc v3.3.3 (ton exw kanei port se windows vevaia alla de nomizw na exei shmasia). Gia perissoteres plhrofories grapse to mhnyma la8ous.

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...