C++: Printing output based on variable value

In summary, this program uses an enum to represent different grocery items. The user can input a grocery item and the program will output whether it is a fruit, drink, or unknown item. It uses if and else if statements to check the value of the userItem variable and print the corresponding category. If the value does not match any defined options, it will print "Unknown".
  • #1
needOfHelpCMath
72
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
  • #2
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.
 
  • #3
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?
 
  • #4
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. :)
 
  • #5
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
 

Related to C++: Printing output based on variable value

1. What is C++ and why is it used for printing output?

C++ is a high-level, general-purpose programming language that is widely used for developing software applications. It is used for printing output because it has powerful features and syntax that allow for efficient and flexible manipulation of data and variables.

2. How do I print output based on variable values in C++?

To print output based on variable values in C++, you can use the cout statement. This statement allows you to display the value of a variable or expression on the screen. You can also use formatting options to customize the output.

3. Can I use conditional statements to control the output in C++?

Yes, you can use conditional statements like if, else if, and switch to control the output based on variable values. These statements allow you to execute different blocks of code depending on the value of a variable.

4. How do I format the output in C++?

C++ offers various formatting options to customize the output, such as using setw to set the width of the output, setprecision to set the number of decimal places, and fixed to display floating-point numbers in fixed notation. You can also use escape sequences to add special characters or formatting to the output.

5. Is there a way to print multiple variables in a single statement in C++?

Yes, you can use the cout statement to print multiple variables in a single statement by using the insertion operator (<<). For example, cout << "The value of x is " << x << " and the value of y is " << y; will display the values of variables x and y in a single statement.

Similar threads

  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
Replies
3
Views
747
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
2
Views
8K
Back
Top