Posts Topics Forums Images
Search videos from message boards Videos Search messages from microblogs Microblogs Search messages from imdb.com Imdb Search messages from yuku.com Yuku Search messages from lefora.com (free forums) Lefora
My account: Login | Sign Up
Loading... 

Thread: Functions Problem

Started 1 month, 3 weeks ago by Ruined Embrace
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...
Site: CodeGuru Forums - A Developer.com Community for C++, C#, VB, Java and .NET Solutions  CodeGuru Forums - A Developer.com Community for C++, C#, VB, Java and .NET Solutions - site profile
Forum: C++ (Non Visual C++ Issues)  C++ (Non Visual C++ Issues) - forum profile
Total authors: 8 authors
Total thread posts: 18 posts
Thread activity: no new posts during last week
Domain info for: codeguru.com

Other posts in this thread:

Ruined Embrace replied 1 month, 3 weeks ago
Code: #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string lastName; double number, target, parkingSticker; const double SENTINEL = 000000; cout << "Please enter a parking sticker number (enter 000000 to exit): " << endl; cin >> target; while (target != SENTINEL) { ifstream input("HWK5.txt");...

treuss replied 1 month, 3 weeks ago
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 (;;) {...

nuzzle replied 1 month, 3 weeks ago
Just a note. It's generally not a very good idea to compare floats and doubles for equality using ==.

treuss replied 1 month, 3 weeks ago
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 !!

Ruined Embrace replied 1 month, 2 weeks ago
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, ...

treuss replied 1 month, 2 weeks ago
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;...

treuss replied 1 month, 2 weeks ago
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 ...

Paul McKenzie replied 1 month, 2 weeks ago
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 ...

Ruined Embrace replied 1 month, 2 weeks ago
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 ...

Ruined Embrace replied 1 month, 1 week ago
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 ...

 

Top contributing authors

Name
Posts
Ruined Embrace
6
user's latest post:
Functions Problem - Page 2 -...
Published (2009-11-12 00:35:00)
I'm not sure man. My professor is kind of strange. He demands we use const double SENTINEL = 000000
treuss
5
user's latest post:
Functions Problem - Page 2 -...
Published (2009-11-12 05:26:00)
Code: input &gt;&gt; name &gt;&gt; tempNum; while (!input.eof()) { if (parkingSticker == tempNum) { returnValue = name; break; } else if (parkingSticker != tempNum) { input &gt;&gt; name &gt;&gt; tempNum; } } A better way to write this would be: Code: while (input &gt;&gt; name &gt;&gt; tempNum) { if (parkingSticker == tempNum) { returnValue = name; break; } } Another improvement I could think...
Paul McKenzie
2
user's latest post:
Functions Problem - Page 2 -...
Published (2009-11-11 21:49:00)
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
nuzzle
1
user's latest post:
Functions Problem
Published (2009-11-03 04:12:00)
Just a note. It's generally not a very good idea to compare floats and doubles for equality using ==.
laserlight
1
user's latest post:
Functions Problem - Page 2 -...
Published (2009-11-12 05:51:00)
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 &gt;&gt; name &gt;&gt; tempNum) { if (parkingSticker == tempNum) { return name; } } return &quot;NOT FOUND&quot;;
Ruined Embrace Ruined Embrace is...
1
user's latest post:
Functions Problem - Page 2 -...
Published (2009-11-12 00:35:00)
I'm not sure man. My professor is kind of strange. He demands we use const double SENTINEL = 000000
treuss's Avatar
1
user's latest post:
Functions Problem - Page 2 -...
Published (2009-11-12 05:26:00)
Code: input &gt;&gt; name &gt;&gt; tempNum; while (!input.eof()) { if (parkingSticker == tempNum) { returnValue = name; break; } else if (parkingSticker != tempNum) { input &gt;&gt; name &gt;&gt; tempNum; } } A better way to write this would be: Code: while (input &gt;&gt; name &gt;&gt; tempNum) { if (parkingSticker == tempNum) { returnValue = name; break; } } Another improvement I could think...
laserlight laserlight is offline...
1
user's latest post:
Functions Problem - Page 2 -...
Published (2009-11-12 05:51:00)
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 &gt;&gt; name &gt;&gt; tempNum) { if (parkingSticker == tempNum) { return name; } } return &quot;NOT FOUND&quot;;

Related threads on "CodeGuru Forums - A Developer.com Community for C++, C#, VB, Java and .NET Solutions":

Related threads on other sites:

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