C++: Printing output based on variable value

Click For Summary

Discussion Overview

The discussion revolves around a C++ programming problem where participants are trying to print specific outputs based on the value of an enumerated variable, userItem. The focus is on correcting the logic used in conditional statements and loops to achieve the desired output.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant points out that the while loop condition userItem != GR_APPLES prevents the loop from executing, suggesting that the condition should allow for termination.
  • Another participant emphasizes the need for the loop condition to eventually become false for proper termination.
  • A later reply indicates that the original approach using a while loop was incorrect and suggests using if and else if statements instead.
  • One participant shares their amended code, which correctly uses if and else if statements to print "Fruit", "Drink", or "Unknown" based on the value of userItem.
  • Tests are mentioned where the outputs for different values of userItem are confirmed to be correct.

Areas of Agreement / Disagreement

Participants generally agree that the use of a while loop was inappropriate for this problem and that if and else if statements are the correct approach. However, there is no consensus on the initial implementation details, as participants provide varying suggestions and corrections.

Contextual Notes

Some limitations include the initial misunderstanding of loop conditions and the need for clarity on how to structure conditional statements in C++. There are also unresolved aspects regarding the handling of the unknown case with the cast to GroceryItem.

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