how do i detect '\n' in C++ ?

Sharky Forums


Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: how do i detect '\n' in C++ ?

  1. #1
    Hammerhead Shark
    Join Date
    Oct 2000
    Location
    Toronto, Canada
    Posts
    1,493

    Post how do i detect '\n' in C++ ?

    ! I cannot read line by line, i must read token by token (seperate by space) !

    How do I tell when I reach the end of a line?

    Thx

    ------------------
    DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
    UWSWA1season GP:9 G:12 A:8 Pts:20 GWG:3 +/-:-3
    MCBHL1season GP:4 G:5 A:5 Pts:10 GWG:0 +/-:9

    uwcdc.com or namgor.com
    DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
    UWSWA6seasons GP:41 G:53 A:46 Pts:99 GWG:5 +/-:-25
    MCBHL3seasons GP:14 G:20 A:8 Pts:28 GWG:4 +/-:19

    uwcdc.com or monkis.com

  2. #2
    Ursus Arctos Moderatis Grizzly's Avatar
    Join Date
    Sep 2000
    Location
    Providence, RI USA
    Posts
    3,077

    Post

    I believe the ordinal value of "\n" is 13 (In ASCII). You can test against that if you want.

    [This message has been edited by Grizzly (edited October 03, 2001).]

  3. #3
    Hammerhead Shark
    Join Date
    Feb 2001
    Posts
    1,612

    Post

    Um, I'm not sure what you're asking, but a newline character is '\n'.

  4. #4
    Tiger Shark
    Join Date
    Mar 2001
    Posts
    615

    Post

    Depends on the OS. Windows uses a '\n' which is really '\r\n' while Unix only uses a '\n.' I could be mistaken on the order they occur, but this is basically it. Just check for those characters, and you will be able to tell if you are at the end of the line. There's also standard functions to do this, but I never remember them.
    System specs:


    | Core i5 750 | GA-P55A-UD3 | 4.0 GB G.skill DDR3 1600 | eVGA 470 GTX |
    | Intel X25-M 80 GB SSD | WD 5000AAKS | Lian Li PC-7FN | Corsair TX750W |
    | Windows 7 Home 64-bit |

  5. #5
    Hammerhead Shark
    Join Date
    Oct 2000
    Location
    Toronto, Canada
    Posts
    1,493

    Post

    Input file:
    1 2 3
    934 543 65 756 765 867

    Say I need to calculate the sum for each line, sum is calculated AFTER each line, I must go cin >> myInt, that is, i dont want to read by string. Currently i have:

    while ( !cin.eof() ) {
    cin >> myInt;
    sum += myInt;
    }

    but this is actually calculating the sum for the whole file, i dont know how to detect \n

    ------------------
    DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
    UWSWA1season GP:9 G:12 A:8 Pts:20 GWG:3 +/-:-3
    MCBHL1season GP:4 G:5 A:5 Pts:10 GWG:0 +/-:9

    uwcdc.com or namgor.com
    DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
    UWSWA6seasons GP:41 G:53 A:46 Pts:99 GWG:5 +/-:-25
    MCBHL3seasons GP:14 G:20 A:8 Pts:28 GWG:4 +/-:19

    uwcdc.com or monkis.com

  6. #6
    Catfish
    Join Date
    Aug 2001
    Posts
    177

    Post

    while(data[i] != "\n")
    {
    sum += atoi(data[i]);
    }
    EPoX 8KHA+
    Athlon XP 1900+
    512 MB DDR-SDRAM
    PNY GeForce3 TI 200

  7. #7
    Hammerhead Shark
    Join Date
    Oct 2000
    Location
    Toronto, Canada
    Posts
    1,493

    Post

    Originally posted by schnarf283:
    while(data[i] != "\n")
    {
    sum += atoi(data[i]);
    }

    ha... what is data ?


    ------------------
    DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
    UWSWA1season GP:9 G:12 A:8 Pts:20 GWG:3 +/-:-3
    MCBHL1season GP:4 G:5 A:5 Pts:10 GWG:0 +/-:9

    uwcdc.com or namgor.com
    DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
    UWSWA6seasons GP:41 G:53 A:46 Pts:99 GWG:5 +/-:-25
    MCBHL3seasons GP:14 G:20 A:8 Pts:28 GWG:4 +/-:19

    uwcdc.com or monkis.com

  8. #8
    Goldfish
    Join Date
    Nov 2000
    Location
    phx, az
    Posts
    50

    Post

    the \n character wont be picked up by the cin and the only way i can think of to check this would be to concurrently increment a pointer to the character you are on. this would get pretty ugly really fast though... is there any reason you dont want to just use strings and the atoi function?

    ------------------
    --obligatory pc specs--
    Abit KT7 RAID
    700 MHz Tbird
    256 MB PC133 cheese
    gainward geforce 3
    --obligatory pc specs--
    Abit KT7 RAID
    700 MHz Tbird
    256 MB PC133 cheese
    gainward geforce 3

  9. #9
    Ursus Arctos Moderatis Grizzly's Avatar
    Join Date
    Sep 2000
    Location
    Providence, RI USA
    Posts
    3,077

    Post

    I'm more familiar with C, rather than C++. I would know how to do this pretty easily with an fscanf() statement, but I don't know of the fscanf() equivelant in C++. Anyone know?

  10. #10
    Hammerhead Shark
    Join Date
    Oct 2000
    Location
    Toronto, Canada
    Posts
    1,493

    Post

    Alright, so seem like the answer is no. Thats what I want to know. Thx all!

    ------------------
    DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
    UWSWA1season GP:9 G:12 A:8 Pts:20 GWG:3 +/-:-3
    MCBHL1season GP:4 G:5 A:5 Pts:10 GWG:0 +/-:9

    uwcdc.com or namgor.com
    DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
    UWSWA6seasons GP:41 G:53 A:46 Pts:99 GWG:5 +/-:-25
    MCBHL3seasons GP:14 G:20 A:8 Pts:28 GWG:4 +/-:19

    uwcdc.com or monkis.com

  11. #11
    Expensive Sushi
    Join Date
    Sep 2001
    Location
    Natimuk, Australia
    Posts
    11

    Post

    int sum, i=0;
    char c, buffer[20];

    cin >> c;

    while( !EOF ){ //can remember exactly, but it was shown above...

    if( c == ' '){
    buffer[i] = '\0'; // End the string here.. so we don't have the reset everytime...
    sum += atoi(buffer);
    }

    if( c == '\n' )
    cout << "Sum = " << sum << endl;

    buffer[i] = c;
    i++;
    cin >> c;
    }

    There are probably prettier ways to do this, but this will work. Wouldn't want to use for anything to big though...

  12. #12
    Expensive Sushi
    Join Date
    Sep 2001
    Location
    Natimuk, Australia
    Posts
    11

    Post

    Thought of a better way last night...

    int sum, i;
    char c;

    while(!EOF){
    cin >> i;
    sum += i;

    cin >> c; // Get either space or newline..
    if(c == '\n'){
    cout << "Sum = " << sum << endl;
    sum = 0;
    }
    }

    Of course, this assumes single spaces, no space between last number and newline. A better way would be to put the cin >> c into a loop which checks whether it is a number, space, or newline, then either pushes the character back into the buffer if it is a number and continues, swallows the space and keeps checking the characters in case of a more than one space, or if it is a newline, then do what it has to do...

  13. #13
    Hammerhead Shark BremenCulhaven's Avatar
    Join Date
    Aug 2001
    Location
    Florida
    Posts
    1,301

    Question

    Originally posted by Grizzly:
    I believe the ordinal value of "\n" is 13 (In ASCII). You can test against that if you want.

    [This message has been edited by Grizzly (edited October 03, 2001).]
    This would be the best way I think. You would need to convert every input value to ASCII and then detect 13 which is '\n'.
    e6600 o/c 3.6Ghz| Evga 680i | XMS2 Dominator 2GB | 2 150GB Raptors RAID0 | XION 103 | Plextor 740A

    AMD 2500+ barton@3200+ o/c| pc3700 1GB Infineon/Micron CAS2| Radeon 9500 128mb mod/9700| Abit AN7 | Maxtor 120GB/200GB | Logitech Z-560 | Audigy gamer hacked 2 Audigy2 | Linksys router| 22" 2020u

    2nd system:
    AMD1700+| 512mb pc3700 Infineon/Micron CAS2| Xtasy Cinema (Geforce2 mx400)| Epox 8rda+| WD 20GB, 80GB Maxtor| Yamaha YST-M10| onboard sound| Plextor 12/10/32a| 19" Princeton

  14. #14
    Ursus Arctos Moderatis Grizzly's Avatar
    Join Date
    Sep 2000
    Location
    Providence, RI USA
    Posts
    3,077

    Post

    Yeah, just a heads up though....I think 13 is the carriage return \r (Which is why testing for that works on a windows box)...because I was messing around with a little Java Program on a Solaris box the other day, and it seemed to equate the ordinal value "10" with New Line.


    [This message has been edited by Grizzly (edited October 06, 2001).]

  15. #15
    Hammerhead Shark
    Join Date
    Feb 2001
    Posts
    1,612

    Post

    And why would you want to test against 13/10 instead of '\n' (and maybe '\r'? I don't quite understand how cin works), anyway?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •