Help with My Pointer Problem in Function - Input Skipping

  • Thread starter Thread starter DivGradCurl
  • Start date Start date
  • Tags Tags
    Pointers
AI Thread Summary
The discussion centers around a C++ function designed to change a string value based on user input. The main issue is that the input is not being read correctly when using a pointer to a string. Participants suggest that the function should be called with the addresses of the variables, using the syntax `changeInfo(&f, &s)`, where `f` is a boolean and `s` is a string. It's noted that passing the boolean by value is more efficient since it avoids the overhead of dereferencing a pointer. Additionally, the function's return type should be changed from `void *` to `void`, as it does not return a value. The discussion emphasizes the importance of understanding pointer usage and function parameter passing 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top