Reputation: 13
For starters, I am new to C and programming in general. I have more experience with PowerShell and bash scripting, so apologies in advance for errors with headers, indentation, syntax, etc. Anyway, I'm trying to complete this program for a class, but I've had some trouble with one particular section, so I'm looking for some guidance as I'm pretty lost right now.
To provide some background: I'm supposed to allow a user to enter a user code between 6 and 10. This code uniquely identifies the user, who would then be asked for input for several other integer values, which would be totaled and averaged at the end. However, the user must be able to start the program again and enter another number (between 6 and 10); the user must then go through the previous process again to finish the program.
My problem is I cannot use if statements, break, continue, exit, abort or goto; I must use a do while loop to figure out when the user is done entering input; and I must provide error messages for when the user enters the wrong input, prompting them to enter it again.
With what I've posted below, I cannot figure out how to give the user an option to continue and/or exit without using if, break, continue, etc and while also prompting for error messages. I'm probably overthinking something but if anyone can provide some insight I would greatly appreciate it.
#include<stdio.h>
main()
{
int usercode; /* setting variables for user code */
do
{
printf ("Please enter your user code: ");
scanf("%1d", secid); /* user must input 1 digit code */
} while(secid >= 6 || secid <= 10); /* code must be between numbers 6 and 10 */
}
Upvotes: 0
Views: 1824
Reputation: 1679
The code you are looking for looks like this:
#include<stdio.h>
int main(int argc, char **argv){
int secid = -1;
do {
printf("Please enter your user code: ");
scanf("%1d", &secid);
} while((secid < 6 || secid > 10) && printf("Error\n"));
printf("The user code was %d", secid);
return 0;
}
Upvotes: 0
Reputation: 76508
The while
at the end of a do
while
is like a repeated if
. If
will be asked again and again until it is false. Note that the block of do
while
will be executed at least once, because the cycle test is executed after the content of the block. You need int secid
instead of int usercode
. Inside the do
while
you need another do
while
to read the data and to calculate sum
and avg
in the process.
Upvotes: 1
Reputation: 12670
You don't use the loop for logging in, you use it for the other input
pseudocode:
get user id
do {
get a value
} while (value is not pause or quit)
Upvotes: 1