Help with Output in a String...

My program works correctly if the input entered matches the "product code" and the "color code" protocols provided in the program. But if the user enters the wrong number, the incorrect "Inventory code" is displayed. How can I get my program to "not display" an invalid code? According to my instructor, the only thing that should display if an incorrect code is entered is "Invalid input" and nothing else. The user will have to "Press any key to continue".

I hope I've explained the issue I'm having, and if not, please let me know. Any assistance would be appreciated...

Here's my 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string itemCode = "";
    string colorCode = "";
    string inventCode = "";

    cout << "Enter 1 for chair or 2 for table: ";
    getline(cin, itemCode);

         if (itemCode == "1")
         {
              cout << "C47" << endl;
              itemCode = "C47";
         }

         else if (itemCode == "2")
         {
              cout << "T47" << endl;
              itemCode = "T47";
         }
         
         else
         {
              cout << "Invalid input." << endl;
         }
              cout << "\nEnter 1 for red, 2 for black or 3 for green: ";
              getline(cin, colorCode);

          if (colorCode == "1")
          {
              cout << "41" << endl;
              colorCode = "41";
          }

          else if (colorCode == "2")
          {
              cout << "25" << endl;
              colorCode = "25";
          }

          else if (colorCode == "3")
          {
              cout << "30" << endl;
              colorCode = "30";
          }

          else
          {
              cout << "Invalid input." << endl;
          }

          inventCode = itemCode + colorCode;
          {
              cout << "\nInventory code: " << inventCode << endl;
          }

system("pause");
return 0;
}
You need a way to bail from the program entirely at lines 30 and 55?

simplest way = put a return right there (and a system("pause"); if you MUST).

Another (more involved way) would be to add a bool variable for testing if the last input was good before entering the next value, etc..
Could you please expound on how and where to put a return?
Last edited on
I just meant write return 0; where you want to quit the program.
Like so:
1
2
3
4
5
else
{
     cout << "Invalid input." << endl;
     return 0;// bail from the program right here.
}
Last edited on
I appreciate the suggestion but using "return 0" completely closes the program, and this isn't the result I should be getting.

My output is as follows if the wrong numbers are entered :

Enter 1 for chair or 2 for table: 3
Invalid input.

Enter 1 for red, 2 for black or 3 for green: 4
Invalid input.

Inventory code: 34
Press any key to continue . . .


The program should display the output like this if incorrect data is entered:

Enter 1 for chair or 2 for table: 3
Invalid input.

Enter 1 for red, 2 for black or 3 for green: 4
Invalid input.

Press any key to continue . . .


Would using a "while" loop help? I've tried but failed to get the right results. Any help would be appreciated...
Last edited on
Something like this might help:
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
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string itemCode = "";
    string colorCode = "";
    string inventCode = "";
    int errorcode = 0;

    cout << "Enter 1 for chair or 2 for table: ";
    getline(cin, itemCode);

         if (itemCode == "1")
         {
              cout << "C47" << endl;
              itemCode = "C47";
         }

         else if (itemCode == "2")
         {
              cout << "T47" << endl;
              itemCode = "T47";
         }
         
         else
         {
              cout << "Invalid input." << endl;
              errorcode = 1;       
         }
              cout << "\nEnter 1 for red, 2 for black or 3 for green: ";
              getline(cin, colorCode);

          if (colorCode == "1")
          {
              cout << "41" << endl;
              colorCode = "41";
          }

          else if (colorCode == "2")
          {
              cout << "25" << endl;
              colorCode = "25";
          }

          else if (colorCode == "3")
          {
              cout << "30" << endl;
              colorCode = "30";
          }

          else
          {
              cout << "Invalid input." << endl;
              errorcode = 1;
          }

          inventCode = itemCode + colorCode;
          if(errorcode = 0)
          {
              cout << "\nInventory code: " << inventCode << endl;
          }

system("pause");
return 0;
}
That works, but the problem now is that the program doesn't display the correct output when the correct numbers are entered. LOL!

No worries... I played around with the code a little more and it seems to be working as it should now.

Thanks everyone for your suggestions!
Hi Tate,
There was a typo in my suggestion:
if(errorcode == 0)
That's why it never printed the inventory code.
Last edited on
No problem, Methodos. Thank you for letting me know. :-)
Topic archived. No new replies allowed.