Functions!

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • So I've been working with functions today, they are not too difficult but I wanted to try and write a name change function just to see if I could.. whelp here it is:

      Source Code

      1. void changeName(char name[])
      2. {
      3. if (!strcmp(name, "Hayden"))
      4. {
      5. cout << "Congratulations! Welcome to nowhere ville." << endl;
      6. } else {
      7. strcpy(newName, "Hayden"));
      8. cout << endl << "Sorry but that name was taken and you are known as " << newName << " now." << endl;
      9. }
      10. }


      Not really useful outside of just practicing, I'm sure there are probably better ways to do this also. But now I'm thinking.. when you choose a character name for a game, you could compare the input to an array of 'bad words' and prevent a player from getting the name, or if a name is already taken.

      That's all I have time for tonight, later all!
      You may call me char 71 97 100 100 97 109.
    • Hey Gaddam,

      One immediate flaw I see in that code is that you don't allocate "newName" anywhere, so that won't compile :) Otherwise that will work, however it doesn't actually modify the input value ("name"), and it doesn't return the computed value ("newName").

      I'd suggest something like:

      Source Code

      1. void main()
      2. {
      3. char buffer[1024]; // Not using C++ strings.
      4. std::cout << "Please enter name: ";
      5. std::cin >> buffer; // Read in name.
      6. while(CheckName(buffer) == false) // As long as name is invalid, keep looping.
      7. {
      8. SuggestName(buffer); // Generate a name suggestion.
      9. std::cout << "Invalid name.\nSuggestion is: " << buffer << "\nPlease enter new name: ";
      10. std::cin >> buffer; // Read next name attempt.
      11. }
      12. std::cout << "Your name is: " << buffer << "\n";
      13. }
      14. bool CheckName(char* name)
      15. {
      16. // True if the name doesn't equal Hayden.
      17. return (strcmp(name, "Hayden") == false);
      18. }
      19. void SuggestName(char* name)
      20. {
      21. // This assumes that buffer is big enough to hold "Hayden", which is a dangerous assumption!
      22. strcpy(name, "Hayden");
      23. }
      Display All


      Now keep in mind that code isn't really rejecting invalid names, as much as rejecting "Hayden", and then suggesting "Hayden" if the name gets rejected. So there is a risk of infinite-user-loop :)

      James
    • By the way, strcmp() returns an int, not a bool. It returns 0 if the strings are equal, and non-zero if they aren't.

      Source Code

      1. if (strcmp("X", "X"))
      2. {
      3. // this won't get run
      4. }
      5. else
      6. {
      7. // this will
      8. }


      -Rez
    • Hey James thanks for the input!

      I did look over your example, it is pretty straight forward! There are so many uses of the '*' that they are indeed confusing. Right now I think they are all pointers, aside from the arithmetic operator. Or to dereference a numerical pointer, the book said pointers of type char don't need to be dereferenced.

      So when I seen:

      Source Code

      1. bool CheckName(char* name)


      The function accepts an argument of type char* name, so it's a pointer to a function?

      I'd like to post the full program that modified the values, this was a personal exercise to see if I could work with functions. You were correct that "newName" was not allocated anywhere! I must have been passed out while posting this thread because looking at the program today, it's not even there! LOL

      As for strcmp() the book gave me the idea that you can compare a string, it failed to mention that it returns an int value for you to do any further comparison on.

      Rez, so my:

      if (!strcmp(name, "Hayden") is really comparing the inputted name and returning a 1 for a match or 0 for no match. So my thought was by placing the ! in my head I was saying if they don't match do this, if they do do that.

      Here's the full program I did to just practice, I was to pass a reference to a function, and using 2 functions, 1 for the input, and 1 for the output. I decided to go further and check the inputted name and do something cool.

      Looking at this code again, I know I could have used recursion to check the values of an array toward the name entered to provide the user a suggested name, all in the same function?

      C Source Code

      1. // Includes
      2. #include <iostream>
      3. #include <iomanip>
      4. // STD Functions used
      5. using std::cout;
      6. using std::cin;
      7. using std::endl;
      8. // Function Prototypes
      9. void getInput(int& number, char name[]);
      10. void sendOutput(const int number, char name[]);
      11. void changeName(const int number, char name[]);
      12. // Main Application
      13. int main()
      14. {
      15. int number(0); // Variable to store the user number
      16. char name[5]; // Array to store the user name
      17. for (;;)
      18. {
      19. getInput(number, name); // Get the user input
      20. if (!number) // If number returns 0 end the loop
      21. break;
      22. sendOutput(number, name); // Display the resulted input
      23. changeName(number, name); // Change the name on purpose
      24. }
      25. return 0;
      26. }
      27. // Function to get the user input
      28. void getInput(int& number, char name[])
      29. {
      30. cout << "Enter a number: ";
      31. cin >> number;
      32. if (number != 0) // As long as the number isn't 0 we will continue
      33. {
      34. cout << endl << "And a name less than 15 characters: ";
      35. cin >> name;
      36. }
      37. }
      38. // Function to send the output
      39. void sendOutput(const int number, char name[])
      40. {
      41. cout << endl << "Thank you. Your number and name were "
      42. << number << " and \"" << name << "\"" << endl;
      43. }
      44. // Function to mess with the name
      45. void changeName(const int number, char name[])
      46. {
      47. if (!strcmp(name, "Hayden"))
      48. {
      49. cout << endl << "Congratulations! Welcome to nowhere ville." << endl;
      50. } else {
      51. strcpy(name, "Magus");
      52. cout << endl << "The name was taken, you are now " << number << " and \"" << name << "\"" << endl;
      53. }
      54. }
      Display All


      Thanks again for the feedback, it is very appreciated! I've really taken all of the advice from the forums and started applying myself even more to do practice examples to 'see' things in action and to start getting them imbedded into my brain.
      You may call me char 71 97 100 100 97 109.