input tests

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 1161 Views - Last Post: 05 May 2013 - 09:26 PM Rate Topic: -----

#1 Maria23  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 24-November 12

input tests

Posted 03 May 2013 - 01:04 PM

if i ask the user to enter a postive integer number and he enters a double number (like 5.4)
and i want the user to enter a new number insted, but if he enters 4.0 for example its a legal input... how do i know if it has been entered a double number not integer one... like if he enters a negative number i know by asking if number<0...
Is This A Good Question/Topic? 0
  • +

Replies To: input tests

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,737
  • Joined: 25-December 09

Re: input tests

Posted 03 May 2013 - 01:11 PM

A value of 4 is a valid integer and a valid floating point number. So I don't understand your question.



Jim
Was This Post Helpful? 0
  • +
  • -

#3 Maria23  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 24-November 12

Re: input tests

Posted 03 May 2013 - 01:21 PM

no... i know it takes the integer part of the number so 4.3 its 4, but what i mean is that the user cant enter a floating number... its illegal.. so 4.3 its illegal input and it should ask the user to enter another number that is positive and integer
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,737
  • Joined: 25-December 09

Re: input tests

Posted 03 May 2013 - 01:37 PM

If you are trying to receive an integer and the user enters a floating point value your input will accept the integral portion of the floating point value and leave the decimal portion in the input buffer, the stream state will not be paced in an error state. You can then use the istream.peek() function to test if there are any characters left in the input buffer.

Jim
Was This Post Helpful? 2
  • +
  • -

#5 Maria23  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 24-November 12

Re: input tests

Posted 03 May 2013 - 11:28 PM

yes i know all of that... i was surprised when the teacher asked that... he wants flaoting numbers to be illegal input even though the input accept the integer value and leave whats after the decimal point. so when a user enters flaoting number it shoukd ask him to enter an integer value !!
and he asked not to use anything we havnt learned yet in the class like what u said the istream.peek() function...
Was This Post Helpful? 0
  • +
  • -

#6 geohhot  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 33
  • Joined: 22-December 12

Re: input tests

Posted 04 May 2013 - 04:45 AM

Hm.. but that is possible to say if input was legal or no, look just at code
double inputD;
cin >> inputD;
// checking if double is equal to its integer part
if (inputD != (int)inputD) {
    cout << "INVALID INPUT!" << endl;
    return 1; // you need this ?
}
else {
   // valid input
   int a = (int)inputD;
}


What you think will this work ?
Was This Post Helpful? 0
  • +
  • -

#7 Maria23  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 24-November 12

Re: input tests

Posted 04 May 2013 - 06:42 AM

thnx... yes it works :):)
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,737
  • Joined: 25-December 09

Re: input tests

Posted 04 May 2013 - 07:09 AM

Quote

What you think will this work ?

No I don't think this will always work. Remember floating point numbers are not precise, they're approximations and there are floating point values that can't be properly represented.

The correct answer would be to always retrieve the user data into a string/C-string, validate the string and then convert to your number if it doesn't contain a decimal point or any other non-numeric characters.

Jim
Was This Post Helpful? 1
  • +
  • -

#9 Maria23  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 24-November 12

Re: input tests

Posted 04 May 2013 - 08:05 AM

give me an example for a number that wont work on that code cuz i cant find one.. this is my code now:
int input()
{
    int res;
    double num;
    printf("Enter a positive integer number: ");
    res=scanf("%lf",&num);
    if(res<1)
    {
        printf("Error!");
        exit(1);
    }

    while((num!=(int)num)||(num<=0))
    {
        printf("illegal input, please enter a positive integer number: ");
        res=scanf("%lf",&num);
        if(res<1)
        {
        printf("Error!");
        exit(1);
        }

    }
  return num;
}




Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,737
  • Joined: 25-December 09

Re: input tests

Posted 04 May 2013 - 08:19 AM

I think this compiler warning is enough explanation.

Quote

main.c||In function ‘input’:|
main.c|16|warning: comparing floating point with == or != is unsafe [-Wfloat-equal]|


Along with this link: What every computer scientist should know about Floating point.

Jim
Was This Post Helpful? 1
  • +
  • -

#11 Maria23  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 24-November 12

Re: input tests

Posted 04 May 2013 - 08:29 AM

is it strange that my compiler didnt give me that warning !?
Was This Post Helpful? 0
  • +
  • -

#12 geohhot  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 33
  • Joined: 22-December 12

Re: input tests

Posted 04 May 2013 - 08:36 AM

Wait jimblumberg. Give an example of number that won't work with my method ?
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,737
  • Joined: 25-December 09

Re: input tests

Posted 04 May 2013 - 08:45 AM

Quote

Wait jimblumberg. Give an example of number that won't work with my method ?

No, I'm not going to try to give you a number that won't work. You should never compare floating point numbers for equality, period end of discussion.

Quote

is it strange that my compiler didnt give me that warning !?

What compiler are you using? Many compilers don't issue many warnings by default, you need to increase the warning levels. The method depends on the compiler/IDE you're using.

Jim
Was This Post Helpful? 1
  • +
  • -

#14 Maria23  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 24-November 12

Re: input tests

Posted 04 May 2013 - 09:13 AM

i work on a program called CodeBlocks thats what my teacher wants...
i know i cant compare with floating numbers u are right but its working though...
what u said about using string method is longer but its perfectly right with no doubt but i dont think my teacher will count it as a mistake to compare floating number as long as the compiler we are using doesnt show that warning...
thanks alot
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,737
  • Joined: 25-December 09

Re: input tests

Posted 04 May 2013 - 09:31 AM

Since you're using Code::Blocks increase the warning levels. Right click your project in the Projects Window and select Build Property. See all those Compiler Flags check boxes? Start a checkin.

I use the following flags for a C program:

Quote

-Wshadow
-Winit-self
-Wredundant-decls
-Wcast-align
-Wundef
-Wfloat-equal
-Winline
-Wunreachable-code
-Wmissing-declarations
-Wmissing-include-dirs
-Wswitch-enum
-Wswitch-default
-Wmain
-pedantic-errors
-pedantic
-Wextra
-Wall
-g
-std=c11


Never compile without warnings and never ignore the warnings you receive.

Jim
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3