Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Input to String 1

Status
Not open for further replies.

SlykFX

Programmer
Oct 12, 2002
76
GB
Can anyone offer any explanation on this please

im trying to accept user input from keyb and ultimatly store it in a string

ive tried both
string sNewName;
getline(cin, sNewName);
and
char cNewName[25];
string sNewName;
cin.getline(cNewName, 25); // also tried "sizeof cNewName" in place of 25
sNewName = cNewName;

in both instances, the program just skips the input and continues to the next block of code
when all the input is being written to file it is just a null input being recorded.

thanks in advance for any help

I can't be bothered to have a sig!
 
The problem is most likely in the rest of the code that you didn't post.

I'm guessing that you are using operator >> to read in an int or something and the newline from the previous user input is still in the stream. Then, when you call getline (either version) it reads in that newline and stops without putting anything into sNewName.

Try adding an extra getline, or an ignore to ignore the newline still in the stream. Or, you can post more code along with input examples if that isn't the problem.
 
thanks so much

cin.ignore();
right before the getline() statement has fixed the problem

its amazing how helpful a fresh view on something can be

thanks loads

I can't be bothered to have a sig!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top