I'm having trouble with this homework problem. I know I'm close to having it solved but I can't get the loop to stop. It finds the correct name to the parking sticker in the text file but keeps looping over and over. Here's the problem for a better understanding:
Quote:
Assume that a file named HWK5.txt has a single name (last name only -- no first name or middle...
The bold part of the assignment means, you should write a function like this:
Code:
std::string find(const std::string& sticker) {
// open file
// search file for sticker
// if found, return lastname
// if not found, return "NOT FOUND"
}
In turn, your main function should be reduced to:
Code:
int main()
{
const double SENTINEL = 000000;
for (;;) {...
Quote:
Originally Posted by nuzzle
Just a note. It's generally not a very good idea to compare floats and doubles for equality using ==.
True and the parking sticker should be neither one nor an int. It should be a string !!
Thanks guys for the responses but I'm still not getting this so I asked for help from my professor. This is what he had to say:
Quote:
The variable parkingSticker should be declared a double, not a string. You should also have the while loop for the sentinel as the outside loop, not the inside loop. The sentinel-controlled loop should have everything else in it, ...
Some hints:
Code:
while (target != SENTINEL)
The condition for your while loop compares target and SENTINEL. Neither one of the variable is changed inside the while loop. Thus the loop, if entered, does not have a possibility to terminate.
Code:
while (! input.eof())
{
input >> lastName >> parkingSticker;...
Quote:
Originally Posted by Ruined Embrace
This is what he had to say:
Quote:
Originally Posted by professor
The variable parkingSticker should be declared a double, not a string.
Ask him why! So you can divide two parking sticker ...
Quote:
Originally Posted by Ruined Embrace
The variable parkingSticker should be declared a double, not a string
Sometimes, I wonder where in the world these teachers are coming from.
Quote:
Just wondering how I'm suppose to stop it from looping over and over and not display the ...
Quote:
Originally Posted by treuss
Some hints:
Code:
while (target != SENTINEL)
The condition for your while loop compares target and SENTINEL. Neither one of the variable is changed inside the while loop. Thus the loop, if entered, does not have a possibility to terminate.
I have to keep ...
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 ...
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...
Quote: 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
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";
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...
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";
Related threads on "CodeGuru Forums - A Developer.com Community for C++, C#, VB, Java and .NET Solutions":
Thread profile page for "Functions Problem" on http://www.codeguru.com.
This report page is a snippet summary view from a single thread "Functions Problem", located on the Message Board at http://www.codeguru.com.
This thread profile page shows the thread statistics for: Total Authors, Total Thread Posts, and Thread Activity