Else Statement Runs Incorrectly

Hey everyone sorry I forgot information I was so darn upset trying to figure this out.

I need to show two responses per data set at most.
For example:

Enter input 1: 35
Enter input 2: class
cold
majority

VERSUS

Enter input 1: 35
Enter input 2: class
cold
majority
equal
cereal
leave

My codes are showing multiple responses of course and my else statement is running on some inputs as well for example

Enter input 1: 12
Enter input 2: run
equal
cereal
leave

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
#include <iostream>
#include <string>
using namespace std;

int main() {
   int input1 = 0;
   string input2 = " ";
   
   
   cout << "Enter input 1: ";
   cin >> input1;
   cout << input1 << endl;
   
   cout << "Enter input 2: ";
   cin >> input2;
   cout << input2 << endl;
   
   if ((input1 > 27) || (input2 == "classroom")) {
      cout << "cold" << endl;
   }
   if ((input1 <= 69) && (input2 < "grab")) {
      cout << "majority" << endl;
   }
   if ((input1 == 44) && (input2 != "disaster")) {
      cout << "greet" << endl;
   }
   if ((input1 > 15) || (input2.at(0) == 'r')) {
      cout << "equal" << endl;
   }
   if (((input1 % 7) == 0) || (input2 >= "labor")) {
      cout << "cereal" << endl;
   }
   if ((input1 == 295) && (input2 == "origin")) {
      cout << "bang" << endl;
   }
   else {
      cout << "leave" << endl;
   }
   
   return 0;
}
Last edited on
What is the code suppose to do?
I'm running my code and for some reason when I run with input1 as 15 and input2 as Gary I'm getting the correct If statements as well as the Else statement.

If you are getting the "correct" If statements and the else. What do you need help with?
you are comparing a string like > and <, though they are not numbers so you can't really do that. you can do that to the integer but not the strings. you can only use == or !=.
closed account (LA48b7Xj)
At a glance, I think you need to use else if statements

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
   if ((input1 > 27) || (input2 == "classroom")) {
      cout << "cold" << endl;
   }
   else if ((input1 <= 69) && (input2 < "grab")) {
      cout << "majority" << endl;
   }
   else if ((input1 == 44) && (input2 != "disaster")) {
      cout << "greet" << endl;
   }
   else if ((input1 > 15) || (input2.at(0) == 'r')) {
      cout << "equal" << endl;
   }
   else if (((input1 % 7) == 0) || (input2 >= "labor")) {
      cout << "cereal" << endl;
   }
   else if ((input1 == 295) && (input2 == "origin")) {
      cout << "bang" << endl;
   }
   else {
      cout << "leave" << endl;
   }
but you cannot compare a string like < right?

Gary is < Barry?
you are comparing a string like > and <, though they are not numbers so you can't really do that. you can do that to the integer but not the strings. you can only use == or !=.

No the std::string class has overloaded all of the comparison operators, even the greater than and less than operators.




how would use < or >= to compare then? it is logically not possible because its not numbers...
i dont get the process of how the system does that.
Relational operators perform comparisons on string objects in a fashion similar to the way the strcmp function compares C-strings. One by one, each character in the first operand is compared with the character in the corresponding position in the second operand. If all the characters in both strings match, the two strings are equal. Other relationships can be determined if two characters in corresponding positions do not match. The first operand is less than the second operand if the mismatched character in the first operand is less than its counterpart in the second operand. Likewise, the first operand is greater than the second operand if the mismatched character in the first operand is greater than its counterpart in the second operand.

For example, assume a program has the following definitions:
1
2
string name1 = "Mary";
string name2 = "Mark";

The value in name1 , “Mary,” is greater than the value in name2 , “Mark.” This is because the
“y” in “Mary” has a greater ASCII value than the “k” in “Mark.”
lol i underestimated technology i guess... thanks
Hey everyone sorry I forgot information I was so darn upset trying to figure this out.

I need to show two responses per data set at most.
For example:

Enter input 1: 35
Enter input 2: class
cold
majority

VERSUS

Enter input 1: 35
Enter input 2: class
cold
majority
equal
cereal
leave

My codes are showing multiple responses of course and my else statement is running on some inputs as well for example

Enter input 1: 12
Enter input 2: run
equal
cereal
leave
Last edited on
closed account (49iURXSz)
Let me see if I have this right. You have a data set corresponding to the list of the following vocabulary words and numbers in this order:

cold
majority
equal
cereal
bang
leave

Now, each vocabulary word corresponds to a particular set of conditions that have to be met. In other words, a position you would like to start at in the vocabulary list. Then, you want to print out that vocabulary word and the one after it?
Last edited on
Topic archived. No new replies allowed.