Can someone help me organize this code?


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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
Project help

#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
/*Input: You will input from a file a number (double)
Output: Display on the screen the properties of the number similar to the example below with correct format.
Example:
Input: 	double number
Output:     The original value is
            The square is
            The square root is
            The factorial is        
            Digits


*/



/*

FACTORAL
long factorial (long n)
{
    if (n > 1)
        return (n * factorial (n-1));
    else
        return (1);
}
int main ()
{
    double n1;
    ifstream xFile;
    xFile.open("na.txt"); //open file
    if (xFile.fail())
        cout << "Error! failed to open file. \n";

    else
    {
        cout<<" The original value is"<<n1;
        long n1;
        cout <<"The factorial is:"<< n1 << "! = " << factorial (n1);
    }
}
SQUARE ROOT
double sqrt (double x);

SQUARE 
#include<iostream>
#include<cmath>

using namespace std;

int main(){
float base = 5.0;
float power = 2.0;
float square = pow(base,power);
float squareroot = sqrt(square);

cout << base << " to the " <<  power << " power is: " << square << endl;
cout << "the square root of " << square << " is " << squareroot << endl;

}

*/
class Number
{
private:
    double num;
public:
    Number(); // the default value should be 0
    void setNum(double); // sets the number value
    double getNumber(); // allows ‘main()’ to use/return the value
    double Square(); // Squares the value
    double SRoot(); // Square Roots the value
    int Factorial();// Returns the factorial value of the integer
    int Digits(); //add up all of the #s represented
    void Properties(); // Shows all properties of a number
};
number::number()
{
    num=0;
}

void number::setNum(double g)
{

    num=g;
}

double number::getNumber()
{

    return ();
}

       double number::square()
{

    return ();
}

       double number::SRoot()
{

    return ();
}

       int number::Factorial()
{

    return ();
}
       int number::digits()
{

    return (//);
}
       void number::properties()
{
    cout<<"The original value is:"<<getNumber()<<endl;
    cout<<"The square is:"<<square()<<endl;
    cout<<"The square root is:"<<SRoot()<<endl;
    cout<<"The factorial is:"<<Factorial()<<endl;
    cout<<"Digits:"<<digits()<<endl;
}

int main(){
number g;
ifstream xFile;
    xFile.open("na.txt"); //open file
    if (xFile.fail())
        cout << "Error! failed to open file. \n";

    else
    {
}
}


Hello mary9734,
I do not understand what you are after? The uncommented code will work and looks organized to me, but unfinished. Some parts of the code that is commented out can be used in the uncommented code.

You have what you need to finish the program.

Hope that helps,

Andy
Hi, mary9734,
you have copied&pasted a lot of good code, but... are you sure it’s enough? :-)

Do you really want to calculate the factorial of a double?

Also: it seems you are up to a math exercise, but I’m not aware of any method to reckon the number of digits of a double only by a mathematical procedure - maybe there is, of course, since I’m terrible at math. The alternative should be to turn it into a string and get the string lenght().

In your requirement there’s written “...with correct format”. Is it possible there’s something you haven’t catch from the exercise?

Anyway, this is an example of your code ‘reorganised’ in my style, but without any solution for the two mentioned issues. For the number of digits, I just used the base 10 logarithm, which only works for the integer part of a number.
It’s up to you to decide if you want to get to the result your way or fix my code (I suggest the first).
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Input: You will input from a file a number (double)
// Output: Display on the screen the properties of the number similar 
// to the example below with correct format.
// Example:
// Input:     double number
// Output:    The original value is
//            The square is
//            The square root is
//            The factorial is        
//            Digits

#include <cmath>
#include <fstream>
#include <iostream>
#include <limits>

class Number
{
private:
    double num;

public:
    Number();                  // the default value should be 0
    void setNum(double);       // sets the number value
    double getNumber();        // allows 'main()' to use/return the value
    double square();           // Squares the value
    double sRoot();            // Square Roots the value
    long long int factorial(); // Returns the factorial value of the integer
    int digits();              // add up all of the #s represented
    void properties();         // Shows all properties of a number
    friend std::istream& operator>>(std::istream& is, Number& number);
};

Number::Number() { num=0; }

void Number::setNum(double g) { num=g; }

double Number::getNumber() { return num; }

double Number::square() { return std::pow(num, 2.0); }

double Number::sRoot() { return std::sqrt(num); }

long long int Number::factorial()
{
    if(num > 20) { // reasonable dimension
        return 0;
    }

    long long int fact = 1;
    for(int i{1}; i<=num; i++) {
        fact *= i;
    }

    return fact;
}

int Number::digits() { return int(log10(num)) + 1; }

void Number::properties()
{
    std::cout << "\nThe original value is: " << num << std::endl;
    std::cout << "The square is: " << square() << std::endl;
    std::cout << "The squareroot is: " << sRoot() << std::endl;
    std::cout << "The factorial is: ";
    long long int tmp = factorial();
    0 != tmp ? std::cout << tmp << '\n'
             : std::cout << "too big to be calculated!!\n";
    std::cout << "Digits: " << digits() << std::endl;
}

std::istream& operator>>(std::istream& is, Number& number)
{
    is >> number.num;

    return is;
}

void pressEnter();

int main()
{
    std::ifstream x_file("na.txt");
    if (!x_file) {
        std::cout << "Error! failed to open file. \n";
    } else {
        while(x_file) {
            Number g;
            x_file >> g;
            if(g.getNumber() == 0.0) {
                break;
            }
            g.properties();
        }
    }

    pressEnter();
    return 0;
}

void pressEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Topic archived. No new replies allowed.