سؤال

I am having trouble with a program that uses void functions. I have never used them before so I'm a bit lost. My program has a 3 sets of cities. It is supposed to get the three cities in one of the sets and figure out how long the flight is. My problem is that I keep getting the error that my variables are undefined. This is the first program I have tried using void functions. I have tried initializing every variable by itself but I don't think that is the correct way to do it or is it? Any help would be appreciated. Here is my code:

#include <fstream>
#include <iostream>
#include <string>

using namespace std;
void readFile (int wall1, double &lat1, double &lon1, string &city1,
                       double &lat2, double &lon2, string &city2,
                       double &lat3, double &lon3, string &city3);
void intro();
void askDataSet(int &w);

//--------------------------------------------------
int main()
{

    intro();
    askDataSet(int &w);
    string name;


    int lat1, lat2, lat3, , lon1, lon2, lon3, beginLat, beginLon, beginCity, midLat,  midLon, midCity, endLat, endLon, endCity;
    string city1, city2, city3;
    readFile (beginLat, beginLon, beginCity, midLat, midLon, midCity, endLat, endLon, endCity);
    cout << "The First City at coordinates " << beginLat << " and " << lon1 << " is:  " << city1 << endl;
    cout << "The Second City is at coordinates " << beginLat << " is " << lon2 << ":  " << city2 << endl;
    cout << "The Third City is at coordinates " <<  beginLat << " is " << lon3 << ":  " << city3 << endl;

    leg1 = dist( beginLat, beginLon, midLat, midLon);
    leg2 = dist( midLat, midLon, endLat, endLon);
    nonstop = dist( leg1-leg2 );

    cout << "It is " << dist << "fewer miles for non-stop" << endl;


   system("pause");
   return 0;
 }

 void readFile (int &wall1, double &lat1, double &lon1, string &city1,
                 double &lat2, double &lon2, string &city2,
                 double &lat3, double &lon3, string &city3)
{
   ifstream dataIn;
   dataIn.open("cities.txt");
   if(dataIn.fail())
{
    cout << "Error.  File does not exist. " << endl;
    exit(1);
}

    dataIn >> lat1;
    dataIn >> lon1;
    dataIn.get();
    getline(dataIn, city1);
    dataIn >> lat2;
    dataIn >> lon2;
    dataIn.get();
    getline(dataIn, city2);
    dataIn >> lat3;
    dataIn >> lon3;
    dataIn.get();
    getline(dataIn, city3);

}

    void intro()
    {
    cout << "In this lab we will try to figure out how much shorter it is to fly non-stop compared to one-stop." << endl;
    cout << endl;
    }

    void askDataSet(int &w)
    {
     cout << "Which set of cities would you like to figure the distances for? " << endl; 
     cin >> w;
    }
هل كانت مفيدة؟

المحلول

To give back a value from a void function via a reference-parameter, a variable has to exist in the caller's scope which is given as the parameter.

So in your case, for example to call askDataSet, you first have to declare an int to hold the result:

int w;
askDataSet(w);

Then askDataSet will write into your integer variable and you can use it after the call.

Further points I noticed with the code:

  • For the readFile call, the variables passed must have the same type as the parameters (double, not int).
  • There is an extra comma in the declarations before the readData call: "lat3, , lon1"
  • Maybe you'll want to replace beginLat, midLon etc. with lat1, lon2 (or the other way around).
  • readFile seems to have an unused first argument int wall1, which is missing in the readFile-call in main.
  • The dist function is not defined and at the end of main you try to print it.
  • To use system and exit, you should #include <cstdlib>.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top