Single Nouns to Plural Nouns Program!

Oct 5, 2011 at 5:33am
I have a problem here, I need to make a program that asks the user to input a noun and the program will make it plural (irregular nouns are not counted)

i got through the basic appending of plural suffixes (s, es) my problem now is how do i do the suffixes like "ies" (for words ending with y) and "ves" (for words ending in f), i created the code but when i type the word "baby" the result that comes out is "babyies" the i don't know how to remove the last letter, urgent help please, thanks in advance :)

CODE:


#include<iostream>
#include<conio.h>
#include<string.h>
#include<stdio.h>
using namespace std;

int main()
{
int length;

char str1[20];


cout<<"Input a word: ";

gets(str1);

length = strlen(str1);

if (str1[length-1]=='x'||'h'||'s')

strcat(str1,"es");

else if(str1[length-1]=='f')

strcat(str1, "ves");

else if(str1[length-1]=='y')

if(str1[length-2]=='a'||'e'||'i'||'o'||'u')

strcat(str1, "s");

else

strcat(str1, "ies");

else

strcat(str1, "s");

cout<<"\nIt's plural form is: "<<str1;



_getch();
return 0;
}




Oct 5, 2011 at 6:19am
bump
need urgent help :(
Oct 5, 2011 at 6:31am
Firstly put code tags round your code.
2) you may be better off using switch() and case:
3) if(str1[length-2]=='a'||'e'||'i'||'o'||'u') //this is wrong
Oct 5, 2011 at 8:39am
sorry bout the code tags,
how can i remove the last letter by means of switch case? may i see the proper code for that?
Oct 5, 2011 at 8:54am
A 'switch' checks multiple options for a single variable (int or char). In this case, you're checking str1[length-1], thus:
1
2
3
switch(str1[length-1]) {
// Code comes here
}

Now, a switch checks that variable for several pre-defined values, called 'cases'. In this situation, each possible letter is a 'case'. For example:
case 'x': strcat(str1,"es");
However, this means every letter has a separate case, meaning 26 cases! However, you can combine cases. The thing is: a switch() case doesn't stop executing until it reaches a "break;" statement. That means it will execute the code from the next case if there is no break!
As a result, you can do:
1
2
3
4
5
case 'x':
case 'h':
case 's': 
  cat(str1,"es"); 
  break;

Now all three cases are combined, saving you the effort of copypasting code (and making your code that much sexier).


Just a few pointers, though:
a) You're not checking for out-of-bounds problems. What if I type in a word that's 19 characters long and you want to glue 'es' behind it?
b) In the case of 'f': you're glueing "ves" behind it, but you're not deleting the 'f'! I'm not sure how to fix it (I don't work with C strings, sorry), but you should definitely look that up.
c) Your if statements were pretty faulty... You should definitely practice those. You can't do "x == 'y' || 'z'". You need to do "x == 'y' || x == 'z'". Else, you'll always get "true".
Oct 5, 2011 at 10:24am
oh sorry about the 'y'||'z' actually i just changed it before posting it here my original code there is

if(str1[length-2]=='a'||str1[length-2]=='e'||str1[length-2]=='i'||str1[length-2]=='o'||str1[length-2]=='u')

oh well im actually done with the program can someone check if there are stil faults/ mistakes?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
	int length;
	char str1[500];
	char str2[500];
	cout<<"Input a word: ";
	gets(str1);
	length = strlen(str1);
	if (str1[length-1]=='x'||str1[length-1]=='h'||str1[length-1]=='s'){
        strcpy(str2, str1);      
	strcat(str2,"es");
}
	else if(str1[length-1]=='f'){
        strncpy(str2, str1, (length-1)); 
        strcat(str2, "ves");
}
	else if(str1[length-1]=='y'){
		if(str1[length-2]=='a'||str1[length-2]=='e'||str1[length-2]=='i'||str1[length-2]=='o'||str1[length-2]=='u'){
   		strcpy(str2, str1);
		strcat(str2, "s");
}
		else{	
		strncpy(str2, str1, (length-1));	
    		strcat(str2, "ies");
}
}
	else{    
	strcpy(str2, str1);
	strcat(str2, "s");
}
		cout<<"\nIt's plural form is: "<<str2;

_getch();
return 0;
}
Oct 5, 2011 at 1:19pm
Compiled and ran fine on MS C++ 2008 Express. The only problem I had was the compiler wanted me to change strcpy to strcpy_s, strcat to strcat_s and gets to gets_s. I then changed the spelling of 'It's' to 'Its', since 'It's' means 'It is', which doesn't make much sense. Good program.
Oct 6, 2011 at 10:30am
okay thanks to you all! im still trying to expand the program for other siuations like goose - geese etc , :)
Oct 6, 2011 at 3:34pm
The only problem I had was the compiler wanted me to change strcpy to strcpy_s, strcat to strcat_s and gets to gets_s
¿? ¿As a compiler error, or just a suggestion?

Try to make it inplace (without str2).

man gets wrote:
Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security.
Oct 6, 2011 at 3:51pm
@ne555

No, it wasn't a compiler error, for changing strcpy to strcpy_s, etc., but a suggestion to prevent possible loss. I took it upon myself to make the suggested changes. It did run before that as well.
Last edited on Oct 6, 2011 at 7:11pm
Oct 12, 2011 at 2:48pm
this is weird, when i use the functions WITHOUT the "_s" the output has some unknown characters but with those, it gets fixed,
BTW im using Dev C++ (which doesn't accept the "_s")

hey last question, my professor asked me to add these two irregular nouns
MAN - MEN
OX - OXEN

how could i do it? it's my final instructions so please help me :)
Oct 16, 2011 at 7:14am
nvm i already did it, i'll mark this as solved :)
Topic archived. No new replies allowed.