Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

SkippyThorson

macrumors 68000
Original poster
Jul 22, 2007
1,706
1,011
Utica, NY
Well hello again. Since the last time we met, I have gone on to learn fully about arrays and strings, and have begun to learn the basic string functions. Our problem this week has stumped me again, so I'm back to try and regain control on the subject.

The program I have here is supposed to take a singular noun, and output its plural version. Examples; toy to toys, diary to diaries, and blob to blobs.

The issue I'm having is recognizing the if...else statements in order. I had two different versions going, with an if...else within a first if statement to find y, and then determine the letter before it... However, that only either recognized the first if, or the concluding else, not the elseif between.

This time, it seems it is only recognizing the first if statement only, and ever. This one has me stumped. There are a couple other rules I have to throw in, but I'm not including them because I want to do them on my own, plus it's less to read for you all. (It's a pride / moral thing - I tried to stay away from here as long as possible, and rely on me and myself. Did good until now. :p)

The program I have is as follows, and is in standard C.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//Global variables.
int i = 0;

void plural(char *word); 

int main()
{	
	//Variables local to main.
	char word[20];
	
	printf("Welcome. \n");
	printf("Please enter a noun: ");
	scanf("%s", word);
	
	//Call to the function plural.
	plural(word);
	
	printf("%s\n", word);
	return 0;
}

void plural(char *word)
{
	int i = strlen(word);
	
	//If there is a vowel before y, and if y is the last letter.
	if (word[i - 2] == 'a' || 'e' || 'i' || 'o' || 'u' && word[i - 1] == 'y')
	{
		strcat(word, "s");
	}
	
	//If y is the last letter otherwise.
	else if (word[i - 1] == 'y')
	{
		--i;
		strcat(word, "ies");
	}
	
	//For all other possibilities. It should be an "s",
        //but changed it to not confuse two "s" outputs.
	else
	{
		strcat(word, "xxx");
	}
}
 
C isn't my strong suit at all but I don't think you can use || in the fashion you are meaning...

Code:
if (word[i - 2] == 'a' || 'e' || 'i' || 'o' || 'u' && word[i - 1] == 'y')

I think it's actually saying:

if...
((word[i -2] == 'a') || ('e') || ('i') || ('o') || ('u')) &&
( word[i-1] == 'y')
then...

so 'e' is resolving to true since it's technically not 0 and your && statement resolves to true because the word ends in 'y'.

There is another issue in the code in your else if block but I'm sure you'll figure that out once you can get in there.
 
That's right. Each additional "clause" in an if statement has to be self-sufficient.

This doesn't work:

Code:
if (a == 1 || 2 )

Because the compiler will treat it as two separate cases:

a == 1
(which may or may not evaluate to true)

OR

2
(which, being a non-zero number, always evaluates to true)

So the end result of that if statement is that it will always return true.

What you need to do instead is:

Code:
if ( (a == 1) || (a == 2) )

Which then makes each part of the statement able to stand on its own.
 
Thankyouthankyou! I'll add it to the database in my mind. Should it stick.

Panda, I found what you were talking about right off the bat. I left the 'decrement i' in there since I was working primarily on the one above it. Totally blew the bottom part off.

Replaced with:
Code:
word[(strlen(word) - 1)] = '\0';
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.