By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

how to take into account endline character in c++

P: 1

In c++ I need to read a file of the format

number (spaces) number (spaces) number
number (spaces) number (spaces) number

into an array/vector of the form array[length of file][3] where the amount of spaces between numbers is variable. I'm trying to do this using the getline function and reading in the numbers as strings but can't seem to make it loop so that on the end of the line it uses a '\n' instead of ' '. I've tried using an if-else combination of the form

Expand|Select|Wrap|Line Numbers
  1. if (j==2)
  2. {
  3.    y='\n';
  4. }
  5. else
  6. {
  7.    y=' ';
  8. }
  9.  and then placing y in the getline command 
  11. for (i=0;i<=1;i++)
  12. {
  13.    for (j=0;j<=2;j++)
  14.    {
  15.       getline (cin,a[i][j],y);
  16.    }
  17. }

Furthermore how best to deal with the variable number of spaces, in this example i've only been using one space.

Mar 16 '07 #1
Share this Question
Share on Google+
1 Reply

Expert 2.5K+
P: 3,652
If you just use the cin >> operator to get the number, it will skip any number of spaces and/or newline characters until it gets the next number. This will also save you the trouble of casting the string you get from getline into a number.
Mar 16 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.