Using counter variables in IF statements (C++)

In summary, the conversation is about the counter not working and getting four outputs instead of two. The code includes prompts for user input and multiple if statements with different conditions. The purpose of the code is to determine what output to display based on the user's inputs.
  • #1
owmybrain
1
0
Hello,

I can't figure out why the counter is not working, I have tried moving it before the cout line and after. I should only get two outputs but as of now, I get four. Here is what I have so far:

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

int main() {
   
int userInput1;
string userInput2;
int counter = 1;

  
   cout << "Enter Input 1: ";
   cin >> userInput1;
   cout << userInput1 << endl;
   
   cout << "Enter Input 2: ";
   cin >> userInput2;
   cout << userInput2 << endl;

   if (userInput1 > 27 || userInput2 == "classroom") {
   cout << "cold";
   counter = userInput1 +1;
   cout << endl;
   }

   if (userInput1 <= 69 && userInput2.compare("grab") < 0) {
    counter = userInput1 +1;
   cout << "majority" << endl;
   }
   
   if (userInput1 == 44 && userInput2.compare("disaster") != 0) {
    counter = userInput1 +1;
   cout << "greet" << endl;
   }
   
   if (userInput1 > 15 || userInput2.at(0) == 'r') {
    counter = userInput1 +1;
   cout << "equal" << endl;
   }

   if (userInput1 % 7 == 0 || userInput2.compare("labor") >= 0) {
   counter = userInput1 +1;
   cout << "cereal" << endl;
   }

   if (userInput1 == 295 && userInput2.compare("origin") == 0) {
  counter = userInput1 +1;
   cout << "bang" << endl;
   }   

   else {
    counter = userInput1 +1;
   cout << "leave" << endl;
   
}

   return 0;
}
 
Technology news on Phys.org
  • #2
It depends what your inputs are..
 

1. How do I use a counter variable in an IF statement in C++?

To use a counter variable in an IF statement in C++, you will need to declare and initialize the counter variable before the IF statement. Then, you can use the counter variable within the IF statement's condition to check if a certain condition is met.

2. What is the purpose of using a counter variable in an IF statement?

A counter variable in an IF statement allows you to keep track of the number of times a certain condition is met. This can be useful for looping through a set of data or for controlling the flow of your program.

3. Can I use a counter variable in an IF statement without declaring it first?

No, you must declare and initialize a counter variable before using it in an IF statement. Without this step, the compiler will not recognize the counter variable and your IF statement will not function properly.

4. How can I increment or decrement a counter variable within an IF statement?

You can increment or decrement a counter variable within an IF statement by using the appropriate operators, such as the ++ or -- operator. These operators will increase or decrease the value of the counter variable by 1 each time the IF statement is executed.

5. Is it possible to use multiple counter variables in a single IF statement?

Yes, it is possible to use multiple counter variables in a single IF statement. This can be helpful when you need to track multiple conditions or have a complex IF statement with multiple levels of conditions.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
Replies
10
Views
960
  • Programming and Computer Science
Replies
3
Views
733
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
2
Views
877
Back
Top