C/C++ C++: Printing output based on variable value

Click For Summary
The discussion focuses on a programming task involving the output of specific strings based on the value of an enumerated type, `GroceryItem`. Initially, the user attempted to use a while loop to determine the output, but it was pointed out that the loop's condition would prevent execution. The user then revised the code to use if and else if statements, which correctly output "Fruit" for `GR_APPLES`, "Drink" for `GR_JUICE` and `GR_WATER`, and "Unknown" for any unrecognized value. The final code successfully passed all tests, demonstrating the correct implementation of conditional logic 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
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
5K
Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 5 ·
Replies
5
Views
3K