0

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 */



    }
8
  • You are missing an & before secid like: scanf("%1d", &secid); Commented Mar 9, 2017 at 19:44
  • 1
    You don't show secid definition. I guess you copied this from a printed book and misread %ld as %1d, with long int secid; previously defined. Or, long live 20th century typewriters lacking a 1 key! Commented Mar 9, 2017 at 19:47
  • Oops my edit about typewriters has reverse-logic. Commented Mar 9, 2017 at 19:54
  • @WeatherVane My intention with %1d was to only take a single digit of input. Does it not do this? Commented Mar 9, 2017 at 20:11
  • It restricts the input when you enter more than one digit, but if you enter only one digit is not satisfied and scanf does not return until another entry is made. The danger too, is if you limit input into scanf the extra characters remain in the input buffer. But since you go on to restrict the input with while(secid >= 6 || secid <= 10); I don't see the point of it. Commented Mar 9, 2017 at 20:18

3 Answers 3

1

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

Comments

1

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.

Comments

0

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

7 Comments

What does this portion do? int argc, char **argv
It has nothing to do with your problem, it is just that the compiler does not throw any warnings, if you want to know more details read this: tutorialspoint.com/cprogramming/c_command_line_arguments.htm
@DZDomi If you do not need int argc, char **argv Why is there? .. Why not (void) ?
cause it does not hurt to know about it :)
@DZDomi ...and by the way, SO is not a Tutorial Site.
|

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.