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:
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.
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
#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>
usingnamespace 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.
returntrue;
elsereturnfalse;
}
and the 'Ì' seems to be the value of empty slots in the array which c++ doesn't turn NULL for w/e reason.
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().