Problems with struct again :(

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
// Fractions.cpp : main project file.

#include "stdafx.h"
#include <iostream>

using namespace std;

struct fraction
{
	int denominator;
	int numerator;
};

int main()
{
	fraction denominator = 0;
	fraction numerator = 0;

	cout << "Enter the numerator ";
	cin >> fraction.numerator;
	cout << "Enter the denominator ";
	cin >> fraction.denominator;

	cout << "That is " << fraction.numerator << "/" << fraction.denominator << endl;
	
	return 0;
}


Run it and get the errors if you can. I'm having a bit of trouble with actually coding the struct correctly.
The struct is fine; the way you're accessing it isn't.

1
2
	fraction denominator = 0;
	fraction numerator = 0;

Here, you create two objects of struct fraction on the stack and initialize them to an integral value, namely 0. What you probably wanted to do was more like
fraction frac = {0, 0};
which would create an object called frac, (with denominator and numerator initialized to 0) which could be accessed like you did below:
1
2
3
4
	cout << "Enter the numerator ";
	cin >> fraction.numerator;
	cout << "Enter the denominator ";
	cin >> fraction.denominator;

except where fraction is a different name; because I don't think you can have an object of struct fraction called fraction. That'd be like having int int = 0;. I might be wrong there, though.

If you want to print fractions like this:
1024
----
2048
i.e. dynamically handling the amount of digits in whichever of those is larger (above, 4); you could do something like this:
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
#include <iostream>
#include <iomanip>

using namespace std;

struct fraction
{
    int denominator;
    int numerator;
};

int main()
{
    fraction frac = {0, 0};

    cout << "Enter the numerator: ";
    cin >> frac.numerator;
    cout << "Enter the denominator: ";
    cin >> frac.denominator;

    int field_width = 0,
        tmp = 0;
    char prev_fill  = 0;
    streamsize prev_width = 0;
    
    /* Get the amount of digits in frac.numerator or frac.denominator, whichever
     * is larger */
    tmp = (frac.numerator > frac.denominator) ? frac.numerator
                                              : frac.denominator;
    
    if (tmp < 0)
        tmp = -tmp;
        
    while (tmp > 0) {
        field_width++;
        tmp /= 10;
    }

#if 0 /* This code doesn't work; not sure why. */
    prev_fill  = cout.fill('-'); /* Set fill character to hyphen ('-') */
    prev_width = cout.width(field_width); /* Number of digits in frac.numerator
                                           * or frac.denominator, whichever is
                                           * larger */
    cout << frac.numerator << "\n"
         << '-'            << "\n" /* This will be repeated field_width times */
         << frac.denominator
         << endl;
    
    cout.fill(prev_fill);
    cout.width(prev_width);
#else /* This code does */
    cout << frac.numerator << "\n";
    tmp = 0;

    while (tmp++ < field_width)
        cout << "-";

    cout << "\n" << frac.denominator << "\n";
#endif
    return 0;
}

Ideally you would get the code in the #if 0 ... else block working; the code in the #else ... endif block is probably less efficient.
Last edited on
Topic archived. No new replies allowed.