Read movies information - C Language Basics

C examples for Language Basics:scanf

Description

Read movies information

Demo Code

#include <stdio.h>
#include <string.h>

int main(){/*from   w w w .j a  v  a2  s.  c o m*/
    int i, numMovies, rating, favRating, leastRating;
    char movieName[40], favorite[40], least[40];

    favRating = 0;
    leastRating = 10;

    do {
        printf("How many movies have you seen this year? ");
        scanf(" %d", &numMovies);

        if (numMovies < 1) {
            printf("No movies! How can you rank them?\n");
            printf("Try again!\n\n");
        }
    } while (numMovies < 1);

    for (i = 1; i <= numMovies; i++)
    {
        printf("\nWhat was the name of the movie? ");
        printf("(1-word titles only!) ");
        scanf(" %s", movieName);

        printf("On a scale of 1 to 10, what would ");
        printf("you rate it? ");
        scanf(" %d", &rating);

        if (rating > favRating){
            strcpy(favorite, movieName);
            favRating = rating;
        }

        if (rating < leastRating){
            strcpy(least, movieName);
            leastRating = rating;
        }
    }

    printf("\nYour Favorite Movie was %s.\n", favorite);
    printf("\nYour Least-favorite Movie was %s.\n", least);

    return 0;
}

Result


Related Tutorials