C++ fraction numbers

Jan 24, 2017 at 1:01pm
Write a c++ program to display a message if the number entered is whole or fraction number when the user enters a whole number or a fractional number. Pls help me. The compiler will accept input
Last edited on Jan 24, 2017 at 1:43pm
Jan 24, 2017 at 1:06pm
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main()
{
    double number = 0;

    cout << "Please enter a number: ";
    cin >> number;

    // ???

    return 0;
}


There you go. Just fill in the rest and it's all done.
Last edited on Jan 24, 2017 at 1:07pm
Jan 25, 2017 at 5:05pm
@Ekinne

Okay, here's a little more help..

if the number inputted minus int(number) does NOT equal zero, then the number inputted was fractional, otherwise, it's a whole number.
Jan 25, 2017 at 5:54pm
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

#include<iostream>
#include<string>
using namespace std;

int main()
{
    char num[10];
    bool check=true;
    int numInt=0;
    double numDouble=0;
    cout<<"\nEnter The Number : ";
    cin>>num;
    for(int i=0;i<10;i++)
    {
    if(num[i]=='.')
    {
    check=false;
    break;
    }
    }
    
    if(check==true)
    {
    cout<<"\nWhole Number";
    numInt=atoi(num);// converting char to int 
    }
    else
    {
    cout<<"\nFraction Number";
    numDouble=atoi(num); // converting char to double
    }
    
    return 0;
}



Enter The Number : 1.24

Fraction Number



Enter The Number : 200

Whole Number

Last edited on Jan 25, 2017 at 5:55pm
Jan 25, 2017 at 6:04pm
There's the ratio class in the standard library which would be the same thing. Why reinvent the wheel?
Jan 25, 2017 at 7:29pm
There's the ratio class in the standard library which would be the same thing. Why reinvent the wheel?

Perhaps you could show us how the above program might be implemented using std::ratio and user-supplied values.
Jan 25, 2017 at 7:47pm
Shh! I'm just messing with the guy because he excepts someone else to do his homework.
Jan 25, 2017 at 7:50pm
Shh! I'm just messing with the guy because he excepts someone else to do his homework.

If you're making a post solely to "mess" with someone, don't bother with the post -- all you're doing is decreasing the signal-to-noise ratio by providing more noise.
Topic archived. No new replies allowed.