Using counter variables in IF statements (C++)

  • Context: C/C++ 
  • Thread starter Thread starter owmybrain
  • Start date Start date
  • Tags Tags
    Counter Variables
Click For Summary
SUMMARY

The discussion focuses on the incorrect behavior of counter variables in C++ IF statements. The user is experiencing unexpected outputs, receiving four outputs instead of the intended two. The code provided includes multiple conditional statements that modify the counter variable and produce outputs based on user inputs. Key issues arise from the use of multiple IF statements without an ELSE IF structure, leading to multiple conditions being satisfied.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with conditional statements in programming
  • Knowledge of variable scope and initialization in C++
  • Experience with user input handling in C++
NEXT STEPS
  • Review C++ conditional statements and the use of ELSE IF for better control flow
  • Learn about variable scope and how it affects program behavior
  • Explore debugging techniques in C++ to identify logical errors
  • Study best practices for user input validation in C++ applications
USEFUL FOR

C++ developers, programming students, and anyone troubleshooting logical errors in conditional statements.

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

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