| | |
Help with 1.pointers and 2.error checking
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2006
Posts: 13
Reputation:
Solved Threads: 0
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:
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:
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:
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)
//function declaration void computeProduct(product *pProduct,int *pBest); //function call computeProduct(product *pProduct,int *pBest); //function definition void computeProduct(product *pProduct,int *pBest) { //code }
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)
cout << "Enter integer value:"; cin >> test; while (cin.fail()) { cin.clear(); cin.ignore(1000,'n' ); cout << "Invalid entry, re-enter value:" cin >> test; }
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:
>>//function call computeProduct(product *pProduct,int *pBest);
Do not include the data type in the function call, just the variable name, like this:
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.
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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.
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Oct 2006
Posts: 13
Reputation:
Solved Threads: 0
given this simple example:
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?
C++ Syntax (Toggle Plain Text)
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; }
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.
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.
c Syntax (Toggle Plain Text)
void changep(int*&p); int main() { int*p; int x; changep (p); cout << *p; x=*p; cout << x; } void changep(int*&p) { static int k=3; // <<<<<< Here p=&k; }
Last edited by Ancient Dragon; Feb 17th, 2007 at 12:42 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
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......
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.
![]() |
Similar Threads
- Custom error checking (C#)
- error checking (Java)
- User Form error checking (PHP)
- Error Checking for user input (Java)
- error checking of user input (C++)
- Basic Error Checking (C++)
- How to Perform Disk Error Checking in Windows XP (Windows tips 'n' tweaks)
Other Threads in the C++ Forum
- Previous Thread: how can i use an iterator for a 2d vector?
- Next Thread: Help with error C2504 & C2143
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






