2 Replies - 449 Views - Last Post: 03 April 2012 - 07:06 PM Rate Topic: -----

#1 kpdunphy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-April 12

Pig latin program. help whats wrong How do i fix?

Posted 03 April 2012 - 06:17 PM

How do i fix this what is wrong with this. It says to few arguments to call to rule2 and in strncat
here is the code

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 120

int RuleApplied(char letters[]);
void rule1(char word[],char newword[]);
void rule2(char word[], char newword[],int length);
void rule3(char word[], char newword[]);
void DisplayIntruc();
int findnonblank (char sentencein[],int i);
int findblank (char sentencein[], int i);

void Firstletter(char letters[], char word[]);
void Secondletter(char letters[], char word[]);

void getWord(char sentence[], char word[],int b, int e);

int main()
{
	int selection;
	int length;
	char trash[1]="\0";
	char sentencein[SIZE];
	char sentenceout[SIZE*3];
	char word[20]="";
	int position=0;
	int nonBlank;
	int blank;
	int rule;
	char newword[20];
	char letters[2];

	FILE *outp;
	outp=fopen("output.txt","w");

	printf("Welcome to PIGLATIN\n");
	printf("enter seletion 1 to quit, any other number to continue\n");
	scanf("%d",&selection);

	while (selection != 1)
	{
		DisplayIntruc();
		scanf("%c",&trash);

		printf("Enter a sentence with less than 80 blah:\n");
		gets(sentencein);
		fprintf(outp, "\nYour sentence: %s\n",sentencein);
		printf("\nYour sentence:%s\n",sentencein);

		strcpy(sentenceout,"");
		
		length =strlen(sentencein);
		printf("length:%d\n", length);

		position=0;

		while(sentencein[position]!='\0')
		{
			nonBlank = findnonblank(sentencein, position);
			blank= findblank(sentencein,nonBlank);

			strcpy(word,"");
			strcpy(newword,"");

			getWord(sentencein,word,nonBlank,blank);

			printf("\nWORD:%s",word);
			Firstletter (letters,word);
			Secondletter (letters,word);
			rule = RuleApplied(letters);

			if(rule==1)
			{
				rule1(word,newword);
			}
			else if(rule==2)
			{
				rule2(word,newword);
			}
			else
			{
				rule3(word,newword);
			}

			printf("\nNEW WORD:%s",newword);

			strcat(sentenceout,newword);
			position = blank;
		}

		printf("\n\nSEntence out:%s\n",sentenceout);
		fprintf(outp, "\n\nSentence out:%s\n",sentenceout);

		printf("\nDo you want to continue? Type 1 if you want to exit.\n");

		scanf("%d",&selection);

		fclose(outp);
	}

	return 0;
}
void DisplayIntruc ()
{
	printf("good choice!!\n");
	printf("You will enter a sentence of 80 chac or less.\n");
	printf("Depending on your words, you will be given a translation\n");
	printf("Lets get started\n");
}

void getWord(char sentence[],char word[], int b, int e)
{
	strncat(word,&sentence[b],(e-B)/>);
}

int findnonblank(char sentencein[], int i)
{
	while(sentencein[i]==' ')
	{
		i++;
	}
	return i;
}

int findblank (char sentencein[],int i)
{
	while (sentencein[i]!=' ')
	{
		if(sentencein[i]=='\0')
		{
			return i;
		}
		i++;
	}
	return i;
}

void Firstletter(char letters[],char word[])
{
	letters[0]= toupper(word[0]);
}

void Secondletter (char letters[],char word[])
{
	letters[1]= toupper(word[1]);
}

int RuleApplied(char letters[])
{
	if(strncmp(letters,"TH",2)==0)
		return 1;
	else if (strncmp(letters,"WH",2)==0)
		return 1;
	else if (strncmp(letters,"SH",2)==0)
		return 1;
	else if (strncmp(letters,"QU",2)==0)
		return 1;
	else if(letters[0] =='A'||letters[0]=='E'||letters[0]=='I'|| letters[0] =='O'|| letters[0] =='U')
	{
		return 2;
	}
	else
	{
		return 3;
	}
}
void rule1(char word[],char newword[])
{
	strcpy(newword,&word[2]);
	strncat(newword,word,2);
	strncat(newword,"AY");
}
void rule2(char word[], char newword[], int length)
{
	strcpy(newword,word);
	strcat(newword,"AY");
}

void rule3 (char word[], char newword[])
{
	strcpy(newword,&word[1]);
	strncat(newword,word,1);
	strcat(newword,"AY");
}


This post has been edited by no2pencil: 03 April 2012 - 06:19 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Pig latin program. help whats wrong How do i fix?

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,421
  • Joined: 25-December 09

Re: Pig latin program. help whats wrong How do i fix?

Posted 03 April 2012 - 06:30 PM

Post your error messages exactly as they appear in your development environment. These messages have important information embedded within them to aid in location and fixing your errors.

After a quick look at your code I noticed you are using gets() this function is considered very dangerous and should never be used. It is one of the few functions to ever be removed from the C standard. Replace this function with something like fgets() which allows you to limit the size of the string that the user can enter. Also for your strncat() problems did you find and read the documentation for this function? Here is a link to one such source for the documentation.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 kpdunphy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-April 12

Re: Pig latin program. help whats wrong How do i fix?

Posted 03 April 2012 - 07:06 PM

View Postjimblumberg, on 03 April 2012 - 06:30 PM, said:

Post your error messages exactly as they appear in your development environment. These messages have important information embedded within them to aid in location and fixing your errors.

After a quick look at your code I noticed you are using gets() this function is considered very dangerous and should never be used. It is one of the few functions to ever be removed from the C standard. Replace this function with something like fgets() which allows you to limit the size of the string that the user can enter. Also for your strncat() problems did you find and read the documentation for this function? Here is a link to one such source for the documentation.


Jim

Thanks for your help Jim, I figured out what was wrong.
I did not add the amount of arguments i had declared on top. Fixed program works
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1