Program 17

Continuing the previous program: Change the program so that it uses doubles instead of ints. Change the program so that it writes out the numbers are almost equal after writing out which is the larger and the smaller if the two numbers differ less than 1.0/100

Strange program, behaves like this:

Enter two double values:
6.0 6.1
The smaller value is: 6 and the larger value is: 6.1
enter | to quit or any other character to continue.
7.09 7.2
Enter two double values:
The smaller value is: 0.09 and the larger value is: 7.2
enter | to quit or any other character to continue.
6.99 7
Enter two double values:
The smaller value is: 0.99 and the larger value is: 7
enter | to quit or any other character to continue.

Here is the code, some advice would be appreciated thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 #include<iostream>
#include<math.h>
#include<stdlib.h>

using namespace std;

int main()
{

 
  
  double a=0.0;
  double b=0.0;
  char c='0';

  

while ( c != '|') {

      cout<< "Enter two double values: \n"; 
      cin>>a>>b; 
    
      
    if (a<b)

          { cout<< "The smaller value is: "<<a<<" and the larger value is: "<<b<<" \n"; }
     
    else if (a>b) 

          { cout<< "The smaller value is: "<<b<<" and the larger value is: "<<a<<" \n"; }

    if (a==b) 

             {  cout<< "The numbers are equal\n"; }

    else if (fabs(a-b) < 1.0/100)

             cout<<"The numbers are almost equal.\n\n";

    cout << "enter | to quit or any other character to continue." << endl;
    cin >> c;
 
Last edited on
cin >> c; It will get first character you type. So after your first input it will read 7 and line 22 will read only .09 7.2. .09 == 0.09
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cmath> // <math.h>
//#include<stdlib.h>

// using namespace std;

int main()
{
    int c = ' ' ;
    while ( c != '|' && c != EOF ) {

        std::cout << "Enter two double values: " ;

        double a = 0.0;
        double b = 0.0;
        if( std::cin >> a >> b ) {

            // put these checks first
            if( a == b )
                std::cout << "The numbers are equal\n";

            else if( std::fabs(a-b) < 0.001 )
                std::cout <<"The numbers are almost equal.\n\n";

            else if( a < b ) // std::isless(a,b)
                std::cout << "The smaller value is: " << a << " and the larger value is: " << b << '\n' ;

            else
                std::cout << "The smaller value is: " << b << " and the larger value is: " << a << '\n' ;
        }

        else
            std::cout << "invalid input was ignored\n" ;

        std::cout << "enter | to quit or hit enter to continue.\n" ;
        std::cin.clear() ; // clear possible failed state
        std::cin.ignore( 1000, '\n' ) ; // empty the input buffer
        c = std::cin.get() ;
    }
}
Getting error if trying to compile,

Documents/Program13.cpp: In function ‘int main()’:
Documents/Program13.cpp:10:30: error: ‘EOF’ was not declared in this scope
while ( c != '|' && c != EOF ) {
^
Documents/Program13.cpp:22:22: error: ‘fabs’ is not a member of ‘std’
else if( std::fabs(a-b) < 0.001 )
^
Documents/Program13.cpp:22:22: note: suggested alternative:
In file included from /usr/include/features.h:371:0,
from /usr/include/x86_64-linux-gnu/c++/4.8/bits/os_defines.h:39,
from /usr/include/x86_64-linux-gnu/c++/4.8/bits/c++config.h:426,
from /usr/include/c++/4.8/iostream:38,
from Documents/Program13.cpp:1:
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:182:1: note: ‘fabs’
__MATHCALLX (fabs,, (_Mdouble_ __x), (__const__));
^
> error: ‘EOF’ was not declared in this scope

#include <cstdio> (missing in the original code)


> error: ‘fabs’ is not a member of ‘std’

#include <cmath> (do not delete or comment out)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cmath> // **** for std::fabs ****
#include <cstdio> // **** for manifest constant 'EOF' **** added ****

int main()
{
    int c = ' ' ;
    while ( c != '|' && c != EOF ) {

        std::cout << "Enter two double values: " ;

        double a = 0.0;
        double b = 0.0;
        if( std::cin >> a >> b ) {
            
            std::cout << "a == " << a << " and b == " << b << "    " ; 
            // put these checks first
            if( a == b )
                std::cout << "The numbers are equal\n";

            else if( std::fabs(a-b) < 0.001 )
                std::cout <<"The numbers are almost equal.\n";

            else if( a < b ) // std::isless(a,b)
                std::cout << "The smaller value is: " << a << " and the larger value is: " << b << '\n' ;

            else
                std::cout << "The smaller value is: " << b << " and the larger value is: " << a << '\n' ;
        }

        else
            std::cout << "invalid input was ignored\n" ;

        std::cout << "enter | to quit or hit enter to continue.\n" ;
        std::cin.clear() ; // clear possible failed state
        std::cin.ignore( 1000, '\n' ) ; // empty the input buffer
        c = std::cin.get() ;
    }
}

http://rextester.com/WHK68676
Ok, I`m still a bit confused of the code.

Is the mission of cin.clear() on line 35 to reset any error flags on the stream? If so, why is this done?

cin.ignore ( 1000, '\n' ) Stops extracting characters if the end-of-file is reached? Or does it empty all the

input memory?

c = std::cin.get():

cin.get() will take some information from the cin input stream. I would pass it a character and it would set that character with a value from the input stream. In this case it assigns c with value '|' that terminates the program.


> Is the mission of cin.clear() on line 35 to reset any error flags on the stream?

Yes.


> If so, why is this done?

If the expression in if( std::cin >> a >> b ) is evaluated as false, the attempt to read two numbers failed; the stream is put into a failed state. https://stdcxx.apache.org/doc/stdlibug/29-1.html

The stream in a failed state can't perform more input/output operations till it is put back into a good state.


> cin.ignore ( 1000, '\n' ) Stops extracting characters if the end-of-file is reached?
> Or does it empty all the input memory?

It extracts and discards (up to a maximum of 1000) characters from the input buffer till a new line is extracted and thrown away. One reason why this is required is that there might have been an input error, and we should throw away the junk remaining in the input buffer.

This is required even if there was no error in input is because we want to throw away the new line left in the buffer after the formatted input, before we attempt unformatted input with std::cin.get() ;
To understand why: http://www.cplusplus.com/forum/general/69685/#msg372532


> cin.get() will take some information from the cin input stream.

std::cin.get() retrieves the next character (unformatted input) from the input buffer as an int; unformatted input means that it does not skip white space characters.
If the stream has reached end of file (nothing more can be read), it returns EOF.
http://www.cplusplus.com/reference/istream/istream/get/

If the next character was '|' or we got an EOF, we exit from the loop to end the program.
Nice explanation, I`m trying to understand how Input/Output streams work, I would actually write it this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
 

#include<iostream>
#include<cmath>
#include<stdlib.h>
#include<cstdio> 

using namespace std;

int main()
{
    int c = ' ' ;
    while ( c != '|' ) {

        cout << "Enter two double values: " ;

        double a = 0.0;
        double b = 0.0;
        cin>>a>>b;  

            
            if( a == b )
                cout << "The numbers are equal\n";

            else if( fabs(a-b) < 0.001 )
                cout <<"The numbers are almost equal.\n\n";

            if( a < b ) 
                cout << "The smaller value is: " << a << " and the larger value is: " << b << '\n' ;

            else
                cout << "The smaller value is: " << b << " and the larger value is: " << a << '\n' ;
        
      }
         

       else
            cout << "invalid input was ignored\n" ;

        

   
        cout << "enter | to quit or hit enter to continue.\n" ;
        cin>>c;
        if(!cin) {
           if (cin.bad()) error("cin is bad");
           if(cin.eof() {}                                    // No more input
           
           if(cin.fail()) {
                  cin.clear(); }
           c = cin.get() ;
          
        
    }
}     
         
   
Topic archived. No new replies allowed.