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

AI Thread 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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
1
Views
6K
Replies
2
Views
5K
Replies
1
Views
1K
Replies
12
Views
2K
Replies
5
Views
8K
Replies
5
Views
3K
Back
Top