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..
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
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 tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top