simple getline not working ><

its my first time doing a getline statement and its not going so well. Its very similar to this example I found:
1
2
3
4
5
6
7
8
9
10
11
char name[256], title[256];

  cout << "Enter your name: ";
  cin.getline (name,256);

  cout << "Enter your favourite movie: ";
  cin.getline (title,256);

  cout << name << "'s favourite movie is " << title;

  return 0;

Though this code stops and asks for input, my code isn't the same for some reason with my code its not stopping to get my text, yep very frustrating.
Here is my code:
1
2
3
char chrSolution [25];
cout << "Solution: ";
cin.getline(chrSolution, 25);
Post the entire program that duplicates the problem so that others could try running it or spot other problems. I can't see anything wrong with those three lines of code.
Don't use cin.getline(), just use getline(std::cin, somestd_string).
your right i could change it into a string and go that route, but i have already tried that in the past and getline didn't work for me. So i checked out some online examples and it shown me that i could do it with an array which is fine with me so i converted it, and I'm not going through that again to show its not stopping for input at the getline that way also. : /

The purpose of this is to check one array against another, in this case one has been sent as an argument to this function. The array im trying to get from the user is supposed to be made from the cin.getline()

Its part of a much larger program so I'm not going to post all that, but heres the function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <windows.h>
#include <cstdlib>
#include <fstream>
#include <ctype.h>
#include <stdio.h>
#include <cstring>
#include <time.h>
using namespace std; // are all present
bool solvePuzzle(char *keyPtr)
{
	int solCount = 0;//number of chars in the array
	int keyCount = 0;//num of chars in array
	char *origin = keyPtr;//keep record of where the pointer is originally looking to return soon.
	while(*keyPtr != 'Ì')//get a count of how many variables are held by this array
	{
		keyCount++;
		keyPtr++;//push pointer forward one spot in array
	}
	keyPtr = origin;//return to the original spot of the pointer
	int correct = 0;
	char chrSolution [25];//decl array
	cout << "Solution: ";//prompt
	cin.getline (chrSolution,25);//get the array from the user
	while(chrSolution[solCount] != 'Ì')//change each of chrSolution to lowercase.
	{
		tolower(chrSolution[solCount]);
		solCount++;
	}
	for(int j = 0; j < solCount; j++)
	{
		if(chrSolution[j] == *keyPtr)//check how many chars of their solution and the Key are correct
			correct++;
		keyPtr++;
	}
	if(keyCount == correct)//If they got all of the chars in the array correct when compared to the key.
		return true;
	else return false;
}

and the 'Ì' seems to be the value of empty slots in the array which c++ doesn't turn NULL for w/e reason.
Last edited on
its not stopping for input at the getline that way also. : /
Sounds like you've used the overload of operator>>() that takes an std::istream as its left-hand operand. The problem doesn't come from std::getline().
Last edited on
oh found the answer from past problems in this area.
Grey Wolf (1134) Apr 20, 2008 at 4:17am

Try flushing the stream (with something like cin.ignore();) before you call cin.getline().

It’s just a hunch that there may be a ‘\n’ left in the stream from another part of your program.
Last edited on Apr 20, 2008 at 4:35am


So i just added that in before the cin.getline() and its acting like it should now. Thanks for replies ne ways.
Last edited on
Topic archived. No new replies allowed.