User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,813 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,573 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1373 | Replies: 9
Reply
Join Date: Oct 2006
Location: Singapore
Posts: 13
Reputation: ReDeViL is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
ReDeViL ReDeViL is offline Offline
Newbie Poster

Help Help with 1.pointers and 2.error checking

  #1  
Feb 15th, 2007
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:
//function declaration
void computeProduct(product *pProduct,int *pBest); 

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

//function definition
void computeProduct(product *pProduct,int *pBest)
{
       //code
}

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:
cout << "Enter integer value:";
cin >> test;
while (cin.fail())
{
     cin.clear();
     cin.ignore(1000,'n' );     
     cout << "Invalid entry, re-enter value:"
     cin >> test;
}

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:
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,643
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 867
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

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

  #2  
Feb 15th, 2007
>>//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.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Oct 2006
Location: Singapore
Posts: 13
Reputation: ReDeViL is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
ReDeViL ReDeViL is offline Offline
Newbie Poster

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

  #3  
Feb 15th, 2007
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.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,643
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 867
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

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

  #4  
Feb 15th, 2007
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 11:34 am.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Oct 2006
Location: Singapore
Posts: 13
Reputation: ReDeViL is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
ReDeViL ReDeViL is offline Offline
Newbie Poster

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

  #5  
Feb 16th, 2007
given this simple example:
void changep(int*&p);

int main()
{
    int*p;
    int x;
    changep (p);
    
    cout << *p;
    x=*p;
    cout << x;
}

void changep(int*&p)
{
    int k=3;
    p=&k;
}

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 10:35 am.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,806
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 338
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

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

  #6  
Feb 16th, 2007
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.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Oct 2006
Location: Singapore
Posts: 13
Reputation: ReDeViL is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
ReDeViL ReDeViL is offline Offline
Newbie Poster

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

  #7  
Feb 16th, 2007
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 16th, 2007 at 11:06 pm.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,643
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 867
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

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

  #8  
Feb 16th, 2007
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 16th, 2007 at 11:42 pm.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Oct 2006
Location: Singapore
Posts: 13
Reputation: ReDeViL is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
ReDeViL ReDeViL is offline Offline
Newbie Poster

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

  #9  
Feb 17th, 2007
wow, thanks!!
y didnt i think of that!
hehe........thanks!
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,806
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 338
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

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

  #10  
Feb 17th, 2007
Originally Posted by ReDeViL View Post
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.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:30 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC