Missing structure Value

Been working on the following assignment :

1
2
3
4
5
6
7
8
9
10
  struct Resort
{
     string resortName;
     int numberLifts;
     int vertical;
     int averageSnowfall;
};
Resort r = { "Kirkwood", 15, 2000, 125 };
Resort resorts[100];
Resort *rPtr = NULL;


Write a function which will take a Resort structure as a parameter, and ask the user to input new values for each field of the structure. The values in the parameter must be changed to the new values and remain changed after the function call.

However my output have some value missed from the structure. I don't quite understand what am I missing and he does not show everything. Can you help explain and fix it?


My code and ouput below :

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

struct Resort
{
    string resortName;
    int numberLifts;
    int vertical;
    int averageSnowfall;
};
// Resort r = {"Kirkwood", 15, 2000, 125};
// Resort resorts[100];
// Resort *rPtr = NULL;

void printvalue(struct Resort data)
{
    cout << "Data of the structure :\n";
    cout << data.resortName << "-" << data.numberLifts << "-"
         << "-" << data.vertical << "- " << data.averageSnowfall << endl;
}

int main()
{

    Resort data;

    string name;
    int lift, vert, avgSnow;
    cout << "Resort Name?\n";
    cin >> name;

    cout << "Number Lift?\n";
    cin >> lift;

    cout << "Vertical?\n";
    cin >> vert;
    data.numberLifts = vert;

    cout << "Average SnowFall?\n";
    cin >> avgSnow;
    data.averageSnowfall = avgSnow;

    printvalue(data);

    return 0;
}



Resort Name?
Rrara
Number Lift?
56
Vertical?
76
Average SnowFall?
8
Data of the structure :
-76--0- 8


Perhaps something like:

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

struct Resort {
	string resortName;
	int numberLifts {};
	int vertical {};
	int averageSnowfall {};
};

void obtainvalues(Resort& r)
{
	cout << "Resort Name?\n";
	cin >> r.resortName;

	cout << "Number Lift?\n";
	cin >> r.numberLifts;

	cout << "Vertical?\n";
	cin >> r.vertical;

	cout << "Average SnowFall?\n";
	cin >> r.averageSnowfall;
}

void printvalue(const Resort& data)
{
	cout << "Data of the structure :\n";
	cout << data.resortName << "-" << data.numberLifts
		<< "-" << data.vertical << "-" << data.averageSnowfall << '\n';
}

int main()
{
	Resort r {"Kirkwood", 15, 2000, 125};

	printvalue(r);
	obtainvalues(r);
	printvalue(r);
}

Hello siid14,

I made some changes to your program and get this output:

Resort Name? Mad River
Number Lift? 4
Vertical? 750
Average Snow Fall? 80


Data of the structure :
Resort name:
Number of Lifts: 750
Vertical Limit: -858993460
Average Snow Fall: 80



And with a little adjustment I get this:

Resort Name? Mad River
Number Lift? 4
Vertical? 750
Average Snow Fall? 80


Data of the structure :
Resort name:
Number of Lifts: 750
Vertical Limit: 0
Average Snow Fall: 80


Notice in the first output: the name has no value. Number Lift has a value of 4 entered, but outputs 750. And vertical Limit is the uninitialized value of that variable.

In the 2nd output: the name has no value, The same problem with vertical limit. And now "vertical" is initialized and has a value of (0)zero.

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

#include <string>

using namespace std;

struct Resort
{
    string resortName;
    int numberLifts{};  // <--- ALWAYS initialize all your variables.
    int vertical{};
    int averageSnowfall{};
};

// Resort r = {"Kirkwood", 15, 2000, 125};
// Resort resorts[100];
// Resort *rPtr = NULL;

void printvalue(struct Resort data)
{
    cout << "\n\nData of the structure :\n";
    cout <<
        "Resort name: " << data.resortName << "\n"
        "Number of Lifts: " << data.numberLifts << "\n"
        "Vertical Limit: " << data.vertical << "\n"
        "Average Snow Fall: " << data.averageSnowfall << '\n';
}

int main()
{
    Resort data;

    string name;
    int lift{}, vert{}, avgSnow{};  // <--- ALWAYS initialize all your variables.

    cout << "Resort Name? ";
    std::getline(std::cin, name);

    cout << "Number Lift? ";
    cin >> lift;

    cout << "Vertical? ";
    cin >> vert;

    data.numberLifts = vert;

    cout << "Average Snow Fall? ";
    cin >> avgSnow;

    data.averageSnowfall = avgSnow;

    printvalue(data);

    return 0;  // <--- Not required, but makes a good break point for testing.
}

The program only does what you tell it to do.

Line 38 I changed to using "std::getline". Should the name have a space in it there is a problem because the formatted input (std::cin >> name;) will only take up the the first space or (\n) whichever comes 1st. If it is a space what is left is left in the buffer for the next input to read. This troughs off your input.

What you did not do is tell the program to store the name in the struct.

You have the same problem with line 41. You get your input, but do not store it in the struct.

Line 44 takes a value for "vert", but line 46 stores it in the wrong variable of the struct.

Lines 48 - 51 are correct.

When you get that working I would go with seeplus's suggestion of putting the input in a function. It is better to have "main" just direct the flow of the program and not be the program.

Notice how I chanced the 2nd "cout" in the "print" function. This makes it much easier to work with.

Andy
I would go with seeplus's suggestion of putting the input in a function


It is a requirement.

Write a function which will take a Resort structure as a parameter, and ask the user to input new values for each field of the structure. The values in the parameter must be changed to the new values and remain changed after the function call.

My bad. I missed that. Thank you.

Andy
Yes, I followed seeplus's suggestion. It made sense for the problem I'm doing. Your explanations are worthy and helpful. Thank you too, Andy.
Topic archived. No new replies allowed.