| Kovs95 (52) | |||
|
our program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely… rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits. Input: 1 2 88 42 99 Output: 1 2 88 Here is my code below! I successfully wrote the program to output the number that the user imputted. Is there a way to write it so that after the user imputs 42, it doesnt output the other numbers that the user imputs, like the example above? I want the user to be able to imput numbers after they imput 42, but the program does not output those numbers. Right now, it just closes the console once the user imputs 42.
| |||
|
|
|||
| JLBorges (1754) | |||
|
> I want the user to be able to imput numbers after they imput 42, > but the program does not output those numbers. This is one way:
| |||
|
|
|||
| Kovs95 (52) | |
|
thanks a lot! I understand it edit: I have a quick question Why does adding while (cin >> imput); stop processing imput after reading 42? Is there an "after" function in c++, so like "after this happen this happens"? That might be stupid sounding, but im just new to c++.
| |
|
Last edited on
|
|
| usandfriends (186) | |
| Because it is a completely different loop that does nothing but read in random input. | |
|
Last edited on
|
|
| JLBorges (1754) | |||||
The expression std::cin >> input && input != 42 evaluates to true ifa. We have succeeded in reading an integer b. And (&&) the integer is not equal to 42 Otherwise it evaluates to false
The first loop is exited from when the expression evaluates to false. And we carry on with the next loop:
| |||||
|
|
|||||
| Kovs95 (52) | |
| Awesome! thanks for that explanation! Until nextime | |
|
|
|