C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1
scanf(" %s", movieName);
printf("On a scale of 1 to 10, what would ");
printf("you rate it? ");
scanf(" %d", &rating);
//Check whether it's their best-rated movie so far
if (rating > favRating)
{
strcpy(favorite, movieName);
favRating = rating;
}
//Check whether it's their worst-rated movie so far
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;
}

Here is a sample output from the program:


Click here to view code image


How many movies have you seen this year? 5
What was the name of the movie? (1-word titles only!) Veranda
On a scale of 1 to 10, what would you rate it? 7
What was the name of the movie? (1-word titles only!) Easiness
On a scale of 1 to 10, what would you rate it? 3
What was the name of the movie? (1-word titles only!) TheJuggler
On a scale of 1 to 10, what would you rate it? 5
What was the name of the movie? (1-word titles only!) Kickpuncher
On a scale of 1 to 10, what would you rate it? 8
What was the name of the movie? (1-word titles only!) Celery
On a scale of 1 to 10, what would you rate it? 8
Your Favorite Movie was Kickpuncher
Your Least-favorite Movie was Easiness

Now, this program is a little long, but you should be able to follow it line by line, and the comments
should help as well. It also combines the use of a do-while loop, a for loop, and some data tests
using if statements. The first if statement serves as a data tester. You are asking users how many
movies they’ve seen, and the code then loops through that number of movies to get titles and ratings. If
the user enters 0 (or mistakenly enters a negative number), there will be no loop, so you give the user

Free download pdf