cin.get() question

I am trying to get a string as input from the user and store each character in a linked list (each character getting it's own node). I am using a sentinel for when to stop. I'm not all too familiar with the cin.get() method, and I have been looking around the internet and I think I'm using it correctly, but when I try to compile it goes all wonky on me. Any help is greatly appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        string vig;
        char userval;

        cin >> vig;
        cin.get (userval, SENTINEL);
        while (userval != SENTINEL){
                temp = new listnode;
                temp -> data = userval;
                temp -> next = head;
                if (head == NULL)
                        tail = temp;
                else 
                        ;
                head = temp;
                cin.get (userval, SENTINEL);
                }
closed account (2UD8vCM9)
Use getline instead

 
getline(cin,vig);
will put your input into the string cig

Forgot to say, make sure you call a cin.clear() after your getline like so

1
2
getline(cin,vig);
cin.clear();
Last edited on
Topic archived. No new replies allowed.