Cannot get Program to cin or cout my stuct variable.

I cannot get this to cin or cout anything. It will compile successfully but it's not doing what I'm defining it to do. Anybody got any ideas.

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

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

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

tourType destination;

int main ()
{
    void input();
    void output();
    
    return 0;
}

void output(tourType destination)
{
    cout << destination.cityName << " "
         << destination.distance << " "
         << destination.travelTime.hr << " "
         << destination.travelTime.min << " "
    << endl;
}
void input(tourType destination)
{
    cout << "\nenter city: ";
    cin >> destination.cityName;
    cout << "\nenter distance: ";
    cin >> destination.distance;
    cout << "\nenter time: ";
    cin >> destination.travelTime.hr;
    cout << "\nenter time in minutes: ";
    cin >> destination.travelTime.min;
}

Thats because you declare your functions inside the main function

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

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

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

tourType destination;

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

int main ()
{
    input();
    output();

    return 0;
}

void output(void)// also you dont need the parameter tourType destination
{
    cout << destination.cityName << " "
         << destination.distance << " "
         << destination.travelTime.hr << " "
         << destination.travelTime.min << " "
         << endl;
}

void input(void)
{
    cout << "\nenter city: ";
    cin >> destination.cityName;
    cout << "\nenter distance: ";
    cin >> destination.distance;
    cout << "\nenter time: ";
    cin >> destination.travelTime.hr;
    cout << "\nenter time in minutes: ";
    cin >> destination.travelTime.min;
}
You made a very common beginners mistake. You forgot to prototype your functions outside the main function. Then, call them within your main. Also, I would move your tourType destination within your main, otherwise you create a global variable (not good practice to have global variables).
Could someone explain to me the idea of parameters of a function. I'm very confused when you include parameters and when you don't. I'm just completely lost on when I need them. I know when I want to print something out I don't need to pass anything in to the function. However, i don't understand what "passing" means. I've flipped back to the functions chapter in my c++ programming book but still cannot get that light bulb to come on in my head.

It would be nice if someone could explain what goes in these ( ).

Also I've been told that when I'm creating a function that prints something I don't need it to pass anything or return anything. I just don't understand when I need to return or use the void. it seems to me that if i'm going to assign something in a function i should return that to the program so the program knows what I just assigned.
This is how I understand them:

1. Function Prototypes
These are not strictly necessary. They are only needed if you define a function below where it is used in your code. So if you use a function in main, it better either be defined before main or else have a prototype before main. A function prototype consists of the return type of the function, the name of the function, and the data types of the parameters, with the parameter names as optional. Example of a function prototype:

int sum(int, int);

2. Function Definition
This is what you call the code that makes up your function. It starts out like a function prototype, but you must have names for the parameters. You can also give default values for the parameters if you want. Example of a function definition:

1
2
3
4
5
int sum(int a = 0, int b = 0) 
//so if the function is called without passing any arguments, a and b are both 0
{
    return a + b;
}


3. Function Calls
This is the piece of code that actually tells the program to use your function. Example of a function call passing x and y as arguments:
SumOfXAndY = sum(x, y);

The stuff in the parentheses are called arguments when it's a function call, and parameters when it's a function definition or function prototype.
Topic archived. No new replies allowed.