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