Requesting structure help

Hello. I am new to C++ and am struggling with the Structure format. One of the questions we have been asked is to write a C++ statement storing data for a destination. I understand how to create the struct but not sure how to output it correctly.

How do I get the tourInit portion to start the program in main so it displays this on the output first, then asks you for your destination?


So far everything else seems to work I just need help figuring out how to output that part first.
Thank you for any assistance or direction you can provide

The entire question is below:

A: Declare the variable destination of type tourType.
B: Write C++ statements to store the following data in destination: cityName—Chicago, distance—550 miles, travelTime—9 hours and 30 minutes.
C: Write the definition of a function to output the data stored in a variable of type tourType.
D: Write the definition of a value-returning function that inputs data into a variable of type tourType.
E: Write the definition of a void function with a reference parameter of type tourType to input data in a variable of type tourType.

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
# include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include<string>

using namespace std;

// predefined struct timeType
struct timeType
{
	int hr;
	double min;
	int sec;
};

//predefined structure tourType
struct tourType
{
	string cityName;
	int distance;
	timeType travelTime;
};

//A: declare destination of type tourType:
struct tourType destination;


void input(void);
void output(void);

int main()
{
	
	input();
	output();
	
	system("pause");
	return 0;
}


//B: store tour data for Chicago
void tourInit(struct tourType* tour)
{

	tour->cityName = "Chicago";
	tour->distance = 550;
	tour->travelTime.hr = 9;
	tour->travelTime.min = 30;
	
}

//C:  function that outputs data from variable tourType
void output(void)
{
	cout << destination.cityName << " "
		<< destination.distance << " "
		<< destination.travelTime.hr << " "
		<< destination.travelTime.min << " "
		<< endl;
}
//E: void function to input data into type tourType
void input(void)
{
	cout << "\nenter destination city: ";
	cin >> destination.cityName;
	cout << "\nenter distance in miles: ";
	cin >> destination.distance;
	cout << "\nenter number of hours: ";
	cin >> destination.travelTime.hr;
	cout << "\nenter time in minutes: ";
	cin >> destination.travelTime.min;
}
struct tourType destination;

you don't need the word struct in c++. It works, but the syntax there is antique.

main does not call the tourInit function. you need to call it.

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

void tourInit(struct tourType* tour); //don't forget a header. 

int main()
{
        tourInit(&destination); //this would work.  but it would be better to make
// the function pass by reference and drop the pointers unless you have a plan.
	input();
	output();
	
	system("pause");
	return 0;
}


//B: store tour data for Chicago
void tourInit(struct tourType* tour)
{

	tour->cityName = "Chicago";
	tour->distance = 550;
	tour->travelTime.hr = 9;
	tour->travelTime.min = 30;
	
}

Last edited on
Thank you for the prompt assistance jonnin!

when you say get rid of the pointer - I can remove the * without any issues correct? I do not have a plan for it.

Thank you.
yes, use a reference instead if you don't need a pointer.
that looks like this

void tourInit(struct tourType& tour)

and you can call it with
tourInit(destination); //don't need address of anymore

this can be confusing because of & reuse.
it would be in your long term best interest to get a great handle on the difference between address of and references.
foo( &x); //address of passed into a function.
int foo(type &x); //reference parameter
int &x = y; //reference.
int *ip = &x; //address of...
see the pattern? something uses or is assigned from &x, its usually address of. Declarations with & in them are references. It can really get nasty if you have references and pointers and bitwise logic all in one statement. Try not to do that, for the sake of any readers of your code (including yourself 6 months later).
Last edited on
Using references is useful for all of your functions so you no longer need a global variable:
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
#include <iostream>
#include <string>

struct timeType
{
   int    hr;
   double min;
   int    sec;
};

struct tourType
{
   std::string cityName;
   int         distance;
   timeType    travelTime;
};

void tourInit(tourType&);
void input(tourType&);
void output(tourType&);

int main()
{
   // declare your destination variable in main instead of globally
   tourType destination;

   input(destination);
   output(destination);
}

void tourInit(tourType& tour)
{
   tour.cityName       = "Chicago";
   tour.distance       = 550;
   tour.travelTime.hr  = 9;
   tour.travelTime.min = 30;
}

void input(tourType& destination)
{
   std::cout << "\nenter destination city: ";
   std::cin >> destination.cityName;
   std::cout << "\nenter distance in miles: ";
   std::cin >> destination.distance;
   std::cout << "\nenter number of hours: ";
   std::cin >> destination.travelTime.hr;
   std::cout << "\nenter time in minutes: ";
   std::cin >> destination.travelTime.min;
}

void output(const tourType& destination)
{
   std::cout << destination.cityName << " "
      << destination.distance << " "
      << destination.travelTime.hr << " "
      << destination.travelTime.min << '\n';
}

One thing to be aware of when using std::cin to get a std::string.

If the city's name being entered is two or more words, (St. Paul for instance) only the first part is retrieved. Leaving the rest of the string still in the input stream for the next std::cin to extract. It is now expecting numbers. Your input queue will not work as expected.
Thank you both for the input! I did do some reading on the reference last night and like usual its best learned if I put it to use.

for the std::cin - what is the best way to handle that if you have 2 words for a city? There are a lot of them out there and I did try it last night with the exact same results you described Furry Guy.

I truly appreciate both of you taking your time in answering my questions!
you can use getline. If you mix getline with << you need to be careful, look up how to handle that... using cin.ignore(). eg http://www.cplusplus.com/forum/general/51433/ or many other posts...
Last edited on
Topic archived. No new replies allowed.