Contact List pt. 2

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hunderpanzer
    New Member
    • Jul 2007
    • 60

    Contact List pt. 2

    I've been working on that "contact list" on and off now, and now I'm really trying to get it complete.

    I'm trying to reduce the clutter right now, and make it more readable. All the braces and if, else statements are getting pretty big, so I tried putting them in functions.

    I declared it, called it and defined it properly ( I hope) but it doesn't seem to be working. The program Compiles fine, no errors or warnings. But when the time comes to call the function, it doesn't call for some reason.

    [CODE=cpp]string option_one(){

    string pass1, pass2;
    cout << "\n\n\t Enter your new password:.";
    cin.ignore();
    getline(cin, pass1);
    cout << "\n\n\t Enter your new password once more (Verification Purposes):.";
    cin.ignore();
    getline(cin, pass2);
    if (pass1 == pass2){
    Realpass = pass1; //Realpass is defined globally
    cout << "\n\n Password has been changed successfully! ";
    }
    if (pass1 != pass2){
    cout << "\n\n The two passwords did not match :'[ ";
    }
    return 0;
    }
    [/CODE]

    Can a function just be made of a bunch of ifs, else, couts and getlines ?

    My guess is, that there's no such thing as a string function? I tried even using a void function, but I get the same results.


    I also understand that this function is pointless since it doesn't actually store the new Realpass into a file, but I'll get that tackled sooner or later. Right now, I just want to get the menus set up correctly.

    I apologize for the trouble.
    Last edited by Hunderpanzer; Aug 17 '07, 09:37 PM. Reason: one more thing...
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    The compiler is not stopping the subroutine from being called. The contents of a method does not dictate whether it is called or not (even though it looks like that is happening).

    Your problem probably lies with the logic of your code. This is where a debugger can help. That or surround the suspect area with lots of print statements. The logic is there, you just have to dig it out.

    Comment

    • Hunderpanzer
      New Member
      • Jul 2007
      • 60

      #3
      Originally posted by RRick
      The compiler is not stopping the subroutine from being called. The contents of a method does not dictate whether it is called or not (even though it looks like that is happening).

      Your problem probably lies with the logic of your code. This is where a debugger can help. That or surround the suspect area with lots of print statements. The logic is there, you just have to dig it out.

      Bah, I'm sorry. I just don't know what's going wrong here.

      I made a new program with a simple function with getlines and couts.
      It did the same thing. I used cin.ignore( ) incase a newline was making act funny. .
      I tried changing all class string types, to char* strings, and using atof(s) and atoi(s). . .

      I tried using the debugger from my compiler but I dont seem to know how to get it to work properly.


      Sorry, but I'm out of ideas.

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by Hunderpanzer
        Bah, I'm sorry. I just don't know what's going wrong here.

        I made a new program with a simple function with getlines and couts.
        It did the same thing. I used cin.ignore( ) incase a newline was making act funny. .
        I tried changing all class string types, to char* strings, and using atof(s) and atoi(s). . .

        I tried using the debugger from my compiler but I dont seem to know how to get it to work properly.


        Sorry, but I'm out of ideas.
        How are you calling the function? What makes you think the function isn't called? Also, in the code above you, i fyou return 0 you should declare it like this:
        [code=cpp]
        int option_one();
        [/code]

        Comment

        • Hunderpanzer
          New Member
          • Jul 2007
          • 60

          #5
          Originally posted by ilikepython
          How are you calling the function? What makes you think the function isn't called? Also, in the code above you, if you return 0 you should declare it like this:
          [code=cpp]
          int option_one();
          [/code]

          Well I think it's not being called because when I call the function by doing:

          [CODE=cpp]
          if (choice == 1){
          cout << " it sHOULD work";
          string option_one();
          }

          else{
          cout <<" Pardon Me ? " << endl << endl;
          goto Menu;
          }
          [/CODE]

          When the program is run, I press 1, and it prints
          it sHOULD work Pardon Me?

          the function should have opened up some more options (see above)

          Also, I've tried using
          Code:
          int option()
          (and changed the corresponding call and prototype) but it does the exact same thing.

          Just now I thought of an idea to test if it was being called by using

          Code:
          system("pause");
          at the beginning of the function definition.
          I get the same response. - it sHOULD work Pardon Me ?

          Maybe I'm calling it wrong? My book doesn't explain returns and calling all that well.
          It uses
          Code:
           return 0;
          throughout the book.
          Last edited by Hunderpanzer; Aug 19 '07, 02:32 AM. Reason: spelling*

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by Hunderpanzer
            Well I think it's not being called because when I call the function by doing:

            [CODE=cpp]
            if (choice == 1){
            cout << " it sHOULD work";
            string option_one();
            }

            else{
            cout <<" Pardon Me ? " << endl << endl;
            goto Menu;
            }
            [/CODE]

            When the program is run, I press 1, and it prints
            it sHOULD work Pardon Me?

            the function should have opened up some more options (see above)

            Also, I've tried using
            Code:
            int option()
            (and changed the corresponding call and prototype) but it does the exact same thing.

            Just now I thought of an idea to test if it was being called by using

            Code:
            system("pause");
            at the beginning of the function definition.
            I get the same response. - it sHOULD work Pardon Me ?

            Maybe I'm calling it wrong? My book doesn't explain returns and calling all that well.
            It uses
            Code:
             return 0;
            throughout the book.
            Yes, you are calling the function wrong. To call it just do:
            [code=cpp]
            option_one();
            [/code]
            The return type is only written when you are declaring the function. Remember:
            Code:
            int some_function(int paramint1, floar paramfloat1);
            1         2                     3 
            1 -- return type of function
            2 -- name of function
            3 -- parameter list
            By doing this:
            [code=cpp]
            string option_one();
            [/code]
            You are probably delcaring a string called option_one.

            Also, the return type should be the type of value you are using after the return keyword. If you return 0, your function should be "int func();". If you return str and str is a string type, you function should be "string func();".

            Comment

            • Hunderpanzer
              New Member
              • Jul 2007
              • 60

              #7
              Originally posted by ilikepython
              Yes, you are calling the function wrong. To call it just do:
              [code=cpp]
              option_one();
              [/code]
              The return type is only written when you are declaring the function. Remember:
              Code:
              int some_function(int paramint1, floar paramfloat1);
              1         2                     3 
              1 -- return type of function
              2 -- name of function
              3 -- parameter list
              By doing this:
              [code=cpp]
              string option_one();
              [/code]
              You are probably delcaring a string called option_one.

              Also, the return type should be the type of value you are using after the return keyword. If you return 0, your function should be "int func();". If you return str and str is a string type, you function should be "string func();".


              Wow I really appreciate your explanation of functions !

              You were exactly correct. I just had to erase string from the function call !

              All that time ! bah !


              Thank you so much, things are starting to roll faster now.

              Comment

              Working...