Click to See Complete Forum and Search --> : how do i detect '\n' in C++ ?
namgor
10-03-2001, 05:57 PM
! 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 (http://www.uwcdc.com) or namgor.com (http://www.namgor.com)
Grizzly
10-03-2001, 06:42 PM
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).]
Strogian
10-03-2001, 09:42 PM
Um, I'm not sure what you're asking, but a newline character is '\n'.
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.
namgor
10-04-2001, 01:21 PM
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 (http://www.uwcdc.com) or namgor.com (http://www.namgor.com)
schnarf283
10-04-2001, 08:01 PM
while(data[i] != "\n")
{
sum += atoi(data[i]);
}
namgor
10-05-2001, 02:34 AM
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 (http://www.uwcdc.com) or namgor.com (http://www.namgor.com)
vitaminj
10-05-2001, 04:03 AM
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 :)
Grizzly
10-05-2001, 09:12 AM
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?
namgor
10-05-2001, 09:58 AM
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 (http://www.uwcdc.com) or namgor.com (http://www.namgor.com)
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... http://www.sharkyforums.com/ubb/wink.gif
Thought of a better way last night... http://www.sharkyforums.com/ubb/smile.gif
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... http://www.sharkyforums.com/ubb/smile.gif
BremenCulhaven
10-06-2001, 02:13 AM
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'.
Grizzly
10-06-2001, 08:42 AM
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).]
Strogian
10-06-2001, 09:12 AM
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?
namgor
10-06-2001, 11:55 AM
guys, i got my coding done, i end up using getline , and parse the string... I knew this methdo all along, but I was trying to ask if it is possible to read from cin to integer while able to detect line by line, n somebody ans me no! -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 (http://www.uwcdc.com) or namgor.com (http://www.namgor.com)
Strogian
10-06-2001, 12:14 PM
It may be possible using some sort of getc/ungetc thing, but those are C functions.