even if(){}code problems

So, I'm trying to make a code so, it asks the user what day it is, and the user says a response depending on the day (I'm very bad at code don't judge me)
Code:

string day;
cout <<
"please input current day \n(monday, tuesday, wednsday, thursday, friday)";
cin >> day;

if (day == "monday")
{
cout << "I HATE monday";
}
if else
(day == "tuesday")
{
cout << "almost halfway, but tuesday";
}
if else
(day == "wednsday")
{
cout << "Is it wednsday";
}
if else
(day == "thursday")
{
cout << "almost friday but thursday";
}
if else
(day == "friday")
{
cout << "yay friday";
}
if else
{
cout
<<"how did you get that wrong?!??!?!?";
}
Last edited on
if else should be else if.
The correct syntax is:
1
2
3
4
5
6
7
8
9
if (expression_1) {
  // ... code to be executed of expression_1 holds true
} else if (expression_2) {
  // ... code to be executed of expression_2 holds true (and none of the previous checks was true)
} else if (expression_3) {
  // ... code to be executed of expression_3 holds true (and none of the previous checks was true)
} else {
  // ... code to be executed if *none* of the previous checks was true
}


Also, for string comparison you have to use:
1
2
3
if (strcmp(day, "monday") == 0) {
  // ... code to execute if the string pointed to by 'day' equals "monday"
}


You can use the == operator to test for equality of std::string objects, but it does not work with plain C strings!

BTW: If you want case-insensitive string comparison, use stricmp() instead of strcmp().
Last edited on
stricmp() isn't standard. On MS VS it's _stricmp().

Why mention strcmp()? The OP code uses string (first line of code)

@Codeman23. When you post code, please use code tags so that the code is readable!


[code]
code goes here
[/code]

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

int main() {
	std::string day;

	std::cout << "please input current day (monday, tuesday, wednesday, thursday, friday): ";
	std::cin >> day;

	if (day == "monday") {
		std::cout << "I HATE Monday\n";
	} else if (day == "tuesday") {
		std::cout << "almost halfway, but Tuesday\n";
	} else if (day == "wednesday") {
		std::cout << "Is it Wednesday\n";
	} else if (day == "thursday") {
		std::cout << "almost Friday but Thursday\n";
	} else if (day == "friday") {
		std::cout << "yay Friday\n";
	} else {
		std::cout << "how did you get that wrong?!??!?!?\n";
	}
}


Note that if there is only 1 statement for the condition true part or the else part if present (condition false) then the {} are optional.

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

int main() {
	std::string day;

	std::cout << "please input current day (monday, tuesday, wednesday, thursday, friday): ";
	std::cin >> day;

	if (day == "monday")
		std::cout << "I HATE Monday\n";
	else if (day == "tuesday")
		std::cout << "almost halfway, but Tuesday\n";
	else if (day == "wednesday")
		std::cout << "Is it Wednesday\n";
	else if (day == "thursday")
		std::cout << "almost Friday but Thursday\n";
	else if (day == "friday")
		std::cout << "yay Friday\n";
	else
		std::cout << "how did you get that wrong?!??!?!?\n";
}

Why mention strcmp()?

Yeah, didn't realize he was already using std::string.
Last edited on
C++ string comparisons have to be exact in case as well as letter layout. "Friday" is not "friday", is not "firday." "wednsday" is also a possible input error waiting to happen.

Strings are notorious for input failures with users. There are ways and/or library functions to eliminate case issues so no matter what the user enters you have to make only one comparison for a given day.

You might consider using a numeric input instead, with a prompt for the numeric input.
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
#include <iostream>

int main()
{
   int day;

   std::cout << "Please enter today's date:\n"
             << "1) Monday\n"
             << "2) Tuesday\n"
             << "3) Wednesday\n"
             << "4) Thursday\n"
             << "5) Friday\n";
   std::cin >> day;

   if (day == 1)
   {
      std::cout << "I HATE Mondays!\n";
   }
   else if
      (day == 2)
   {
      std::cout << "Almost halfway, but Tuesday.\n";
   }
   else if
      (day == 3)
   {
      std::cout << "Is it Wednesday?\n";
   }
   else if
      (day == 4)
   {
      std::cout << "Almost Friday, this Thursday\n";
   }
   else if
      (day == 5)
   {
      std::cout << "Yay Friday!\n";
   }
   else
   {
      std::cout << "How did you get that wrong?!??!?!?\n";
   }
}

Multiple if/if else statements can be converted to a switch statement:
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
#include <iostream>

int main()
{
   int day;

   std::cout << "Please enter today's date:\n"
             << "1) Monday\n"
             << "2) Tuesday\n"
             << "3) Wednesday\n"
             << "4) Thursday\n"
             << "5) Friday\n";
   std::cin >> day;

   switch (day)
   {
      case 1:
      {
         std::cout << "I HATE Mondays!\n";
         break;
      }
      case 2:
      {
         std::cout << "Almost halfway, but Tuesday.\n";
         break;
      }
      case 3:
      {
         std::cout << "Is it Wednesday?\n";
         break;
      }
      case 4:
      {
         std::cout << "Almost Friday, this Thursday\n";
         break;
      }
      case 5:
      {
         std::cout << "Yay Friday!\n";
         break;
      }
      default:
      {
         std::cout << "How did you get that wrong?!??!?!?\n";
      }
   }
}
Please enter today's date:
1) Monday
2) Tuesday
3) Wednesday
4) Thursday
5) Friday
3
Is it Wednesday?
Codeman23 wrote:
I'm very bad at code

There is a free online tutorial you could spend some time reading to help improve your coding skills, Learn C++.

https://www.learncpp.com/
Topic archived. No new replies allowed.