Code Review - Printing Variable Information to Console

Hello, I was hoping a few people would take a quick look at some super simple code I created. The program simply prints the memory location, size, and value of different types of variables. What I don't like about it is that it seems like a bit much. I was thinking of putting the variables in a structure but I don't think that will help too much. I also might make a different function for each print job to help "condense" it. If you think there is a better way to display the variable name and associated information, please let me know, thank you!

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
#include <iostream>

using namespace std;

int main()
{
    int int_variable = 1;
    bool bool_variable = true;
    char char_variable = 'A';
    short int short_variable = 32766;
    long int long_variable = 9223372036854775806;
    float float_variable = 89234.454;
    double double_variable = 308923.4567;
    long double long_double_variable = 1029384.5938245;
    double double_array[5] = {2.5,8.93};
    int int_array[4] = {0,1,2,3};

    int *ptr_int = nullptr;
    float *ptr_float = nullptr;

    ptr_int = &int_variable;
    ptr_float = &float_variable;

    cout << "**********Integer Variable**********" << endl
         << "The address of the int_variable is " << &int_variable << endl
         << "The size of the int_variable is " << sizeof(int_variable) <<
            " bytes " << endl
         << "The value in int_variable is " << int_variable << endl << endl;

    cout << "**********Bool Variable**********" << endl
         << "The address of the bool_variable is " << &bool_variable << endl
         << "The size of the bool_variable is " << sizeof(bool_variable)
         << " bytes " << endl
         << "The value in bool_variable is " << bool_variable << endl << endl;

    cout << "**********Character Variable********** " << endl
         << "The address of the char_variable is " << &char_variable << endl
         << "The size of the char_variable is " << sizeof(char_variable) <<
            " bytes " << endl
         << "The value in char_variable is " << char_variable << endl << endl;

    cout << "**********Short Variable**********" << endl
         << "The address of the short_variable is " << &short_variable
         << endl
         << "The size of the short_variable is "<< sizeof(short_variable)
         << " bytes " << endl
         << "The value in short_variable is " << short_variable
         << endl << endl;

    cout << "*********Float Variable*********" << endl
         << "The address of the float_variable is " << &float_variable
         << endl
         << "The size of the float_variable is " << sizeof(float_variable)
         << " bytes " << endl
         << "The value in float_variable is " << float_variable << endl
         << endl;

    cout << "**********Double Variable**********" << endl
         << "The address of the double_variable is " << &double_variable << endl
         << "The size of the double_variable is " << sizeof(double_variable)
         << " bytes " << endl
         << "The value in double_variable is " << double_variable << endl
         << endl;

    cout << "**********Long Double Variable**********" << endl
         << "The address of the long_double_variable is "
         << &long_double_variable << endl
         << "The size of the long_double_variable is "
         << sizeof(long_double_variable) << " bytes " << endl
         << "The value in long_double_variable is " << long_double_variable
         << endl << endl;

    cout << "**********Double Array**********" << endl
         << "The address of the first element in double_array is "
         << &double_array[0] << endl
         << "The size of the element in the double_array is "
         << sizeof(double_array) << " bytes " << endl
         << "The value of the first element in the double_array is "
         << double_array[0] << endl << endl;

    cout << "**********Integer Array**********" << endl;

    for (int count = 0; count < 4; count++)
        cout << "The address of element " << count << " in int_array is "
             << &int_array[count] << endl
             << "The size of element " << count << " in the int_array is "
             << sizeof(int_array) << " bytes " << endl
             << "The value of element " << count << " in the int_array is "
             << int_array[count] << endl << endl;

    cout << "**********Integer Pointer Variable**********" << endl
         << "The address the integer pointer variable points to is "
         << ptr_int << endl
         << "The size of the int_variable is " << sizeof(int_variable)
         <<" bytes " << endl
         << "The value in int_variable is " << int_variable << endl << endl;

    cout << "**********Float Pointer Variable**********" << endl
         << "The address the float pointer variable points to is "
         << ptr_float << endl
         << "The size of the float_variable is " << sizeof(float_variable)
         <<" bytes " << endl
         << "The value in float_variable is " << float_variable << endl << endl;

    return 0;
}
Sorry, I didn't get round to sorting the arrays out similarly, but you should get the gist.

If you find yourself writing the same thing lots of times then consider functions ... and if variables of different types then consider templates:
http://www.cplusplus.com/doc/tutorial/functions2/

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
#include <iostream>
using namespace std;

template <typename T> void allAboutIt( string description, T &var )
{
   cout << description << '\n';
   cout << "Address: " << &var << '\n';
   cout << "Size: " << sizeof( var ) << " bytes\n";
   cout << "Value: " << var << "\n\n";
}


int main()
{
    int int_variable = 1;
    bool bool_variable = true;
    char char_variable = 'A';
    short int short_variable = 32766;
    float float_variable = 89234.454;
    double double_variable = 308923.4567;
    long double long_double_variable = 1029384.5938245;
    double double_array[5] = {2.5,8.93};
    int int_array[4] = {0,1,2,3};

    int *ptr_int = nullptr;
    float *ptr_float = nullptr;

    ptr_int = &int_variable;
    ptr_float = &float_variable;

    allAboutIt( "Integer:    ", int_variable    );
    allAboutIt( "Bool:       ", bool_variable   );
    allAboutIt( "Character:  ", char_variable   );
    allAboutIt( "Short:      ", short_variable  );
    allAboutIt( "Float:      ", float_variable  );
    allAboutIt( "Double:     ", double_variable );
    allAboutIt( "Long double:", long_double_variable );
}
Last edited on
Topic archived. No new replies allowed.