trying to get these if statements to work T^T

Hey Everyone,

I can't seem to figure out how to get these if statements to work properly. I'm at the point where I just want to see how to do this properly so I can learn from it. If anyone is able to figure it out, THANK YOU.

The area that wont work is the MAX portion of it. Trying to figure out which words in the string have the most chars. Thanks again!

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

bool isMax(size_t w1, size_t w2) {
	if(w1 > w2)
		return true;
	else if(w1 == w2)
		return true;
	else
		return false;
}


int main(){
	string s;
	vector<string> word;
	cout << "Enter a string: ";
	getline(cin, s);
	size_t found;
	size_t found1;
	found = s.find_first_not_of(" ");
	found1 = s.find_first_of(" ");
	while(found1 != string::npos){
		word.push_back(s.substr(found,found1-found));
		found = found1 + 1;
		found1 = s.find_first_of(" ", found1 + 1);
	}
	found1 = string::npos;
	int count = 0;
	word.push_back(s.substr(found, found1));
	string sWord;
	cout << "What word would you like to search for?: ";
	cin >> sWord;
	int max = 0;;
	for (unsigned int i = 0; i < word.size(); ++i){
		cout << word[i] << endl;
		if(word[i] == sWord)
			++count;
	}
	int i = 0;
	if (isMax(word[i].length(), word[i+1].length()) ? max = i : max = i + 1)
	while(i < word.size()){
		if (isMax(word[max].length(), word[i].length()) ? max = max : max = i)
			++i;
	}
	cout << "The string with the most characters is " << word[max]<< endl;
	cout << "\nThere are " << count << " instances of the word " << sWord << endl;

	return 0;
}

Last edited on
Okay so it only works in some instances...
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

bool isMax(size_t w1, size_t w2) {
	if(w1 > w2)
		return true;
	else if(w1 == w2)
		return true;
	else
		return false;
}


int main(){
	string s;
	vector<string> word;
	cout << "Enter a string: ";
	getline(cin, s);
	size_t found;
	size_t found1;
	found = s.find_first_not_of(" ");
	found1 = s.find_first_of(" ");
	while(found1 != string::npos){
		word.push_back(s.substr(found,found1-found));
		found = found1 + 1;
		found1 = s.find_first_of(" ", found1 + 1);
	}
	found1 = string::npos;
	int count = 0;
	word.push_back(s.substr(found, found1));
	string sWord;
	cout << "What word would you like to search for?: ";
	cin >> sWord;
	int max = 0;;
	for (unsigned int i = 0; i < word.size(); ++i){
		if(word[i] == sWord)
			++count;
	}
	int i = 0;
	if (isMax(word[i].length(), word[i+1].length()) ? max = i : max = i + 1)
	i = 0;
	while(i < word.size()){
		if (isMax(word[max].length(), word[i].length())){
			 max;
			++i;
		}
		else
			max = i;
		++i;
	}
	cout << "The string with the most characters is " << word[max]<< endl;
	cout << "\nThere are " << count << " instances of the word " << sWord << endl;

	return 0;
}
Last edited on
closed account (o3hC5Di1)
Hi there,

You are mixing if statements with conditional operators:

1
2
if (isMax(word[max].length(), word[i].length()) ? max = max : max = i)
			++i;


What you are trying to do is this:

1
2
3
4
5
if ( !isMax(word[max].length(), word[i].length() ) //if isMax returns false
{
    max = i;
    i++;
}


As to your mixing, here's an illustration of how the two compare:

1
2
3
4
5
6
7
8
9
10
11
if (somechar == 'a')
{
    //somechar is 'a'
}
else 
{
    //somechar is not 'a'
}

// the above is the same as:
(somechar == 'a')  ?  /*somechar is 'a'*/   :   /*somechar is not 'a'*/  ;


They both do the same in this case, but you can't mix them up.

Hope that helps.
All the best,
NwN
Your suggestion solved my problem 100%. I guess this means I will have to do some reading on the differences between if else statements and conditional operators.

Is it because "else" is not equivalent to "false" in a boolean statement that is checking for truth?

Thanks a million NwN!
closed account (o3hC5Di1)
They are different things performing the same task to a certain extent.
The conditional operator was created because common statements like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
int number;
bool number_even = false;
std::string statement;

if (name_empty == false)
{
    statement = "number is odd";
}

else
{
    statement = "number is even";
}


This can be written with the help of the conditional operator in one line, making it much shorter:

1
2
3
4
5
int number;
bool number_even = false;
std::string statement;

statement = (number_even == false)  ?  "number is odd"  :  "number is even";


As a general rule of thumb., only use the conditional operator to do these kinds of variable initialisations that depend on an expression (like number_even = false). For executing other code, especially multiple statements, always use if/else.

The conditional operator is a good tool - when it's used correctly, when used incorrectly it can really make a mess of your code. Never nest conditional operators or you'll end up in a syntax-jungle.

So conditional operators ( ? : ) and the if/else control structure can be used to do the same thing in some cases, but never mixed. When in doubt, use if/else.

All the best,
NwN

Okay I see, so the conditional operator was created in order to save time and screen real estate when writing if-else's that have only one statement each. That makes sense and again, I really appreciate your detailed response and suggestions. I have incorporated you suggestions in my code, and it has really cleaned things up!

Thanks NwN!
Topic archived. No new replies allowed.