Jump to content
  • Advertisement
Sign in to follow this  
SaintForHire

Pointers and functions help

This topic is 1922 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I haven't been learning c++ but for a few months and I've come to pointers. So far, the most confusing thing I've encountered.

I'm using a text book that I borrowed from a friend to learn from and one of the exercises in the book asks me to ask the user to type in a name and a score, separated in to two separate arrays using pointers. 

 

I've pulled the snippet of the code that I've been working on below. The problem is that after the first pass of the function (or even before that if I put an integer into the program before the function even begins) the getline(cin, name) in the function getNames doesn't do anything anymore. My question is why doesn't it work? 

 

Thanks!

#include <iostream>
#include <string>
 
using namespace std;
 
void getNames(int, int *, string *);
 
int main()
{
    int x = 2;
 
    int *nums;
    string *names;
    nums = new int[x];
    names = new string[x];
 
    getNames(x, nums, names);
 
    for (int i = 0; i < x; ++i)
        cout << names[i] << " " << nums[i] << endl;
}
 
void getNames(int size, int *number, string *name)
{
    for (int i = 0; i < size; ++i)
    {
        cout << "Name " << i + 1 << ": ";
        getline(cin, name[i]);
        cout << "Number " << i + 1 << ": ";
        cin >> number[i];
    }
}

Share this post


Link to post
Share on other sites
Advertisement

getline() reads the current input up to the next \n and then returns. However, cin >> number reads the number and leaves everything after the number in the buffer, which includes any newlines. So if your input is "name\n10\nname\n10\n" after getline reads the name, >> will read the 10 and the input buffer will look like "\nname\n10\n" and the next getline will read just that first \n.

 

One way to get around this is to use getline to read the text for the number and then dump that text into a stringstream. You can use >> on the stringstream to parse the number.

Share this post


Link to post
Share on other sites

One way to get around this is to use getline to read the text for the number and then dump that text into a stringstream. You can use >> on the stringstream to parse the number.

 

Admittedly I don't know what a stringstream is. I've searched a little bit after reading your post and tried to use it, but I couldn't find anyway to actually implement it correctly. Help would be appreciated. 

 

I used the code at this link to help me.

 

I added the #include <sstream> in the header and also changed the getNames function to this

void getNames(int size, int *number, string *name)
{
    stringstream ss;
    for (int i = 0; i < size; ++i)
    {
        cout << "Name " << i + 1 << ": ";
        getline(cin, name[i]);
        cout << "Number " << i + 1 << ": ";
        cin >> number[i];
        ss >> number[i];
    }
}
Edited by SaintForHire

Share this post


Link to post
Share on other sites
A std::stringstream is a stream like any of the other C++ strings such as std::cout, std::cin or std::fstream. Instead of being a wrapper around the keyboard, console output or a file, it's a wrapper around a string. To read from a stringstream you need to feed it a string first. So one way to do that with your code would be:
cout << "Number " << i + 1 << ": ";
string buffer;
getline(cin, buffer);
stringstream sstr(buffer);
sstr >> number[i];

Share this post


Link to post
Share on other sites

Oh wow that's really cool. So basically I have to make the input equal to "buffer"(or whatever I want to call it). And then tell the stringstream to equal that.

 

As a side thing, is there any way to make the input equal the stringstream directly or do I always have to go through the extra step to make it equal a string, and then put that into the stringstream?

Share this post


Link to post
Share on other sites

You have to use the string in order to use the getline function. SiCrane's example seems to be the easiest way to solve the problem. As an alternative, you could eat the newline character after the number like this:

for (int i = 0; i < size; ++i)
{
    cout << "Name " << i + 1 << ": ";
    getline(cin, name[i]);
    cout << "Number " << i + 1 << ": ";
    cin >> number[i];
    char newline = cin.get();
}

The only problem with this solution is that if you enter something like a space after the number then the newline does not get eaten.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!