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

  • Thread starter Thread starter owmybrain
  • Start date Start date
  • Tags Tags
    Counter Variables
AI Thread 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..
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top