C++: Printing output based on variable value

Click For Summary
SUMMARY

The discussion focuses on implementing conditional output in C++ based on the value of an enumerated type, specifically using the enum GroceryItem. The final solution utilizes if and else if statements to print "Fruit" for GR_APPLES and GR_BANANAS, "Drink" for GR_JUICE and GR_WATER, and "Unknown" for any undefined values. The initial incorrect use of a while loop was corrected to ensure proper output.

PREREQUISITES
  • Understanding of C++ enumerations and their usage
  • Knowledge of conditional statements in C++ (if, else if)
  • Familiarity with standard input/output operations in C++
  • Basic debugging skills in C++ programming
NEXT STEPS
  • Learn about C++ enumerations and their practical applications
  • Explore advanced conditional logic in C++, including switch statements
  • Investigate debugging techniques for C++ programs
  • Study best practices for structuring C++ code for readability and maintainability
USEFUL FOR

C++ developers, programming students, and anyone looking to improve their skills in conditional logic and enumerations in C++.

needOfHelpCMath
Messages
70
Reaction score
0
Print either "Fruit", "Drink", or "Unknown" (followed by a newline) depending on the value of userItem. Print "Unknown" (followed by a newline) if the value of userItem does not match any of the defined options. For example, if userItem is GR_APPLES, output should be:

Fruit
Code:
#include <iostream>
using namespace std;

int main() {
   enum GroceryItem {GR_APPLES, GR_BANANAS, GR_JUICE, GR_WATER};

   GroceryItem userItem = GR_APPLES;

   

   userItem = GR_APPLES;
   while ( userItem != GR_APPLES) {
         if (userItem == GR_APPLES) {
         cout << userItem << "Fruit" << endl;
         
         }
   }
      

   return 0;
 
Technology news on Phys.org
Re: i don't know if I am on right track but give me some help

You have the statement:

[m]userItem = GR_APPLES;[/m]

And then the condition on your while loop is:

[m]userItem != GR_APPLES[/m]

So, the code within the loop will never get executed. Without any means of terminating the loop, if the condition on the loop was:

[m]userItem == GR_APPLES[/m]

the loop would run endlessly. When running a while loop, you need for the condition to eventually become false, so the loop will terminate.

edit: I have edited your thread title to show the nature of the question being asked.
 
Re: i don't know if I am on right track but give me some help

MarkFL said:
You have the statement:

[m]userItem = GR_APPLES;[/m]

And then the condition on your while loop is:

[m]userItem != GR_APPLES[/m]

So, the code within the loop will never get executed. Without any means of terminating the loop, if the condition on the loop was:

[m]userItem == GR_APPLES[/m]

the loop would run endlessly. When running a while loop, you need for the condition to eventually become false, so the loop will terminate.

edit: I have edited your thread title to show the nature of the question being asked.
okay I fix it but still would not print out 'Fruit" is any other hint you can give me?
 
Re: i don't know if I am on right track but give me some help

needOfHelpCMath said:
okay I fix it but still would not print out 'Fruit" is any other hint you can give me?

What does your amended code look like now? I will be glad to take a look and offer further suggestions. :)
 
Re: i don't know if I am on right track but give me some help

MarkFL said:
What does your amended code look like now? I will be glad to take a look and offer further suggestions. :)
well I figure out my issue that the programs did not want any while loop instead it wanted if and else if statements
Code:
#include <iostream>
using namespace std;

int main() {
   enum GroceryItem {GR_APPLES, GR_BANANAS, GR_JUICE, GR_WATER};

   GroceryItem userItem = GR_APPLES;

   if (userItem == GR_APPLES) {
      cout << "Fruit" << endl;
   }
    if (userItem == GR_BANANAS) {
      cout << "Fruit" << endl;
    }
   
   if(userItem == GR_JUICE) {
      cout << "Drink" << endl;
   }
   else if (userItem == GR_WATER) {
      cout << "Drink" << endl;
   }
   else if (userItem == (GroceryItem)5) {
   cout << "Unknown" << endl;
   }

   return 0;
}

All tests passed
Testing with userItem = GR_APPLES
Your output: Fruit
Testing with userItem = GR_JUICE
Your output: Drink
Testing with userItem = (GroceryItem)5
Your output: Unknown
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 4 ·
Replies
4
Views
5K
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 7 ·
Replies
7
Views
11K
  • · Replies 25 ·
Replies
25
Views
3K