Thread: Restrictions on input with C language

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    3

    Restrictions on input with C language

    How do you restrict a user to type in only positive numbers rather than alphabets if the condition is that the input has to be a positive integer otherwise the program must generate an error when the user inputs an alphabet or string.

    I have an assignment that says that I have to ask the user for the number of working hours to calculate their basic pay or overtime depending on the hours.
    I'm new to C and our professor has only taught us if-else and loop functions so I wrote my code like this...
    Could someone please check my code and tell me how I'm supposed to stop the user from inputting a string or character value and how I can get my re entered input checked again with my if-else statements.

    I will appreciate all the answers give to my query,

    Thank you and have a nice day

    Code:
    #include<studio.h>
    #include<conio.h>
    
    main(){
    int Basic_Pay = 325; 
    \\because this was the given pay per hour
    
    into OverTime_Pay = 457; \\and some long calculation lol 😂
    
    int Num_Hours; \\I'm using this variable to prompt the user for input as number of hours.
    
    printf("Enter the number of hours worked : ");
    
    scanf(" %d", &Num_Hours);
    
    If(Num_Hours >= 0 && <=8) {
    
    printf("your basic pay is : %d", Num_Hours*Basic_Pay);
    
    }\\condition that if the number of hours is between or equal to 0 and eight display the users basic payment per hour.
    
    Else if( Num_Hours > 8){
    
    int cal1 = Basic_Pay*Num_Hours;
    int cal2 = working hours greater than 8 - 8;
    int cal3 = cal2*OverTime_Pay;
    int cal4 = cal1 + cal3;
    
    printf("your basic pay including overtime is : %d", cal4);
    }
    else{
    
    printf("Error!");
    
    }
    printf(" please re-enter the input : ");
    
    scanf(" %d", &Num_Hours);
    
    \\ This code is the problem first it doesn't give me an error when I input an alphabet as an input fact it calculus the alphabet, secondly it does not recheck my input in the if-else statements after it has been re entered so yeah that's my problem
    }
    Last edited by Aimen; 04-12-2018 at 03:44 PM. Reason: Missed a couple of details

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Aimen
    How do you restrict a user to type in only positive numbers rather than alphabets if the condition is that the input has to be a positive integer otherwise the program must generate an error when the user inputs an alphabet or string.
    (...)
    I'm new to C and our professor has only taught us if-else and loop functions so I wrote my code like this...
    It looks like you only need to read integers, so that simplifies the problem somewhat. You should also assume that the user actually does enter something rather than trigger EOF, in which case the basic idea for what you're trying to do would be to observe that when you read an integer with scanf, it will return 1 when the read is successful, and then you want to check for a positive integer, so what you're looking to do is to keep looping while this expression is false: scanf("%d", &num) == 1 && num > 0. Hence:
    Code:
    int num;
    while (scanf("%d", &num) != 1 || num <= 0) {
        printf("Error: please enter a positive integer");
    }
    // use num
    By the way:
    • <studio.h> should have been <stdio.h>
    • <conio.h> is a non-standard header. Since you don't seem to be using anything that requires it, you should remove that header inclusion.
    • main() should be int main(void)
    • You need to indent your code, e.g.,
      Code:
      int main(void) {
      foo();
      }
      should be:
      Code:
      int main(void) {
          foo();
      }
    • A single-line comment in C begins with // not \\ though note that if you need to compile with respect to earlier versions of C you should stick to multi-line comments (even if they only span a single line).
    • It looks like you didn't bother to try compiling your code before posting it, e.g., you have wrongly used capitalisation for keywords like if and else.
    • Name your variables with meaningful names, e.g., find a better name for cal1 like total_basic_pay or something like that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    3
    Quote Originally Posted by laserlight View Post
    It looks like you only need to read integers, so that simplifies the problem somewhat. You should also assume that the user actually does enter something rather than trigger EOF, in which case the basic idea for what you're trying to do would be to observe that when you read an integer with scanf, it will return 1 when the read is successful, and then you want to check for a positive integer, so what you're looking to do is to keep looping while this expression is false: scanf("%d", &num) == 1 && num > 0. Hence:
    Code:
    int num;
    while (scanf("%d", &num) != 1 || num <= 0) {
        printf("Error: please enter a positive integer");
    }
    // use num
    By the way:
    • <studio.h> should have been <stdio.h>
    • <conio.h> is a non-standard header. Since you don't seem to be using anything that requires it, you should remove that header inclusion.
    • main() should be int main(void)
    • You need to indent your code, e.g.,
      Code:
      int main(void) {
      foo();
      }
      should be:
      Code:
      int main(void) {
          foo();
      }
    • A single-line comment in C begins with // not \\ though note that if you need to compile with respect to earlier versions of C you should stick to multi-line comments (even if they only span a single line).
    • It looks like you didn't bother to try compiling your code before posting it, e.g., you have wrongly used capitalisation for keywords like if and else.
    • Name your variables with meaningful names, e.g., find a better name for cal1 like total_basic_pay or something like that.
    You're right I didn't compile this code and yes I wrote it down roughly, my original code which I had compiled is on my computer I just wrote this so I could figure out what I did wrong and if there is some sort of restrict fiction in C also the unnecessary headers in C are due to my lack of understanding of the language, my apologize. I'm still learning hopefully I'll figure it out. Thank you bye the way for taking your time to post the reply and guide me on the basic of C I think I'm starting it understand it better with you reply so thanks
    Last edited by Aimen; 04-12-2018 at 09:59 PM.

  4. #4
    Registered User
    Join Date
    Apr 2018
    Posts
    3
    Hmm...although your code seems good but it still doesn't have anything to counter an char or string type.
    So when I input an alphabet I just receive an endless loop of an error message that's it.

    I could try do while or else and that wouldn't loop but the real problem with that is that it won't take my strings to be rechecked in the if statements and nor will it give me an error when it comes to inputting char and string types, it just won't loop.
    I'll keep trying maybe I'll find something.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    36
    Couple of things that might help steer you in the right direction.
    1. There are only 24 hours in a day
    2. Look up the ASCII table.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What are the restrictions on typeid?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2008, 07:15 PM
  2. Compute a square root (with restrictions)
    By brewbuck in forum Contests Board
    Replies: 45
    Last Post: 06-07-2007, 09:18 PM
  3. restrictions on pointers using const
    By Chaplin27 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2005, 10:23 AM
  4. Car restrictions
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 55
    Last Post: 06-06-2003, 06:56 PM

Tags for this Thread