What would be a method to call a user input from one function to the main function?

I'm trying to do a project for school, and I'm a little stuck. I have to simulate gas consumption of a car on a road trip under different scenarios (e.g.: weather conditions, road conditions, time of day, location etc.), with each scenario in its own function, and calling the function from the main function. Part of what I have is something like this:

Select road condition (1) Flat (2) Gravelly (3) Hilly:

Then the user will type either 1, 2, or 3 to indicate the condition they want to select. The problem is, I have no idea how I should call the user input to the main function, and then display the results of each scenario and how that affects the gas consumption at the end when the user input is completed. I was thinking of using pointers to call the user input to the main function, and using if else statements for each condition, e.g.:
1
2
3
4
if (roadCond == 1) 
{
cout << "You selected: Flat road.";
}


But I am completely stuck.
Anyways, any help would be greatly appreciated. I've done a lot of Googling, but I can't seem to find a solution (or at least a solution that I have been taught).
Read http://www.cplusplus.com/doc/tutorial/functions/
Did you see there that a function can return a value?
I've tried that, but I still can't get the program to work like I want it to . I can get it to work by having the user select a scenario, with the results right afterwards, but I was hoping to get the user to select a scenario from each condition, then at the end display the results of each selected scenario.


This is what I have so far:

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

void results();

int main()
{
    cout << "= = = = = = = = = = = = = = = = = = = = = = = = = = = =" << endl;
    cout << "# - - - >> Welcome to the driving simulator! << - - - #" << endl;
    cout << "= = = = = = = = = = = = = = = = = = = = = = = = = = = =" << endl;
    cout << endl;
    
    cout << "Select a city by typing its name:\n(1) Toronto\n(2) Sydney\n(3) Berlin\nCity: " << endl;
    cout << endl;
    cout << "Select a car:\n(1) 2005 Toyota Echo\n(2) 2011 MINI Cooper\n(3) 2006 Ferrari F430\nCar: " << endl;
    
    results();
   
    
    return 0;
}

int city()
{
    int inputCity;
    cin >> inputCity;
    cout << endl;
    
    return inputCity;
}

int car()
{
    int inputCar;
    cin >> inputCar;
    cout << endl;
    
    return inputCar;
}

void results()
{
    int cityResults = city();
    if (cityResults == 1)
    {
        cout << "City: Toronto." << endl;
    }
    else if (cityResults == 2)
    {
        cout << "City: Sydney." << endl;
    }
    else if (cityResults == 3)
    {
        cout << "City: Berlin." << endl;
    }
    cout << endl;
    
    int carResults = car();
    if (carResults == 1)
    {
        cout << "Car: 2005 Toyota Echo." << endl;
    }
    else if (carResults == 2)
    {
        cout << "Car: 2011 MINI Cooper." << endl;
    }
    else if (carResults == 3)
    {
        cout << "Car: 2006 Ferrari F430." << endl;
    }
}
Last edited on
Here's an example for 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
#include <fstream>
#include <iostream>
#include <string.h>

using namespace std;

string myString()
{
	return "hello ";
}

string myName()
{
	return "bob";
}

int main (int argc, char *argv[])
{
	string myData;
	myData=myString()+myName();
	cout << myData;
	
return 0;
}
SamuelAdams, you included <string.h> when clearly you meant <string>.


Here's an example.

You can see that the returned strings must be stored in a variable to make any use of them.

You can then use the strings stored in the variables to output text to the user dynamically, instead of having to type the same stuff several times.


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

std::string get_city();
std::string get_car();

int main()
{
  std::cout << "= = = = = = = = = = = = = = = = = = = = = = = = = = = =\n"
               "# - - - >> Welcome to the driving simulator! << - - - #\n"
               "= = = = = = = = = = = = = = = = = = = = = = = = = = = =\n"
            << std::endl;

  std::string city = get_city();

  std::cout << "\n\n" << city << " is an excellent choice!" << std::endl;

  std::string car = get_car();

  std::cout << "\nYou will be driving a " << car << " on the streets of "
            << city << "!" << std::endl;
}

std::string get_city()
{
  std::cout << "Select a city by typing it's coresponding number and pressing enter:\n"
               "\n"
               "1) Toronto, Canada\n"
               "2) Sydney, Australia\n"
               "3) Berlin, Germany\n" << std::endl;

  while(true)
  {
    int selection;
    std::cin >> selection;

    switch (selection)
    {
      case 1:
        return "Toronto";

      case 2:
        return "Sydney";

      case 3: 
        return "Berlin";

      default:
        std::cout << "Invalid selection... Please choose from 1-3.\n" << std::endl;
    }
  }
}

std::string get_car()
{
  std::cout << "Select a car by typing it's coresponding number and pressing enter:\n"
               "\n"
               "1) 2005 Toyota Echo\n"
               "2) 2011 Mini Cooper\n"
               "3) 2006 Ferrari F430\n" << std::endl;

  while(true)
  {
    int selection;
    std::cin >> selection;

    switch (selection)
    {
      case 1:
        return "2005 Toyota Echo";

      case 2:
        return "2011 Mini Cooper";

      case 3: 
        return "2006 Ferrari F430";

      default:
        std::cout << "Invalid selection... Please choose from 1-3.\n" << std::endl;
    }
  }
}
Last edited on
Hydranix, that's great, thank you! I've been racking my brain while trying to study for exams, but seeing the code now makes me realize that I was making things harder than it actually was.
Topic archived. No new replies allowed.