I have wrote a program for an assignment. The problem I am having is that I can not figure out how to make a for loop that limits the amount of data that can be entered. For example, for this assignment the user can enter lets say 100 grades. Any advice on how and where to add the for loop?
#include <stdio.h>
#include <stdlib.h>
/* Kimberly Brand - IT2240 * 05/17/2022
*/
int main (void) {
// defines a max of 100
#define maxnum 100
int i, grade[maxnum], maxentered;
i = 0;
printf ("Welcome! \n");
printf ("Please enter your grades, enter -1 when finished \n\n");
// while statemtn accepts numbers over 0
while (i < 100) {
printf ("please enter your grade ");
scanf ("%i", & grade[i]);
// ends program if - 1 is entered
if (grade[i] == -1) { //max entered only accepts 100
maxentered = i;
i = 100;
} else
i ;
}
printf ("Your grades are :\n");
for (i = 0; i < maxentered; i ) {
// displays grade entered
printf ("%i", grade[i]);
printf ("\n");
}
printf ("Thank you!\n");
}
CodePudding user response:
while
is a looping construct, and you do already limit the amount of data that can be read.
If you initialize maxentered
to maxnum
(for the event where the limit is met before the user enters -1
), and change the bounds of the loop as well (to avoid the magic number 100 that may not be kept in sync with the size of the array), this program will work just fine (assuming scanf
never fails).
#include <stdio.h>
#include <stdlib.h>
/* Kimberly Brand - IT2240 * 05/17/2022
*/
int main (void) {
// defines a max of 100
#define maxnum 100
int i, grade[maxnum], maxentered = maxnum;
i = 0;
printf ("Welcome! \n");
printf ("Please enter your grades, enter -1 when finished \n\n");
// while statemtn accepts numbers over 0
while (i < maxnum) {
printf ("please enter your grade ");
scanf ("%i", & grade[i]);
// ends program if - 1 is entered
if (grade[i] == -1) { //max entered only accepts 100
maxentered = i;
i = 100;
} else
i ;
}
printf ("Your grades are :\n");
for (i = 0; i < maxentered; i ) {
// displays grade entered
printf ("%i", grade[i]);
printf ("\n");
}
printf ("Thank you!\n");
}
That said, it can be cleaned up somewhat.
Always check the return value of scanf
is the expected number of successful conversions that took place. We are looking for one conversion to occur, so we want the return value to be 1
.
Once the loop is complete, the original indexing variable can be thought of as holding the length of the valid entries in the array. This can just be reused as the new bound when accessing the data.
size_t
is the appropriate type for indexing memory and dealing with object sizes. While it does not matter all that much for smaller data structures, it is a good habit to practice.
#include <stdio.h>
#define MAX_ENTRIES 100
int main(void) {
int grades[MAX_ENTRIES];
size_t len = 0;
printf("Welcome!\nPlease enter your grades, enter -1 when finished.\n\n");
while (len < MAX_ENTRIES) {
printf("Please enter your grade: ");
if (1 != scanf("%d", &grades[len])) {
fprintf(stderr, "Failed to read input.\n");
return 1;
}
if (grades[len] == -1)
break;
len ;
}
printf("Your grades are:\n");
for (size_t i = 0; i < len; i )
printf("[#%zu] %d\n", i 1, grades[i]);
printf("Thank you!\n");
}
CodePudding user response:
Instead of setting i
directly to 100
, you can use a break
statement to exit the loop.
https://en.cppreference.com/w/cpp/language/break
// ...
while (i < 100) {
printf ("please enter your grade ");
scanf ("%i", & grade[i]);
// ends program if - 1 is entered
if (grade[i] == -1) { //max entered only accepts 100
maxentered = i;
break; // <<< NEW
} else
i ;
}
// ...
CodePudding user response:
Do you mean you want to change the while loop you have for a for loop?
Your while loop already limits the amount of data pretty successfully.
while (i < 100) {
printf ("please enter your grade ");
scanf ("%i", & grade[i]);
// ends program if - 1 is entered
if (grade[i] == -1) { //max entered only accepts 100
maxentered = i;
i = 100;
} else
i ;
}
If you where to change (i < 100) for (i < 5) your code would limit the amount of entries in the array to 5 even though it could store 100 entries.
But a for-loop is probably better than a while loop in this scenario. It would look something like this:
for(i = 0; i < 100; i ){
//And then just add the code you want to execute inside
//I would recommend using a 'break;' statement instead of relying
//on 'i = 100' to break out of the loop.
}
As a side note, you never gave maxentered a default value, so your program will crash if you never type -1 but instead type as many grades as your loop lets you.