Text manipulation

name game song

Page 1 of 1

9 Replies - 715 Views - Last Post: 22 November 2008 - 11:24 AM Rate Topic: -----

#1 investyoung  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-October 08

Text manipulation

Post icon  Posted 20 November 2008 - 10:51 AM

Ok everyone, I need some big help. I have to create a word manipulation program that simulates the name game song by Sherry Ellis: For example If your name is Shirley, then the program will output "Shirley, Shirley bo Birley Bonana fanna fo Firley Fee fy mo Mirley, Shirley!" So it basically need to replace the first letters of the name up until the vowel and replace it with the appropriate letters of the song.

I am in my very new to all of this and have started my program but I am having a lot of problems and don't even know if this is close to the right start. Any help would be GREAT!
#include <stdio.h>
#include <string.h>


int main(){
char name[50], root[50], indexchar[50]; EOF;
int i, choice;
int NameLength, FirstVowel;

printf("Want to play a game?\n\n");
printf("1. Play name game\n2. To exit");
scanf("%d", choice);

while(choice == '1'){
printf("\n\nEnter a name");
gets(name);

while(name!= EOF){
NameLength = name.length();

for(i=0; i<NameLength; i++) {
indexchar= name.substr(i,1);
if(indexchar=='a'||indexchar=='e'||indexchar=='i'||indexchar=='o'||indexchar=='u'){
FirstVowel=i;
break;
}/*end if*/
}/*end for*/

root=name.substr(FirstVowel,NameLength-FirstVowel);
printf("\n%s,%s, Bo B%s", name, name, root);
printf("Banana, Fana, Fo F%s", root);
printf("Fee, Fy mo M%s", root);
printf(" %s!", name);
}/*end outer while*/


return 0;

}



Is This A Good Question/Topic? 0
  • +

Replies To: Text manipulation

#2 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: Text manipulation

Posted 20 November 2008 - 11:21 AM

The first thing you need to do is decide between C & C++.

char i[50]; C string, 49 charaters, Null terminated. Write your own substr function.

string i; C++ string class, already has substr.
Was This Post Helpful? 0
  • +
  • -

#3 investyoung  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-October 08

Re: Text manipulation

Posted 20 November 2008 - 11:31 AM

View Postbrds, on 20 Nov, 2008 - 10:21 AM, said:

The first thing you need to do is decide between C & C++.

char i[50]; C string, 49 charaters, Null terminated. Write your own substr function.

string i; C++ string class, already has substr.



That is the part of the problem I am having. I have been introduced to both and keep crossing them. For this particular problem I need to be using "C".
Was This Post Helpful? 0
  • +
  • -

#4 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: Text manipulation

Posted 20 November 2008 - 12:44 PM

View Postinvestyoung, on 20 Nov, 2008 - 12:31 PM, said:

For this particular problem I need to be using "C".


In that case:
Ditch that errant EOF;, in your variable declaration.
gets(); is unsafe for use (no bounds checking). A scanf("%49s", name) is better, or fgets(name, 50, stdin) is best.
Calling strlen(name) will get you the length in characters of name.
To aproximate first use of substr in your code, name is just an array, indexed 0 to 49. Insted of indexchar= name.substr(i,1);, use indexchar= name[i]; or just directly compare name[i] == 'a'. Second use, strncpy(root, &name[FirstVowel], NameLength-FirstVowel); it is also unsafe, but short of writeing your own, or finding someone elses it's all we got. You should do the appro bounds checks before use.
Was This Post Helpful? 0
  • +
  • -

#5 investyoung  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-October 08

Re: Text manipulation

Posted 20 November 2008 - 01:08 PM

here is my new and updated code(THANK YOU BRDS), I am still getting an error in the 'root' line....the error is Error 4 error C2440: '=' : cannot convert from 'char *' to 'char'
your insight would be helpful.....thank you as always

#include <stdio.h>
#include <string.h>


int main(){
char name[50], root[50], indexchar[50]; 
int i, choice;
int NameLength, FirstVowel;

printf("Want to play a game?\n\n");
printf("1. Play name game\n2. To exit");
scanf("%d", choice);

while(choice == '1'){
printf("\n\nEnter a name");
scanf("%49s", name);

while(1){
NameLength = strlen(name);

for(i=0; i<NameLength; i++) {

if(name[i]=='a'||name[i]=='e'||name[i]=='i'||name[i]=='o'||name[i]=='u'){
FirstVowel=i;
break;
}/*end if*/
}/*end for*/

root=strncpy(root, &name[FirstVowel], NameLength-FirstVowel);
printf("\n%s,%s, Bo B%s", name, name, root);
printf("Banana, Fana, Fo F%s", root);
printf("Fee, Fy mo M%s", root);
printf(" %s!", name);
}
}/*end outer while*/


return 0;

}


Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Text manipulation

Posted 20 November 2008 - 01:51 PM

This code is wrong:
scanf("%d", choice);
while(choice == '1'){

You need to pass the address of choice (an integer) to scanf, and when comparing don't use a character, use the integer.

And how are you ever going to leave the while(1) loop?

Get rid of root= in root=strncpy(...
Was This Post Helpful? 0
  • +
  • -

#7 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: Text manipulation

Posted 20 November 2008 - 02:02 PM

root=strncpy(root, &name[FirstVowel], NameLength-FirstVowel); should be strncpy(root, &name[FirstVowel], NameLength-FirstVowel);. I don't have a compiler handy so I can't check.

@Edit:

JackOfAllTrades has a point, and also once choice == 1 how are you getting out of that while loop.

Don't change the second scanf, name without the [somenumber] is a pointer in that context.

This post has been edited by brds: 20 November 2008 - 02:17 PM

Was This Post Helpful? 0
  • +
  • -

#8 investyoung  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-October 08

Re: Text manipulation

Posted 22 November 2008 - 09:33 AM

Alright below is my code. I have tried a million things to get out of the inner while loop but am indefinately stuck there after I type in "1" as my choice in the outer while. I have tried many options to get out, like have a special condition like while (name != 'done'), but it won't compile....do you guys have any suggestions. Is it a conversion problem or do I need some type of pointer?


#include <stdio.h>
#include <string.h>


int main(){

char name[50], root[50]={0}, indexchar[50]; 
int i, choice= 0;
int NameLength, FirstVowel;
char *rootPointer;

rootPointer= &root[0];

printf("Want to play a game?\n\n");
printf("1. Play name game\n2. To exit\n\n");
scanf("%d", &choice);

while(choice == 1){
	printf("\n\nEnter a name:\n\n");
	scanf("%49s", &name);

	while (1){
		NameLength = strlen(name);

			for(i=0; i<NameLength; i++) {

				if(name[i]=='a'||name[i]=='e'||name[i]=='i'||name[i]=='o'||name[i]=='u'){
				FirstVowel=i;
				break;
				}/*end if*/
			}/*end for*/

rootPointer = strncpy(rootPointer, &name[FirstVowel], NameLength - FirstVowel);

printf("\n%s,%s, Bo B%s  ", name, name, rootPointer);
printf("Banana Fana, Fo F%s  ", rootPointer);
printf("Fee Fy mo M%s  ", rootPointer);
printf(" %s!\n\n", name);
break;

		}/*end inner while*/
	}/*end outer while*/


return 0;

}


Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Text manipulation

Posted 22 November 2008 - 10:01 AM

Have you thought about why you need a while(1) loop at all?

Also, copy the
printf("Want to play a game?\n\n");
printf("1. Play name game\n2. To exit\n\n");
scanf("%d", &choice);
before the end of the outer while.
Was This Post Helpful? 1
  • +
  • -

#10 investyoung  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-October 08

Re: Text manipulation

Posted 22 November 2008 - 11:24 AM

An "ah ha" moment. Thank you all for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1