Error check in C when a char has been entered intead of an int?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alenik1989
    New Member
    • Oct 2007
    • 64

    Error check in C when a char has been entered intead of an int?

    How can i check and creat a loop for the error in which a char has been entered by user insted of an int input!!!!??
    Im new to C, so please keep it simple!!!!!!Tan x
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    I'd use a while loop, you can test the ASCII value, but it mostly depends on who you plan on reading it in.

    Comment

    • Alenik1989
      New Member
      • Oct 2007
      • 64

      #3
      Originally posted by sicarie
      I'd use a while loop, you can test the ASCII value, but it mostly depends on who you plan on reading it in.
      I just suppose to get 3 int numbers as 3 sides of a triangle. So I cant allow the user to enter any char or decimals. So is the ASCII valuse a good choice in a while loop or not ( in error checking)?

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        I would recommend reading a value in as a char, then comparing that. You can use the char value, or convert it if you want. That, encompassed in a while, will probably do it.

        Comment

        • Alenik1989
          New Member
          • Oct 2007
          • 64

          #5
          i can do that. But there is a problrm.
          what if a side of triangle be greater than 9. for example 123, 321, 32. Then what?

          Comment

          • Arulmurugan
            New Member
            • Jan 2008
            • 90

            #6
            Originally posted by Alenik1989
            i can do that. But there is a problrm.
            what if a side of triangle be greater than 9. for example 123, 321, 32. Then what?

            Hi ,
            There are multiple way to solve ur problem.
            1.check the return status of the scanf function and can conclude it.
            2.get number as string and verify it then convert it to int.
            3.get chars one by one then parese it and pass it to you program.
            Regards,
            Arul

            Comment

            • Alenik1989
              New Member
              • Oct 2007
              • 64

              #7
              Originally posted by Arulmurugan
              Hi ,
              There are multiple way to solve ur problem.
              1.check the return status of the scanf function and can conclude it.
              2.get number as string and verify it then convert it to int.
              3.get chars one by one then parese it and pass it to you program.
              Regards,
              Arul
              So i will get the int numbers in a string.....chec k to see if they are char or nor then convert them into int ...right????!!!

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                I think this thread might help you get a better idea of one method you can use.

                Comment

                • Arulmurugan
                  New Member
                  • Jan 2008
                  • 90

                  #9
                  Originally posted by Alenik1989
                  So i will get the int numbers in a string.....chec k to see if they are char or nor then convert them into int ...right????!!!
                  yes, also can go with scanf return val

                  Comment

                  Working...