+ 1

How would I explicitly state this positive condition? [SOLVED]

I just wrote a code in an attempt to scan into an array and print 5 integer values , so long as they are positive. When a negative number is given, or the request of 5 positive integers has been satisfied, the code will output the given integers. For some reason, my code does everything right except it still accepts negative integers. Can someone take a look at it and see where I am going wrong? My suspicions lie in the darned syntax... \#include <stdio.h> &#x200B; int main(void){ int count=0; int x; // TODO: Declare array named values\[ \] with five elements int values\[4\]; // TODO: Obtain array input until a negative value or // more than five are entered while (count<5 && x>=0){ scanf("%i",&x); values\[count\] = x; \++count; } // Print array values printf("%i values entered: ",count); for (int i=0; i < count; i++) { printf("%i ",values\[i\]); } return 0; }

4th Apr 2021, 4:43 AM
Alejandro
8 Respuestas
+ 3
Alejandro, There are many unnecessary '\' character in parts of the code, so you must remove them first. And then that &#x200B; also, it is not a valid statement, it shoud go. Then, you need to define an `int` array named <values> with 5 elements, not 4. int values[5]; Check for number of input that's been accepted only, we can check for negative value in the loop body while (count < 5) { scanf("%i", &x); if (x < 0) break; values[count] = x; ++count; } P.S.Next time around please, share a saved code bit link instead of raw text like this. It will be easier to test, and we can refer line numbers easily 👍 https://www.sololearn.com/post/75089/?ref=app
4th Apr 2021, 5:58 AM
Ipang
+ 2
I’m new here, and quite honestly, pretty new to coding as well! Thank you for the help and pointers!
4th Apr 2021, 6:16 AM
Alejandro
+ 2
Alejandro, Not a problem, we all gotta start somewhere, sometime 👌
4th Apr 2021, 6:18 AM
Ipang
+ 2
Evaluation for value of <x> needs to be done twice with utlisation of do-while loop. Once to decide whether or not <x> should be inserted into the array, once again in the loop condition. All roads lead to Rome in the end 👍
4th Apr 2021, 4:33 PM
Ipang
+ 1
Martin Taylor, If I didn't use `break`, then negative value will be stored into the array, but that's not the task requirement. How to prevent saving negative value into the array when loop condition and negative value verification is evaluated after loop body block is processed?
4th Apr 2021, 1:43 PM
Ipang
+ 1
Ipang, just a question for learning purposes. If the condition to prevent negative numbers was not evaluated in the while loop, would there be another way to express that without a break command?
4th Apr 2021, 1:50 PM
Alejandro
+ 1
Alejandro, I think this may work, in case the use of `break` in loop body is not allowed. But TBH, I don't really like it, cause it looks ugly. #include <stdio.h> int main() { int v, array[ 5 ]; size_t count = 0; while( ( 1== scanf( "%d", &v ) ) && v > -1 && count < 5 ) { array[ count++ ] = v; } printf( "%lu positive values read.\n", count ); for( size_t i = 0; i < count; i++ ) { printf( "%d ", array[ i ] ); } return 0; }
4th Apr 2021, 2:29 PM
Ipang
+ 1
Martin Taylor, You mean something like this? while( count < 5 ) { if( x > -1 ) { // add value to array //++ count; // should <count> be incremented here? } //++count; // or in here? }
4th Apr 2021, 2:41 PM
Ipang