0

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;
}
1
  • 3
    Hmmm... if you want to repeat something you're probably going to need some kind of loop... have you tried following a basic C tutorial on basic language constructs like for/while loops? Commented Mar 28, 2014 at 3:21

2 Answers 2

4

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);
Sign up to request clarification or add additional context in comments.

Comments

-1
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;
}

2 Comments

This seems to read only one value, so doesn't answer the question at all.
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.