Thread: Question on "Jumping into C++" Chapter 13 practice problem 1

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    2

    Question on "Jumping into C++" Chapter 13 practice problem 1

    Hi Everybody,

    This is my first time posting to this board, so hopefully my question is formulated properly. I'm having problems with Chapter 13 practice problem 1. Basically, we have to write a function which prompts the user to enter first and last names and return the user's full name to main. This has to be done using pointers, since this is the chapter on pointers. Anyway, here is what I got so far:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void return_names(char *p_first, char *p_last)
    {
        char first, last;
        cout << "Please enter the first name: ";
        cin >> first;
        cout << "Please enter the last name: ";
        cin >> last;
        *p_first = first;
        *p_last = last;
    }
    
    int main()
    {
        char first_name, last_name;
        return_names(&first_name, &last_name);
        cout << "The full name is: " << first_name << " " << last_name;
        return 0;
    }
    The problem is that my code will only read in one character for first and last name. I tried using a char array, but couldn't get it to work for some reason. Can somebody give me a clue how this can be done using pointers? My problem is I don't know how to read in the entire name. Thanks in advance for your help!

    Ben

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,631
    The problem is that my code will only read in one character for first and last name.
    That's because you used a char as the type, this can only hold one character.
    I tried using a char array, but couldn't get it to work for some reason.
    First you should be using C++ strings not C-strings. And if you're having problems you need to show the code that is causing the problem, and then describe the problem. Also remember that the extraction operator>> will stop processing your string when it encounters any whitespace character.


    Jim

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The OP is using C-strings because, presumably, the book hasn't taught the OP how to use std::string yet. Awful book.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    meh chapter 13 sounds advanced enough for c-strings. using them has educational value, I guess.

    Code:
    #include <iostream>
    
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    
    char nameone[21]; //max 20 character name, global
    
    
    void getnames(char*& p)
    {
      cin>>nameone; //includes '\0' at the end
      p=nameone;//assign our pre-sized array to ptr1
    }
    
    
    int main()
    {
      char* ptr1;
      cout<<"gimme a name without spaces plzz"<<endl;
      getnames(ptr1);
      cout<<"Your name is: ";
      cout<<ptr1<<endl; //normally you dereference ptr, but cout handles your char* if it has a '\0'
    }
    //bonus points: try declaring nameone array inside main or getnames and make it work.
    Last edited by jiggunjer; 04-01-2015 at 09:59 AM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jiggunjer View Post
    meh chapter 13 sounds advanced enough for c-strings. using them has educational value, I guess.
    Using them in C++ has absolutely no educational value. Not in a beginner's book anyway.
    Also, nameone should really be a local variable in main.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    Hence the bonus question :-P
    We'll just have to disagree on the didactics part.

  7. #7
    Registered User
    Join Date
    Mar 2015
    Posts
    2
    Hi Everybody,

    Thanks so much for your help so far. I will respond to one of the replies and then show the code where I see my error.

    Jiggunjer, I tried running your code and it works, but I'm not sure why. In the book, he hasn't done stuff like this...

    Code:
    void getnames(char*& p)
    {
      cin>>nameone; //includes '\0' at the end
      p=nameone;//assign our pre-sized array to ptr1
    }
    Why do you do char*& p? I'm not sure what the "&" is there for.

    Here is my attempt at using a char array, but I'm getting an error. I will post the code along with the error.

    Code:
    #include <iostream>
    
    using namespace std;
    
    void return_names(char *p_first, char *p_last)
    {
        char first[20], last[20];
        cout << "Please enter the first name: ";
        cin >> first;
        cout << "Please enter the last name: ";
        cin >> last;
        p_first = first;
        p_last = last;
    }
    
    int main()
    {
        char first_name[20], last_name[20];
        return_names(&first_name, &last_name);
        cout << "The full name is: " << first_name << " " << last_name;
        return 0;
    }
    Here is the error:

    cannot convert 'char (*)[20]' to 'char*' for argument '1' to 'void return_names(char*, char*)'|
    ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


    I don't really understand this error. Can somebody give me a clue how my code could be modified so that it works?

    Also, in my original code, for the return_names function, I wrote this:

    Code:
    *p_first = first
    However, it seems once I modified it to use the char array I got an error when I tried dereferencing p_first and assigning it to first, so I got rid of the '*' and the error went away. Why is that?

    Thanks,
    Ben

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Ben Sturm View Post
    Why do you do char*& p? I'm not sure what the "&" is there for.
    Reference.

    Here is my attempt at using a char array, but I'm getting an error. I will post the code along with the error.

    Code:
        char first[20], last[20];
        cout << "Please enter the first name: ";
        cin >> first;
        cout << "Please enter the last name: ";
        cin >> last;
    Unsafe. Any book that encourages you to do something like this must be tossed out the window. Use std::getline or just forget everything and use std::string.

    >> p_first = first;
    >> p_last = last;
    Won't work because p_first and p_last aren't references. They are copies of the variables in main, so the variables in main will remain untouched.

    >> char first_name[20], last_name[20];
    >> return_names(&first_name, &last_name);
    This is just stupid C-array weirdness. C arrays don't follow the rules that the rest of the language follows. There is a pointer to the first element of an array (in case of char, it's char*). And there's also a pointer to an array (in this case, char (*)[20]). Change it to

    return_names(&first_name[0], &last_name[0]);

    ...and it will compile. But you still need references in return_names for it to work.

    However, it seems once I modified it to use the char array I got an error when I tried dereferencing p_first and assigning it to first, so I got rid of the '*' and the error went away. Why is that?
    Would you mind showing how that code looked?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    The & means pass by reference. This is the way most void returning functions manage to manipulate variables from a different scope (like main in this case).
    Code:
    void function1(int A) //makes a copy of the int passed to it and calls it A
    void function2(int &A) //Uses the original integer from wherever it was called, no new variable A is created in memory.
    as for your last remark
    However, it seems once I modified it to use the char array I got an error when I tried dereferencing p_first and assigning it to first, so I got rid of the '*' and the error went away. Why is that?
    now you are assigning an array to a pointer (valid)
    before you were assigning a char to the contents of a char pointer--i.e. char to char (valid)
    when you changed "first" from char to array it would be assigning an array to char (not valid)

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    Quote Originally Posted by Ben Sturm
    Why do you do char*& p? I'm not sure what the "&" is there for.
    Err... a quick check of the book's table of contents shows that chapter 13 covers both pointers and references. If you have to ask that question, then perhaps you should go over the material again.

    Quote Originally Posted by Elysia
    Unsafe. Any book that encourages you to do something like this must be tossed out the window. Use std::getline or just forget everything and use std::string.
    The book cover this in a much earlier chapter, and strangely, again in a much later chapter. I may be mistaken, but it seems that as a possible oversight null terminated C-style strings are not mentioned at all even after the chapters on arrays and on pointers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists Chapter 15 Practice Problem 1 Jumping Into C++
    By ArtemisFowl2nd in forum C++ Programming
    Replies: 34
    Last Post: 04-30-2014, 12:59 PM
  2. Jumping Into C++ Chapter 14 Practice Problem 1
    By ArtemisFowl2nd in forum C++ Programming
    Replies: 5
    Last Post: 04-16-2014, 09:36 PM
  3. Replies: 2
    Last Post: 02-24-2014, 05:51 PM
  4. "Jumping into C++" Chapter 5 Practice Problem
    By Grae in forum C++ Programming
    Replies: 4
    Last Post: 09-04-2013, 01:46 PM
  5. Replies: 18
    Last Post: 03-07-2013, 06:55 AM