CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    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.

  2. #17
    Join Date
    Nov 2009
    Posts
    8

    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;
    
    }

  3. #18
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Functions Problem

    Quote Originally Posted by Ruined Embrace View Post
    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

  4. #19
    Join Date
    Nov 2009
    Posts
    8

    Re: Functions Problem

    I'm not sure man. My professor is kind of strange. He demands we use const double SENTINEL = 000000

  5. #20
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    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.

  6. #21
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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";
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured