C/C++ Using counter variables in IF statements (C++)

  • Thread starter Thread starter owmybrain
  • Start date Start date
  • Tags Tags
    Counter Variables
Click For Summary
The discussion revolves around a code snippet in C++ where the user is experiencing an issue with a counter variable that is not functioning as expected. The user anticipates only two outputs based on their inputs but is receiving four. The code includes multiple conditional statements that print different outputs based on the values of `userInput1` and `userInput2`. Each condition modifies the `counter` variable, which is set to `userInput1 + 1`. The confusion arises from the fact that multiple conditions can be true simultaneously, leading to multiple outputs being printed. The final `else` statement also contributes to the output, as it executes if none of the previous conditions are met. The discussion highlights the importance of understanding how conditional statements interact and the need to structure them to achieve the desired output.
owmybrain
Messages
1
Reaction score
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
It depends what your inputs are..
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 4 ·
Replies
4
Views
6K
Replies
10
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
3
Views
1K
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 6 ·
Replies
6
Views
12K