Help with My Pointer Problem in Function - Input Skipping

  • Thread starter DivGradCurl
  • Start date
  • Tags
    Pointers
In summary: There's also a member function "getline" of std::istream that does the same, and it's that one that you're calling here.
  • #1
DivGradCurl
372
0
In my program have a function like this:

Code:
void * changeInfo(bool *flag, string *NewName)
{
     if (!(*flag))
     {
     cout << endl << "Enter the new Name: ";
     getline(cin, *NewName);
     cout << *NewName;
    }
}

My problem is reading the input using the pointer to a string. The program simply skips that part. It is weird that "getline" works if I'm using the commands within the main. Yes, I prototype and call the function in addition to passing the inputs. It does not seem to be trivial. I've spent a lot of time trying to fix this, but no success so far.

Any help is highly appreciated.
 
Technology news on Phys.org
  • #2
How are you calling the function, in main() or wherever? If a function expects to receive pointers, and main() has the actual objects, you need to specify when you call the function that you're passing the addresses of the objects rather than the objects themselves:

Code:
bool f;
string s;
changeInfo (&f, &s);

Also, your function doesn't change the value of 'flag', so there's no need to pass it by pointer. Bools are small enough that they can be passed by value efficiently.

(Personally, in C++ I prefer to pass parameters that need to be modified in the function, by reference instead of by pointer, but this is basically a matter of personal preference only, so I won't press this issue.)

Finally, why did you declare the function as 'void *'? If the function doesn't return anything via a 'return' statement, it should be declared simply as 'void'.
 
  • #3
Unfortunately, I can only use pointers since this is one of the requirements of my project. I'm using * after void because I thought it was the right procedure if the inputs are pointers. I have a lot to learn. :)

I'm going to remove the flag and inspect my main function. I'll get back to you if I have more questions.

Thank you
 
  • #4
What I meant about the flag is that you can pass it this way:

In main():

Code:
bool f;
string s;
// I assume that you set the value of f somewhere in between here...
changeInfo (f, &s);

In the function:

Code:
void changeInfo(bool flag, string *NewName)
{
     if (! flag)
     {
     cout << endl << "Enter the new Name: ";
     getline(cin, *NewName);
     cout << *NewName;
    }
}

Passing the flag by value is more efficient than passing by pointer, because the function doesn't have to dereference a pointer first, before using the value of the flag. It just uses the value directly.
 
  • #5
I though it was cin.getline(char*,size)
 
  • #6
There's a global "getline" function that let's you read a line into a std::string.
 

1. What is a pointer in programming?

A pointer in programming is a variable that holds the memory address of another variable. It allows for direct access to the value stored in that memory address.

2. Why is my function skipping input when using pointers?

There could be several reasons for this. One possible reason is that the pointer is not properly initialized or pointing to the correct memory address. Another reason could be that the pointer is being incremented or modified within the function, causing it to skip over certain inputs.

3. How can I fix my pointer problem?

To fix a pointer problem, it is important to carefully review your code and make sure the pointer is properly initialized and pointing to the correct memory address. You may also need to check for any unintended modifications to the pointer within the function.

4. Can using pointers in a function cause memory issues?

Yes, using pointers in a function can potentially cause memory issues if not properly managed. This can include memory leaks, where memory is not properly freed after use, or accessing memory that has already been deallocated.

5. Are there any alternatives to using pointers in a function?

Yes, there are alternatives to using pointers in a function, such as passing variables by reference or using arrays. These alternatives may be more suitable depending on the specific problem you are trying to solve.

Similar threads

  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
5
Views
846
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
4
Replies
118
Views
6K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
5
Views
1K
Replies
10
Views
904
  • Programming and Computer Science
Replies
5
Views
759
Back
Top