Thread: Can a pointer accept user input?

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Can a pointer accept user input?

    Just curious, but can a pointer accept user input?
    For example, would the following code work:

    Code:
    #include <iostream> 
    using namespace std; 
    
    int f(int* p) { 
      if (*p == 1) 
        cout << "You entered 1. Good guess." <<endl; 
      else { 
        cout << "Wrong guess, please try again." <<endl; 
        cin >> *p; 
      } 
    
    } 
    
    int main() { 
    
      int x; 
      cout << "Please input a number:"<< endl; 
      cin >> x; 
      f(x); 
      for ( ; x != 1; ; ) { 
        cout << "You entered " <<x<< ", wrong guess. Please try again"<<endl; 
        cin >> x; 
      } 
      return 0; 
    }
    Last edited by Programmer_P; 07-24-2009 at 02:45 AM.

  2. #2
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    yes abosolutely
    as long as you don't do something like this
    Code:
    int* p;
    cin >> p;
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Wink

    Quote Originally Posted by Hussain Hani View Post
    yes abosolutely
    as long as you don't do something like this
    Code:
    int* p;
    cin >> p;
    Ahh right... If I did that, that would mean pointing the user input directly at the address stored in the pointer, correct? And that would be bad...
    Using the * results in the data going to the variable located at the address, instead.
    Last edited by Programmer_P; 07-24-2009 at 01:45 AM.

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Hmm...just tried to compile it, but it gave me the following error messages:

    Line Message

    19 error: invalid conversion from 'int' to 'int'
    19 error: initializing argument 1 of 'int f(int*)'
    20 error: expected primary-expression before ')' token
    20 error: expected ')' before ';' token

  5. #5
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    I a nub, but here is my thoughts:

    p is an int but f() expects int*

    ?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Pass the address of x.

    Though a pointer for this is probably only good in demonstration: Integers are not too big to copy. As long as the prototype is OK, just pass an integer.

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    Pass the address of x.

    Though a pointer for this is probably only good in demonstration: Integers are not too big to copy. As long as the prototype is OK, just pass an integer.
    Ok, thanks. That did it.
    I was going to do that from the beginning, but didn't, thinking that I wanted to pass the value (not the address) of x to *p. But I see now the address is passed so the value of the linked-to variable can be modified via the pointer, and the actual data of the variable being pointed at never gets placed in the pointer itself (it was dumb to ever think that it did).
    On another note, I think I remember reading in a tut on this site that in a for loop, any one of the 3 fields (i.e. variable initialization, condition, or variable update) can be left blank if necessary, only the semicolons have to be there. But oddly enough that did not work.

    I had to change the

    Code:
    for ( ; x != 1; ; )
    line to a

    Code:
    for (int i; x != 1;)
    instead, before it worked.
    Last edited by Programmer_P; 07-24-2009 at 02:46 AM.

  8. #8
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Kudose View Post
    I a nub, but here is my thoughts:

    p is an int but f() expects int*

    ?
    No, actually p is an int* p, which just means its a pointer. So I basically passed-in the address of x to p, and then used *p to modify the value of x based on user input.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Programmer_P View Post
    Ok, thanks. That did it.
    On another note, I think I remember reading in a tut on this site that in a for loop, any one of the 3 fields (i.e. variable initialization, condition, or variable update) can be left blank if necessary, only the semicolons have to be there. But oddly enough that did not work.

    I had to change the

    Code:
    for ( ; x != 1; ; )
    line to a

    Code:
    for (int i; x != 1;)
    instead, before it worked.
    That first line has an errant semicolon. That's why it didn't work and why the other one does work.
    Last edited by whiteflags; 07-24-2009 at 03:49 AM.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This works as well:
    Code:
    #include <iostream> 
    using namespace std; 
    
    void f(int& p) { 
      if (p == 1) 
        cout << "You entered 1. Good guess." <<endl; 
      else { 
        cout << "Wrong guess, please try again." <<endl; 
        cin >> p; 
      } 
    
    } 
    
    int main() { 
    
      int x; 
      cout << "Please input a number:"<< endl; 
      cin >> x; 
      f(x); 
      for ( ; x != 1; ) { 
        cout << "You entered " <<x<< ", wrong guess. Please try again"<<endl; 
        cin >> x; 
      } 
      return 0; 
    }
    References instead of pointers make life easier where it can.
    Last edited by Elysia; 07-24-2009 at 04:10 AM.
    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.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Would you really use a reference here?

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, because the function actually reads a new integer from the user if the answer is incorrect.
    But I suppose the code might fail due to input buffer issues, still.
    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.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    But the function is supposed to return an int anyway.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is true, but probably not what the OP wants, since the function returns nothing (invoking undefined behavior, possibly not compiling).
    Let's change that to void.
    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.

  15. #15
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    That first line has an errant semicolon. That's why it didn't work and why the other one does work.
    What do you mean? It has 3 semicolons...one for the variable initialization (which was blank), one for the condition, and one for the variable update (which was blank too).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. timed user input
    By sainiabhishek in forum C Programming
    Replies: 4
    Last Post: 04-01-2009, 11:59 AM
  2. Truncating user input
    By CS_Student8337 in forum C Programming
    Replies: 10
    Last Post: 03-19-2009, 12:34 AM
  3. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  4. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  5. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM