Help with My Pointer Problem in Function - Input Skipping

  • Thread starter Thread starter DivGradCurl
  • Start date Start date
  • Tags Tags
    Pointers
Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to the use of pointers in a C++ function that reads input into a string. Participants explore the behavior of the function, particularly why the input seems to be skipped when using pointers, and discuss various approaches to passing parameters to the function.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes a function that uses a pointer to a string and reports that the input is skipped, despite working correctly in the main function.
  • Another participant suggests that the function call must pass the addresses of the objects, indicating how to correctly call the function with pointers.
  • Concerns are raised about the necessity of passing the boolean flag by pointer, with a suggestion that passing by value might be more efficient.
  • A participant expresses a requirement to use pointers due to project constraints and acknowledges a lack of understanding regarding the function's declaration.
  • Further clarification is provided on how to pass the flag by value and the implications for efficiency.
  • There is a mention of confusion regarding the use of `cin.getline` versus `getline` for reading input into a string.
  • A participant points out the existence of a global `getline` function for reading lines into a `std::string`.

Areas of Agreement / Disagreement

Participants express differing views on the best way to pass parameters to the function, particularly regarding the boolean flag. There is no consensus on the optimal approach, and the discussion remains unresolved regarding the best practices for using pointers in this context.

Contextual Notes

Participants mention various assumptions about the function's behavior and efficiency, but these assumptions are not fully explored or resolved. The discussion also highlights a potential misunderstanding of function declarations and input handling in C++.

DivGradCurl
Messages
364
Reaction score
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
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'.
 
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
 
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.
 
I though it was cin.getline(char*,size)
 
There's a global "getline" function that let's you read a line into a std::string.
 

Similar threads

  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 8 ·
Replies
8
Views
5K
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
5
Views
2K
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
6K