-
May 11th, 2010, 11:01 PM
#1
How to I return multiple values in a function
OK, I a program that takes in data from a file and returns messages based upon it. Right now my function shot is supposed to be a multiple value returning function but I don't know how to return multiple values because it says that its not a void type variable. Any help on this would be great.
Code:
/Patrick Howard
/*
*/
//May 12, 2010
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>
// declared constants
const float PI = 3.14159265f;
const float NUM = 180.0f;
const int AIRCRAFT_CARRIER = 3030; // targets locations in meters
const int BATTLESHIP = 2000; // targets locations in meters
const int DESTROYER = 1900; // targets locations in meters
const int SUBMARINE = 1000; // targets locations in meters
int pv = 0;//public variable to test to see if it got hit or not
int ahit = 6;
int bhit = 5;
int dhit = 3;
int subhit = 2;
using namespace std;
// functions prototype
float calcRadians(int deg);
float fDistance(float , float vel);
void shot(bool &isHit, float &distance);
void print(string fileName, int falseData, int shots, int hits, int missed, int reload);
int main()
{
//declare number of hits for each ship
int aircraftHits = 6; // max number of hits for known targets
int battleshipHits = 5; // max number of hits for known targets
int destroyerHits = 3; // max number of hits for known targets
int submarineHits = 2; // max number of hits for known targets
int numShots = 0;
int numHits = 0;
int misses = 0;
int rounds = 0;
int amo = 2;
ifstream din;
string name;
bool hit = false;
bool badData = false;
int data = 0;
int degrees; // the angle in degrees
float velocity; // the initial velocity
float radians; // the radians
float distance; // the distance
do
{
cout << "Please enter the input file name. " << endl; // file name is entered
cin >> name; // name printed
din.open(name.c_str()); // file opened
if(!din.is_open()) // make sure file is real
{
cout << name << " is an invalid file name." <<endl; // if not found it will state that the file is invalid
din.clear();
}
}
while(!din.is_open());
{
while(din)
{
din >> degrees >> velocity;
if(degrees <= 0 && velocity <= 0)
{
cout << "Bad data." << endl;
data ++;
}
else if(degrees <= 0)
{
cout << "Bad data." << endl;
data ++;
}
else if(velocity <= 0)
{
cout << "Bad data." << endl;
data ++;
}
else
{
din.ignore(80,'\n');
radians = calcRadians(degrees);
//cout << radians << endl;
distance = fDistance(velocity, radians);
//cout << distance << endl;
shot(hit, distance);
numShots++;
if(pv == 1)
numHits ++;
else
misses ++;
amo = amo - 1;
if(amo == 0)
{
rounds++;
amo = 2;
}
pv = 0;
}
}
}
print(name, badData, numShots, numHits, misses, rounds);//numhits6 misses2
return 0;
}
float calcRadians(int deg)
{
float numerator;
float rad;
numerator = deg * PI;
rad = numerator / NUM;
//cout << rad << endl;
return rad;
}
float fDistance(float vel,float radians)
{
float Distance;
Distance = (float)(((vel * vel)*(sin(2*radians))/32.2));
//cout << Distance << endl;
return Distance;
}
void shot(bool &isHit, float &distance)
{
if(distance >= (AIRCRAFT_CARRIER - 1) && distance <= (AIRCRAFT_CARRIER + 1))
{
isHit = true;
ahit = ahit - 1;
pv = 1;
}
else if (distance >= (-1 + BATTLESHIP) && distance <= (1 + BATTLESHIP))
{
isHit = true;
bhit = bhit - 1;
pv = 1;
}
else if(distance >= (-1 + DESTROYER) && distance <= (1 + DESTROYER))
{
isHit = true;
dhit = dhit - 1;
pv = 1;
}
else if(distance >= (-1 + SUBMARINE) && distance <= (1 + SUBMARINE))
{
isHit = true;
subhit = subhit - 1;
pv = 1;
}
return;
}
void print(string fileName, int falseData, int reload, int hits, int missed)//num amo, num hits, num miss, percent, num of lines of bad data
{
float percentage = 0;
ofstream dout;
cout << "Please enter the output file name. " << endl;
cin >> fileName;
dout.open(fileName.c_str());
percentage = ((float)reload / hits);
dout << "The number rounds of ammo were expended: " << reload << endl;
dout << "The total number of hits overall: " << hits << endl;
dout << "The total number of misses were: " << missed << endl;
dout << "The percentage of hits to rounds expended: " << percentage << endl;
dout << "The number of lines of bad data in the file: " << falseData << endl;
dout.close();
}
-
May 11th, 2010, 11:28 PM
#2
Re: How to I return multiple values in a function
Originally Posted by patsies62
OK, I a program that takes in data from a file and returns messages based upon it. Right now my function shot is supposed to be a multiple value returning function but I don't know how to return multiple values
You cannot return multiple values in C++.
Instead learn how to use structs or classes. Right now, your code has no use of these types. A struct/class holds multiple values, therefore if you return a single struct or class, you are returning "multiple values".
Regards,
Paul McKenzie
-
May 11th, 2010, 11:38 PM
#3
Re: How to I return multiple values in a function
If you only need to return two values, you can use a std:: pair.
-
May 12th, 2010, 07:24 AM
#4
Re: How to I return multiple values in a function
Another option is to use output parameters. Pass in parameters by reference and populate them inside the function.
-
May 12th, 2010, 07:40 AM
#5
Re: How to I return multiple values in a function
As Paul mentioned, structs is probably the easiest way.
in header file:
typedef struct tagMYSTRUCT {
bool bHello;
int nHello;
CString csHello;
} MYSTRUCT;
in Source file
MYSTRUCT MyFunction(int nParams)
{
MYSTRUCT myStruct;
myStruct.bHello = true;
myStruct.nHello = 512;
myStruct.csHello.Format("Hi!");
return myStruct;
}
-
May 12th, 2010, 08:44 AM
#6
Re: How to I return multiple values in a function
Originally Posted by Anarchi
As Paul mentioned, structs is probably the easiest way.
in header file:
typedef struct tagMYSTRUCT {
bool bHello;
int nHello;
CString csHello;
} MYSTRUCT;
in Source file
MYSTRUCT MyFunction(int nParams)
{
MYSTRUCT myStruct;
myStruct.bHello = true;
myStruct.nHello = 512;
myStruct.csHello.Format("Hi!");
return myStruct;
}
I'd argue that ALLCAPSNAMES should be reserved for constants. Also, the "typedef struct" definition is unnecessary in C++ (though there is apparently a minor benefit to it, in that it will prevent accidental name hiding in a few rare cases).
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|