Please help with program - STUCK

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2005
Posts: 14
Reputation: cjones5499 is an unknown quantity at this point 
Solved Threads: 0
cjones5499 cjones5499 is offline Offline
Newbie Poster

Please help with program - STUCK

 
0
  #1
Aug 8th, 2005
I am ahving a problem with the do/while loop. When the YesNo function returns a yes, main() repeats;however, it prints out what is stored in the string from before. How do you clear the string in order to run the program over and over. Also, when the piglatin result is printed, the last 3 letters are placed on the next line, why is this?

Here is the code thus far:

  1. #include<stdio.h>
  2. #include<ctype.h>
  3. #include<string.h>
  4.  
  5.  
  6. char * vowel_rule(char *);
  7. char * two_letter(char *);
  8. char * last_rule(char *);
  9. void rules();
  10. int YesNo();
  11. void instructions();
  12.  
  13. int main()
  14. {
  15. do
  16. {
  17. instructions();
  18. rules(); //call function to read in sentence & print piglatin
  19.  
  20. }while(YesNo() != 0);
  21.  
  22. return(0);
  23. }
  24.  
  25.  
  26.  
  27. void rules()
  28. {
  29. char str[80];
  30. char english[80];
  31. int length,a,b,i;
  32.  
  33. i=0;
  34. a=b=0;
  35.  
  36. printf("Please enter a phrase to be translated to piglatin: ");
  37. fgets(str,79,stdin); //store sentence in str
  38.  
  39. length = strlen(str);
  40.  
  41. printf("\nThis is your translated sentence: ");
  42.  
  43. while(a<=length)
  44. {
  45. english[b]=str[a]; //copy str to english
  46.  
  47. if(english[b]==' '||english[b]=='\0') //check for whitespace
  48. {
  49. english[b]='\0';
  50. b=0;
  51.  
  52. if(english[0]=='A'||english[0]=='E'||english[0]=='I'||english[0]=='O'||english[0]=='U'||english[0]=='a'||english[0]=='e'||english[0]=='i'||english[0]=='o'||english[0]=='u') //check for word beginning with vowel
  53. {
  54. printf(" %s",vowel_rule(english));
  55.  
  56. }
  57.  
  58. else if(english[0]=='T'&&english[1]=='H'||english[0]=='C'&&english[1]=='H'||english[0]=='S'&&english[1]=='H'||english[0]=='P'&&english[1]=='H'||english[0]=='W'&&english[1]=='H'||english[0]=='Q'&&english[1]=='U') //checks for first 2 letters
  59. {
  60. printf(" %s",two_letter(english));
  61. }
  62. else
  63. {
  64. printf(" %s",last_rule(english));
  65. }
  66. }
  67. else
  68. {
  69. b++;
  70. }
  71.  
  72. a++;
  73. }
  74.  
  75. }
  76.  
  77.  
  78.  
  79.  
  80. void instructions()
  81. {
  82. printf("This program will translate an english word or phrase up to 80 characters\n");
  83. printf(" into piglatin. The program will convert your phrase according to the\n");
  84. printf(" following rules:\n\n");
  85. printf(" Rule 1: If the word begins with a vowel, 'AY' will be appneded to the\n");
  86. printf(" end.\n");
  87. printf(" Rule 2: If the word begins with 'TH','SH','CH','WH','PH',or 'QU', the\n");
  88. printf(" first two letters are appended to the end of the word\n");
  89. printf(" followed by 'AY'\n\n");
  90. printf(" Rule 3: If none of the rules apply, the first letter is appended to\n");
  91. printf(" the end of the word, followed by 'AY'.");
  92. printf("\n\nThe program will also check for case sensitivity. The output will match the \n");
  93. printf(" input, whether it is entered in lowercase or uppercase.\n\n\n\n");
  94.  
  95.  
  96.  
  97. }
  98.  
  99.  
  100.  
  101.  
  102. char * vowel_rule(char *str)
  103. {
  104. static char temp[80];
  105. memset(temp,'\0',80);
  106.  
  107. strcpy(temp,str); //copy word starting from second character to temp
  108. temp[strlen(temp)+1]= '\0'; // Null terminate temp
  109. strcat(temp,"AY"); // append "AY" to the string.
  110.  
  111. return temp;
  112. }
  113.  
  114.  
  115.  
  116.  
  117. char * two_letter(char *str)
  118. {
  119. static char temp[80];
  120. memset(temp,'\0',80);
  121. char first,append[]="H";
  122.  
  123. first=str[0]; //copy first char of str into first
  124.  
  125. if(str[1]=='H')
  126. {
  127. strcpy(temp,str+=2); //copy word starting from third character to temp
  128. append[0]=first;
  129. strcat(temp,append);//copy the first char of word to end of temp
  130. temp[strlen(temp)+1]='\0'; // Null terminate temp
  131. strcat(temp,"HAY"); // append "HAY" to the string.
  132. }
  133.  
  134. if(str[1]=='U')
  135. {
  136. strcpy(temp,str+=2); //copy word starting from third character to temp
  137. append[0]=first;
  138. strcat(temp,append);//copy the first char of word to end of temp
  139. temp[strlen(temp)+1]='\0'; // Null terminate temp
  140. strcat(temp,"UAY"); // append "UAY" to the string.
  141. }
  142.  
  143. return temp;
  144. }
  145.  
  146.  
  147.  
  148.  
  149. char * last_rule(char *str)
  150. {
  151. static char temp[80];
  152. memset(temp,'\0',80);
  153.  
  154. strcpy(temp,++str); //copy word starting from second character to temp
  155. temp[strlen(temp)]=*(--str); //copy the first char of word to end of temp
  156. temp[strlen(temp)+1]= '\0'; // Null terminate temp
  157. strcat(temp,"AY"); // append "AY" to the string.
  158.  
  159. return temp;
  160. }
  161.  
  162.  
  163.  
  164. int YesNo()
  165. {
  166. char check;
  167.  
  168. printf("\n\nWould you like to enter another sentence to be translated(Y/N)?: ");
  169. scanf(" %c", &check);
  170.  
  171. if(check=='Y'||check=='y')
  172. {
  173. return (1);
  174. }
  175. else
  176. return (0);
  177.  
  178. }//end of function
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 9
Reputation: cyber_aziz is an unknown quantity at this point 
Solved Threads: 0
cyber_aziz cyber_aziz is offline Offline
Newbie Poster

Re: Please help with program - STUCK

 
0
  #2
Aug 8th, 2005
you can clear a string like this


char ch;
char ch = {' '}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 9
Reputation: cyber_aziz is an unknown quantity at this point 
Solved Threads: 0
cyber_aziz cyber_aziz is offline Offline
Newbie Poster

Re: Please help with program - STUCK

 
0
  #3
Aug 8th, 2005
you can clear a string like this


char ch;
ch[] = {' '};
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Please help with program - STUCK

 
0
  #4
Aug 8th, 2005
Originally Posted by cyber_aziz
you can clear a string like this

char ch;
char ch = {' '}
Here since ch is a single character it cannot be a string.
Originally Posted by cyber_aziz
you can clear a string like this

char ch;
ch[] = {' '};
And this is even worse. You cannot assign to arrays. You need a subscript. And it's still a single character.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 14
Reputation: cjones5499 is an unknown quantity at this point 
Solved Threads: 0
cjones5499 cjones5499 is offline Offline
Newbie Poster

Re: Please help with program - STUCK

 
0
  #5
Aug 8th, 2005
If neither of those will work, what are my options this program to clear the array for the second input?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Please help with program - STUCK

 
0
  #6
Aug 8th, 2005
You could do a simple loop for the length of str that writes blankspace in for each entry in the array.
Or, you could clear the str array after you increment a.
  1. a++;
  2. str[--a] = ' ';
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 14
Reputation: cjones5499 is an unknown quantity at this point 
Solved Threads: 0
cjones5499 cjones5499 is offline Offline
Newbie Poster

Re: Please help with program - STUCK

 
0
  #7
Aug 8th, 2005
I tried using str[--a] and also tried a for loop setting blank spaces, neither worked, when the program is ran the second loop run skips the user input and goes to YesNo again. Also, can anyone tell me why the last 3 chars printed go to the next line?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Please help with program - STUCK

 
0
  #8
Aug 8th, 2005
You need to read the '\n' from the input buffer -- scanf leaves it there.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Please help with program - STUCK

 
0
  #9
Aug 8th, 2005
Why not use memset to clear the str, then? Just write in blanks or whatever for your 80 character entries, as you've done in last_rule?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 14
Reputation: cjones5499 is an unknown quantity at this point 
Solved Threads: 0
cjones5499 cjones5499 is offline Offline
Newbie Poster

Re: Please help with program - STUCK

 
0
  #10
Aug 8th, 2005
I have tried to use memset and the for loop to allow the program to loop, but after the first run it skips the input and displays 'AY' and prompts the YesNo function. Can anyone tell me why this is, I cannot figure it out.

Thanx for the help on the '\n' - I took care of that problem
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC