Shed some light on this please.

Okay i have to check a string for some things, heres the rules

------
If the first letter is a consonant, move it to the end of the string and add “ay” to the end.
If the first letter is a vowel, add “way” to the end.
------

So I have this function, but it wont go to the else statement, it will only assume that the first char is a vowel.
string FnLetterCheck(string fn)
{
	if (fn[0] == 'a' || 'e' || 'i' || 'o' || 'u' || 'A' || 'E' || 'I' || 'O' || 'U')
	{
		fn = fn + "way";
		return fn;
	}
	else
	{
		fn[fn.length() + 1] = fn[0];
	fn = fn + "ay";
	return fn;
	}
}


so my question is why?


EDIT:


okay i made it so there was only 1 = sign

if (fn[0] = 'a' || 'e' ......

and it replaces the first letter with a smiley face... and doesnt add it to the end of the string
Last edited on
1
2
3
4
5
6
7
8
(fn[0]=='a') or 'e'  //how it evaluates because of precedence rules
fn[0] == ('a' or 'e') //this would be wrong too
('a' or 'e') //evaluates to true

fn[0] == 'a' and 'e' //¿does this make sense to you?


fn[fn.length() + 1] //accessing out of bounds is an error 

i see, so how can i add the letter to the end of the string without causing problems.

i guess i would have to shift them with a for loop?

also it wont even go to the else statement, i still have no clue why.
You're pretty close, but you have misunderstood how multiple conditions work. You need to specify every condition fully.

e.g
1
2
3
4
5
unsigned x = 1;
if (x == 1 || x ==2) { // Valid
}

if (x == 1 || 2) // Not valid C++ 


Loop up the C++ methods for tolower() and substr() to understand how I have achieved it below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>

using namespace std;

string FnLetterCheck(string fn) {

	string return_string = "";

	const char letter = tolower(fn[0]);
	if (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u')
		return_string = fn.substr(1, fn.length() - 1) + fn.substr(0, 1) + "ay";
	else
		return_string = fn + "way";

	return return_string;
}

int main() {

	cout << FnLetterCheck("alphabet") << endl;
	cout << FnLetterCheck("constant") << endl;

}
Last edited on
> if (x == 1 || 2) // Not valid C++
it is valid.
Thanks guys, i got it, its a long code for what it is, i took some long-cuts.

//Tyler Roznos CS-155-02
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;

string makeFnLower(string fn)
{
	string temp(fn);
	for (int i = 0; i < fn.length(); i++)
	{
			temp[i] = tolower(fn[i]);
	}
		return temp;
}


string makeLnLower(string ln)
{
	string temp(ln);
	for (int i = 0; i < ln.length(); i++)
	{
			temp[i] = tolower(ln[i]);
	}
		return temp;
}

string FnLetterCheck(string fn)
{
	int length = fn.length();
	if (fn[0] == 'a' || fn[0] == 'e' || fn[0] == 'i' || fn[0] == 'o' || fn[0] == 'u')
	{
		fn = fn + "way";
		fn[0]=toupper(fn[0]);
		return fn;
	}
	else
	{
	fn = fn + fn[0] + "ay";
	fn[0] = ' ';
	fn[1]=toupper(fn[1]);
	return fn;
	}
}

string LnLetterCheck(string ln)
{
	int length = ln.length();
	if (ln[0] == 'a' || ln[0] == 'e' || ln[0] == 'i' || ln[0] == 'o' || ln[0] == 'u')
	{
		ln = ln + "way";
		ln[0]=toupper(ln[0]);
		return ln;
	}
	else
	{
	ln = ln + ln[0] + "ay";
	ln[0] = ' ';
	ln[1]=toupper(ln[1]);
	return ln;
	}
}

int main ()
{
	string fn, ln, phrase;
	cout << "Please enter a first and last name" << endl;
	cin >> fn >> ln;
	fn = makeFnLower(fn);
	ln = makeLnLower(ln);
	fn = FnLetterCheck(fn);
	ln = LnLetterCheck(ln);
	cout << endl << "Your Pig Latin name is: " << fn << " " << ln << endl;
	cout << endl;
	return 0;
}
@ne555 The syntax is valid, but most compilers should error. Functionally that is not valid.
¿why should a compiler generate an error with valid code? A warning at most.
Functionally, `if (x == 1 || 2)' is a tautology.
Because if (x == 1 || x == 2) is not the same as if (x == 1 || 2). You've hard coded a value in there what will always be true, therefore negating the condition
Last edited on
Do you think while(true) should also result in an error? for ( ; ; ) ?

Do you think such errors/warnings might cause problems in template usage? Generated code?
while(true) is a single condition, so it's ok to use because there is no possible confusion.

if (x == 1 || 2) will always result in true because the if statement is incorrectly written to be logically correct. Regardless of x's value it will always equate to true. In this scenario it's not as bad as it could be because the condition is before the hard-coded true value. Looking at the OPs code he had coded his IF statement incorrectly for the logic he wanted to achieve.

e.g
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
#include <iostream>

using namespace std;

bool doA() {
	cout << "A" << endl;
	return true;
}

bool doB() {
	cout << "B" << endl;
	return false;
}

int main() {

	cout << "&& Operator" << endl;
	if (doB() && doA());

	cout << "|| Operator" << endl;
	if (doA() || doB());

	cout << "|| Operator" << endl;
	if (doB() || doA());

	return 0;
}


Outputs:

&& Operator
B
|| Operator
A
|| Operator
B
A


Notice how the change of flow is determined by the resulting condition.
Last edited on
while(true) is a single condition, so it's ok to use because there is no possible confusion.

if (x == 1 || 2) will always result in true because the if statement is incorrectly written to be logically correct.


if ( x==1 || 2 ) is also a single condition. Ask any optimizer. The compiler is not supposed to verify logic.

What if the condition isn't connected with an or? What if it's connected with an &&? What if one of the conditions is only included for it's side effect? What if one of the conditions is always true now, but may not be in the future?

Yes, I get how the logical operators work. No, I don't see how a warning (and especially not an error) is called for (unless it's an optional one that is preferably not enabled by default.) Certainly the standard doesn't call for a diagnostic.
if ( x==1 || 2 ) is also a single condition.

This is 2 conditions. If the first condition is true, the second condition is never checked. The sum result of those 2 conditions is 1 condition, but that does not mean that both conditions will be executed. I'd recommend you re-visit how the || and && operators behave in conditional statements.

I also recommend you re-read my original post where I say that the statement is not valid C++. Anyone should be able to realise this was in response to the OP where he had coded the if statement conditions in a way that was not valid for the logic he was trying to portray.


Topic archived. No new replies allowed.