Code compiles, but doesn't work.

Here is where I am at. This code compiles and works fine except for two issues. First -1 does not terminate the program. Second, if you enter 7 characters without the 4th being a B, G, R, or W, it does not tell you Invalid Item Description.

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()
 {
 
string item = "";
 
cout << "Enter your 7-character item description (-1 to quit): ";
 getline(cin, item);
 
if (item == "-1") { 
} else { 
cout << "Enter your 7-character item description (-1 to quit): ";
 getline(cin, item);
 }
 while (item.length() != 7)
 {
 cout << "Invalid item description. Please enter a SEVEN-character item description: ";
 cin >> item;
 }
 
if (item.length() == 7)
 {
 if('B' == toupper(item[3])) 
cout << "Your item's color is blue" << endl;
 else if ('G' == toupper(item[3])) 
cout << "Your item's color is green" << endl;
 else if ('R' == toupper(item[3]))
 cout << "Your item's color is red" << endl;
 else if ('W' == toupper(item[3]))
 cout << "Your item's color is white" << endl;
 }
 else if (item.length() != 7)
 cout<< "Invalid item description - no matching color."; 

system("pause");
 return 0;
 }
1
2
3
4
5
if (item == "-1") { 
} else { 
cout << "Enter your 7-character item description (-1 to quit): ";
 getline(cin, item);
 }


In the event that item == "-1", the statement executed is between the braces I have made bold. There is nothing there. Why would the programme quit?

Second, if you enter 7 characters ... it does not tell you Invalid Item Description.


Your invalid item output will only happen when you did not enter 7 characters:
else if (item.length() != 7)
Last edited on
OK, so what do I do to fix the problems? I'm lost on these two. Been at this problem for hours.
Any help?
You can make the main function return (and thus your programme end) with the statement
return 0;
Put that between the braces I marked in bold, so that it is executed when item == "-1"


You want it to say "invalid item" if none of the four colours come up? Simply add a final else statement to the set of if else statements checking for the right colour.

1
2
3
4
5
6
7
8
9
10
11
12
13
if (item.length() == 7)
 {
 if('B' == toupper(item[3])) 
cout << "Your item's color is blue" << endl;
 else if ('G' == toupper(item[3])) 
cout << "Your item's color is green" << endl;
 else if ('R' == toupper(item[3]))
 cout << "Your item's color is red" << endl;
 else if ('W' == toupper(item[3]))
 cout << "Your item's color is white" << endl;
 else
   {cout<< "Invalid item description - no matching color.";}
 }


Currently, your code asks the user to enter his item, and then immediately asks again at the start of the while loop. That seems rather inelegant; does

1
2
3
4
else { 
cout << "Enter your 7-character item description (-1 to quit): ";
 getline(cin, item);
 }


need to be there? Note that the second time you ask for the item, entering "-1" does not make it quit, even though it says it will.
Last edited on
No, you're right -- it doesn't need to be there.

Adding the else statement fixed the "no matching color" problem. But I am still stuck on getting the program to quit when someone enters a -1. Here is the revised code:

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
#include <iostream>
#include <string>
 
using namespace std;
 
int main()
 {
 
string item = "";
 
cout << "Enter your 7-character item description (-1 to quit): ";
 getline(cin, item);
 
if (item == "-1") { 
} 
 while (item.length() != 7)
 {
 cout << "Invalid item description. Please enter a SEVEN-character item description: ";
 cin >> item;
 }
 
if (item.length() == 7)
 {
 if('B' == toupper(item[3])) 
cout << "Your item's color is blue" << endl;
 else if ('G' == toupper(item[3])) 
cout << "Your item's color is green" << endl;
 else if ('R' == toupper(item[3]))
 cout << "Your item's color is red" << endl;
 else if ('W' == toupper(item[3]))
 cout << "Your item's color is white" << endl;
else
   {cout<< "Invalid item description - no matching color." << endl;
   }
 }

system("pause");
 return 0;
 }
1
2
if (item == "-1") { 
} 



When item == "-1" the code between the braces gets executed.

You have not put any code between the braces, so nothing happens.

If you wanted it to print out "beans on toast" when item == "-1", you could have

1
2
3
if (item == "-1") { 
cout << "Beans on toast";
} 
So what is the code that goes between the braces to make the program quit? I've been at this for four hours now and I am tired, unable to figure it out, lost.
The same way you make it quit at the end - return 0; :)
Thanks! That was one of those forest through the trees things. I am gonna have nightmares about C++ tonight.
There's not a week goes by that I don't open some C++ for maintenance and have a panic attack. Once they had to coax me back out from under my desk with a doughnut :)
LMAO^
Topic archived. No new replies allowed.