Can a pointer accept user input?

This is a discussion on Can a pointer accept user input? within the C++ Programming forums, part of the General Programming Boards category; For loops do not have 3 semicolons. Look at the simplest case: Code: for(int i = 0; i < something; ...

  1. #16
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    For loops do not have 3 semicolons. Look at the simplest case:
    Code:
    for(int i = 0; i < something; i++)

  2. #17
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Oh, right...duh!
    I knew that, but didn't remember to visualize what it would look like if it had all 3 fields.
    Forget I even said that...

    So I suppose if I removed that last semicolon in the first for loop I wrote, it would have worked?

  3. #18
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    So I suppose if I removed that last semicolon in the first for loop I wrote, it would have worked?
    correct.

  4. #19
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    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.
    So I suppose every int function is supposed to return an int then?
    Just for the record, the code did compile, when I changed it to the following:

    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 ( int i; x != 1; ) { 
        cout << "You entered " <<x<< ", wrong guess. Please try again"<<endl; 
        cin >> x; 
      } 
      return 0; 
    }
    Of course it doesn't handle the cases if a user entered a char, or float. But it was only supposed to be an example anyway for my question, which was whether or not a pointer could accept user input (I assumed it could, but just wanted to make sure).

    Thanks for the replies.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,829
    Quote Originally Posted by Programmer_P View Post
    So I suppose every int function is supposed to return an int then?
    No, it returns whatever you want it to return.

    Just for the record, the code did compile, when I changed it to the following:

    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 ( int i; x != 1; ) { 
        cout << "You entered " <<x<< ", wrong guess. Please try again"<<endl; 
        cin >> x; 
      } 
      return 0; 
    }
    Of course it doesn't handle the cases if a user entered a char, or float. But it was only supposed to be an example anyway for my question, which was whether or not a pointer could accept user input (I assumed it could, but just wanted to make sure).

    Thanks for the replies.
    Yes, it does compile, but you may as well use references instead of pointers when possible, and in this case, it is indeed possible.
    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. #21
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,745
    Quote Originally Posted by Elysia View Post
    No, it returns whatever you want it to return.
    Just to be crystal clear, this is all kinds of wrong.

    OP, if we consider the parts of a function prototype such as int f (int *p); then, the return value type is int, and you need to return an integer. You are only supposed to omit a return statement if the return type is void.

  7. #22
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by whiteflags View Post
    Would you really use a reference here?
    Always use a reference rather than a pointer if it is not valid for the parameter to be NULL. (except in cases where the function is exported)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #23
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by iMalc View Post
    Always use a reference rather than a pointer if it is not valid for the parameter to be NULL. (except in cases where the function is exported)
    His comment was not in regards to reference vs pointer. It was if the function should return the integer from f() instead of passing it back through a reference.

  9. #24
    Registered User kakayoma's Avatar
    Join Date
    Jul 2009
    Location
    Dallas, Texas
    Posts
    42
    yes of course

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,829
    You revive a thread only to make an answer everyone else has already provided? This is not proper forum etiquette.
    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.

Page 2 of 2 FirstFirst 12
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, 02:53 PM
  5. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 09:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21