944,144 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2410
  • C++ RSS
Feb 15th, 2007
0

Help with 1.pointers and 2.error checking

Expand Post »
Hi guys

need a little help for my homework, any help is greatly appreciated.
:cheesy:

Qn 1. pointers
i have a problem with passing pointers to functions
code is as follows:
C++ Syntax (Toggle Plain Text)
  1. //function declaration
  2. void computeProduct(product *pProduct,int *pBest);
  3.  
  4. //function call
  5. computeProduct(product *pProduct,int *pBest);
  6.  
  7. //function definition
  8. void computeProduct(product *pProduct,int *pBest)
  9. {
  10. //code
  11. }
i keep gettin 2 errors when trying to compile:
a. expected primary expression before '*' token.
b. expected primary expression before "int".
both errors point to the function call.
any ideas/hints on what i'm doin wrong?
_________________________________________________________
Qn2. error checking
i need to error proof my program.
say i need to retrieve an integer input from user.
i need to make sure that the user inputs an integer or i will prompt him to re-enter value:
C++ Syntax (Toggle Plain Text)
  1. cout << "Enter integer value:";
  2. cin >> test;
  3. while (cin.fail())
  4. {
  5. cin.clear();
  6. cin.ignore(1000,'n' );
  7. cout << "Invalid entry, re-enter value:"
  8. cin >> test;
  9. }
yet, i'm unable to find a simple way to error proof the above user's input if it is of float type.
i.e. if user enters 2.3, the variable test will automatically be allocated the value 2.
any ideas how to ensure i can detect if user enters a float value?

thanks for any help in advance.
:cheesy:
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ReDeViL is offline Offline
13 posts
since Oct 2006
Feb 15th, 2007
0

Re: Help with 1.pointers and 2.error checking

>>//function call computeProduct(product *pProduct,int *pBest);

Do not include the data type in the function call, just the variable name, like this:
computeProduct(pProduct, pBest);


Q2: Error checking is a little more difficult to resolve; there is no simple way to do it. In your case I would get keyboard input as a string and then parse the string for illegal characters. Or get keyboard input one character at a time using get() function and test it for illegal characters.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Feb 15th, 2007
0

Re: Help with 1.pointers and 2.error checking

if i declare a pointer in the main function, pass it via a function as a parameter, point it to a variable that only exists in the called function, will i be able to retrieve the vlaue of the variable via the pointer in the mian function?

gretaly appreciate any help rendered.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ReDeViL is offline Offline
13 posts
since Oct 2006
Feb 15th, 2007
0

Re: Help with 1.pointers and 2.error checking

Not unless you pass a pointer to the pointer in main(). Something like this thread from yesterday.

Also you must make sure the object in the function will not go out of scope then the function returns to main(). Objects created on the stack do not exist beyond the lifetime of the function in which it was declared, so when the function returns to main the pointer will have an invalid address.
Last edited by Ancient Dragon; Feb 15th, 2007 at 12:34 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Feb 16th, 2007
0

Re: Help with 1.pointers and 2.error checking

given this simple example:
C++ Syntax (Toggle Plain Text)
  1.  
  2. void changep(int*&p);
  3.  
  4. int main()
  5. {
  6. int*p;
  7. int x;
  8. changep (p);
  9.  
  10. cout << *p;
  11. x=*p;
  12. cout << x;
  13. }
  14.  
  15. void changep(int*&p)
  16. {
  17. int k=3;
  18. p=&k;
  19. }
in the above case, i want the pointer p to point to a variable k of value 3 in the function. i then pass it back to main and display it.
but after assigning the value of p (which is already 3) to another variable x, the value of x becomes a weird value??
any idea what i'm doin wrong?
Last edited by ReDeViL; Feb 16th, 2007 at 11:35 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ReDeViL is offline Offline
13 posts
since Oct 2006
Feb 16th, 2007
0

Re: Help with 1.pointers and 2.error checking

This is because you are assigning p the address of a variable k which is local to the function changep and hence k loses its existence (goes out of scope) as soon as the function exits.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Feb 17th, 2007
0

Re: Help with 1.pointers and 2.error checking

then y is it that when i display *p for the 1st time in the main AFTER the function was called, it still manages to display the value or 3?
it only displays rubbish after i try to change the value......
Last edited by ReDeViL; Feb 17th, 2007 at 12:06 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ReDeViL is offline Offline
13 posts
since Oct 2006
Feb 17th, 2007
0

Re: Help with 1.pointers and 2.error checking

If you make the variable k static then it will work because static variables are similar to globals except they only have scope within the function in which they are declared.
  1. void changep(int*&p);
  2.  
  3. int main()
  4. {
  5. int*p;
  6. int x;
  7. changep (p);
  8.  
  9. cout << *p;
  10. x=*p;
  11. cout << x;
  12. }
  13.  
  14. void changep(int*&p)
  15. {
  16. static int k=3; // <<<<<< Here
  17. p=&k;
  18. }
Last edited by Ancient Dragon; Feb 17th, 2007 at 12:42 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Feb 17th, 2007
0

Re: Help with 1.pointers and 2.error checking

wow, thanks!!
y didnt i think of that!
hehe........thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ReDeViL is offline Offline
13 posts
since Oct 2006
Feb 17th, 2007
0

Re: Help with 1.pointers and 2.error checking

Click to Expand / Collapse  Quote originally posted by ReDeViL ...
then y is it that when i display *p for the 1st time in the main AFTER the function was called, it still manages to display the value or 3?
it only displays rubbish after i try to change the value......
Undefined behaviour. The "it works in this case, so whats wrong with it" thing is not so good to rely on. Since k in your case is an automatic variable whose scope is local to the function you have defined, its existence is and has to be limited to the function block. Any reference to the variable or its location after the function has exited is not to be relied on.

In general, returning the address of the local variable unless you have explicity requested for it (so that you can explicity free it) is a bad progamming practice, resulting in subtle bugs. Better not rely on such things.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how can i use an iterator for a 2d vector?
Next Thread in C++ Forum Timeline: Help with error C2504 & C2143





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC