-
November 7th, 2009, 04:20 PM
#16
Re: Functions Problem
You are still not using a function. You use a return statement, you have no function except main to return from. Your book or lecture notes must have an example for a function. Read it. Understand it. Then look again at Post #3 above.
You are still having a condition while (target != SENTINEL) but don't change either value in the loop. The condition can thus be only "always true" or "always false" thus making little sense.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
November 11th, 2009, 09:32 PM
#17
Re: Functions Problem
After days and days of slaving, I think I finally have something that works. You guys see any corrections I could make to this?
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string findName(int parkingSticker);
int main ()
{
int target = 0;
const double SENTINEL = 000000;
//string functionMessage = "";
cout << "Please enter a parking sticker number (enter 000000 to exit): " << endl;
cin >> target;
while (target != SENTINEL)
{
if (findName (target) == "NOT FOUND")
{
cout << "This parking sticker is not on file. Try again." << endl << endl;
}
else
{
cout << "This parking sticker belongs to " << findName(target) << endl << endl;
}
cout << "Please enter a parking sticker number (enter 000000 to exit): " <<endl;
cin >> target;
}
system ("pause");
return 0;
}
string findName (int parkingSticker)
{
string name;
int tempNum;
string returnValue = "";
ifstream input;
input.open("HWK5.txt");
input >> name >> tempNum;
while (!input.eof())
{
if (parkingSticker == tempNum)
{
returnValue = name;
break;
}
else if (parkingSticker != tempNum)
{
input >> name >> tempNum;
}
}
if (returnValue == "")
{
returnValue = "NOT FOUND";
}
return returnValue;
}
-
November 11th, 2009, 09:49 PM
#18
Re: Functions Problem
Originally Posted by Ruined Embrace
You guys see any corrections I could make to this?
Not corrections, but a misunderstanding of what a double means:
Code:
const double SENTINEL = 000000;
Those extra 0's do nothing except wear out your keyboard. This is exactly the same as this:
Code:
const double SENTINEL = 0.0;
Why is SENTINEL a double anyway? Since target is an int, then SENTINEL should be an int.
Regards,
Paul McKenzie
-
November 12th, 2009, 12:35 AM
#19
Re: Functions Problem
I'm not sure man. My professor is kind of strange. He demands we use const double SENTINEL = 000000
-
November 12th, 2009, 05:26 AM
#20
Re: Functions Problem
Code:
input >> name >> tempNum;
while (!input.eof())
{
if (parkingSticker == tempNum)
{
returnValue = name;
break;
}
else if (parkingSticker != tempNum)
{
input >> name >> tempNum;
}
}
A better way to write this would be:
Code:
while (input >> name >> tempNum)
{
if (parkingSticker == tempNum)
{
returnValue = name;
break;
}
}
Another improvement I could think of would be to initialize returnValue with "NOT FOUND" and not with an empty string. This would save the check after the loop.
Last edited by treuss; November 12th, 2009 at 05:29 AM.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
November 12th, 2009, 05:51 AM
#21
Re: Functions Problem
I suppose that if you were not concerned about trying for a single point of return, the returnValue variable could be easily eliminated:
Code:
while (input >> name >> tempNum)
{
if (parkingSticker == tempNum)
{
return name;
}
}
return "NOT FOUND";
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
|