Hi this is the code for life, universe and everything problem, where the program stops only when the user inputs the number "42"

I tried to understand the problem, but could not make it that how the program stops at exactly 42. Any help would be extremely appreciated.

#include <iostream>
 
int main(void) {
char c, d=10;
while(std::cin.get(c) && (c!='2' || d!='4') && std::cout.put(d))
d=c;
}

Hi this is the code for life, universe and everything problem, where the program stops only when the user inputs the number "42"

I tried to understand the problem, but could not make it that how the program stops at exactly 42. Any help would be extremely appreciated.

#include <iostream>
 
int main(void) {
char c, d=10;
while(std::cin.get(c) && (c!='2' || d!='4') && std::cout.put(d))
d=c;
}

First thing who told you to take it as a character...??? You just want to stop on 42 right take it as a integer and compare.

Hi this is the code for life, universe and everything problem, where the program stops only when the user inputs the number "42"

I tried to understand the problem, but could not make it that how the program stops at exactly 42. Any help would be extremely appreciated.

#include <iostream>
 
int main(void) {
char c, d=10;
while(std::cin.get(c) && (c!='2' || d!='4') && std::cout.put(d))
d=c;
}

Forget that "ugly code".

This does the same and is more readable :

#include<iostream>

int main()
{
  //enable less qualified name
   using std::cout;
   using std::cin;
   using std::endl;

   int terminateNumber = 42;
   int inputNumber = -1;

  while( inputNumber != terminateNumber )
  {
      cout <<" Enter a number : " ;
      cin >> inputNumber;
      cout<<"\nYour number is : " << inputNumber << endl;
      cout << endl;
   }
  
   return 0;
}

It hasn't been compiled though.

Hi friends,

Thanks for your prompt reply. Firstly, I took this solution from the list of sample solutions available at http://www.codechef.com/wiki/sample-solutions#C

I already know how to work a solution for this petty question in the conventional and "sane" way of comparing input with the integer 42, as suggested by @firstPerson and @csurfer.

But I was left goofified with the "that" solution in which '4' and '2' are compared separately as chars and then something weird happening to them, producing the right result. @firstPerson-- Yes the code is super ugly, but out of curiosity I still wanted to know how that chunk of code worked.

#include <iostream>
 
int main(void) {
char c, d=10;
while( std::cin.get(c) &&  //while cin does not fail to get input
        (c!='2' || d!='4') &&  //while c != '2' or d != '4'
        std::cout.put(d)  //while cout does not fail to write
        )
       d=c; //save last input
}

so basically when the user inputs a number , it gets saved into d,
and when the user input another number, it checks if that number is 2 and if the previously saved number was 4. if so then it quite, otherwise it continues.

OK here it goes.The piece of code provided by you works on the character buffer from which "cin" reads from, rather than the actual number 42.

Take the example of input of number 10.
It is treated as '1' '0' '\n' ,ie 1 followed by 0 followed by 0 followed by no character.

loop 1 : d=10 c not assigned

condition 1) When it enter the loop first condition to be checked or executed is cin.get() which fetches a character in this case '1' .Therefore c='1'

condition 2) c!='2' and d!='4' which is true

condition 3) print d ie here the character '\n' of ascii 10.

So finally you will get a newline. and then d=c

Loop2 : d='1',c='1'

c1) cin.get() therefore c='0'
c2) c!='2' and d!='4' is true
c3) Print d hence prints '1' , then d=c

Loop3: d='0',c='0'

c1) cin.get() therefore c='\n'
c2) c!='2' and d!='4'
c3) print d,therefore prints '0', then d='\n'

Now as there are no more inputs to read in the buffer until the buffer gets newly filled by some other characters the cin.get() waits and hence the while loop waits.
As we have already seen the first input 10 has already been printed and d holds '\n' and waits to print it.

Assume our next input is 42 taken as '4' '2' '\n'

Loop4 : d='\n' ,c= (un-assigned) <same as the first loop condition>

c1) cin.get() therefore c='4'
c2)c!='2' and d!='4'
c3) prints d ,ie a new line and then d=c

Loop 5: d='4' c='4'

c1)cin.get() therefore c='2'
c2) c!='2' and d!='4' is false
So breaks directly (refer K & R '&&' properties) and quits the entire program.

Man....
Thankyou guys. Both firstPerson and csurfer were awesome in there explanation... The focused and step by step explanation by csurfer was damn cool. Simple enough to get a first grader sailing...

Thankyou guys for relieving the problem off my gray matter. You guys rock!!! I totally understood the weird stuff now. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.