Hi I'm just starting programming in C and am struggling to write a program designed to take a string of integers and then output if the value being checked is smaller than the one before it. But I cannot get the program to repeat over the data and it seems to only be checking the first value. I have tried using loops but this further confused me. Sorry to ask such a basic question. Here is my code so far:
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[]) {
int num;
int smaller=0;
printf("Input integers: ");
scanf("%d", &num);
if (num<smaller) {
printf("*** value %d is smaller than %d \n", num, smaller);
}
smaller=num;
return 0;
}
You could use a do-while loop to ask the user for values over and over again until they type something invalid, like 0
:
int smaller=0;
int num=0;
do {
printf("Input an integer (0 to stop): ");
scanf("%d", &num);
if (num<smaller) {
printf("*** value %d is smaller than %d \n", num, smaller);
}
smaller=num;
} while (num != 0);