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..
 

FAQ: Using counter variables in IF statements (C++)

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.

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.

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.

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.

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.

Back
Top