|
-
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
-
Ursus Arctos Moderatis
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).]
-
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.
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 |
-
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
-
while(data[i] != "\n")
{
sum += atoi(data[i]);
}
EPoX 8KHA+
Athlon XP 1900+
512 MB DDR-SDRAM
PNY GeForce3 TI 200
-
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
-
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 
-
Ursus Arctos Moderatis
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?
-
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
-
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...
-
-
Hammerhead Shark
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
-
Ursus Arctos Moderatis
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).]
-
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
-
Forum Rules
|
|