C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem with getline

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Tarali Dunn
Guest





PostPosted: Sat Sep 20, 2003 10:09 am    Post subject: Problem with getline Reply with quote



In my code I write:

char test_file_name [20], shift_file_name[20], structure_file_name[20],
autocovariance_file_name[20], statistics_file_name[20];
int size_test_file, size_shift_file;
double test_resolution_x, test_resolution_y;

std::cout <<"Enter test file name n";
std::cin.getline(test_file_name, sizeof(test_file_name));
std::cout <<"Enter number of coordinates in test filen";
std::cin >> size_test_file;
std::cout <<"Enter shift file namen";
std::cin.getline(shift_file_name, sizeof(shift_file_name));
std::cout <<"Enter number of coordinates in shift filen";
std::cin >> size_shift_file;
std::cout <<"Enter output file name for structure data n";
std::cin.getline(structure_file_name, sizeof(structure_file_name));
std::cout <<"Enter output file name for autocovariance data n";
std::cin.getline(autocovariance_file_name,
sizeof(autocovariance_file_name));

When I go to run it. It waits for input after "Enter test file name" but
it seems to skip the other input statements and just does the output
statements. What am I doing wrong?

Tara
[email]taralid (AT) yahoo (DOT) com[/email]





[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
John Potter
Guest





PostPosted: Sat Sep 20, 2003 10:47 pm    Post subject: Re: Problem with getline Reply with quote



On 20 Sep 2003 06:09:30 -0400, "Tarali Dunn" <mad (AT) seanet (DOT) com> wrote:

Quote:
In my code I write:

char test_file_name [20], shift_file_name[20], structure_file_name[20],
autocovariance_file_name[20], statistics_file_name[20];
int size_test_file, size_shift_file;
double test_resolution_x, test_resolution_y;

std::cout <<"Enter test file name n";
std::cin.getline(test_file_name, sizeof(test_file_name));

Reads the file name and removes the newline.

Quote:
std::cout <<"Enter number of coordinates in test filen";
std::cin >> size_test_file;

Reads the number and leaves the newline in the input.

Quote:
std::cout <<"Enter shift file namen";
std::cin.getline(shift_file_name, sizeof(shift_file_name));

Reads nothing and removes the newline.

Quote:
std::cout <<"Enter number of coordinates in shift filen";
std::cin >> size_shift_file;

Fails because the first character of the above file name is not
usable in an int.

Cin is now broken and all following reads fail.

Mixing >> and getline is always a pain.

(std::cin >> size_test_file).get();

Reads the number and removes the following character which we hope
is the newline. You might prefer something hopeless.

std::cin >> size_test_file;
cin.ignore(1000, 'n');

Reads the number and removes a bunch through the first newline.

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.