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

How to get a specific line in a Code... Need Help

P: 19
C++ gets confusing sometimes just like my next problem
I need to get a specific line in the file
Say File Precipitation has this inside
Expand|Select|Wrap|Line Numbers
  1. January    
  2. 14.5
  3. 12.5
  4. 10.8
  5. 8.2
  6. 13.8
  7. february
  8. 15.4
  9. 15.2
  10. 8.2
  11. 7.5
  12. 9.9
  13.  
know what i need to do is display the file like this
Expand|Select|Wrap|Line Numbers
  1. January       February
  2. 14.5            15.4
  3. 12.5            15.2
  4. 10.8            8.2
  5. 8.2              7.5
  6. 13.8            9.9
  7.  
now i need to mod it i think
so i was thinking this
Expand|Select|Wrap|Line Numbers
  1. int n;
  2. if (n % 4 ==0)
  3. fin >> Prec#1
  4.  
but since i am not very good with mods i dunno how to do it...
so i need someones help
so for the first line its just

fin >> sMonth

just need help on getting 2nd, 3rd, 4th, 5th line please
Nov 5 '06 #1
Share this Question
Share on Google+
12 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Fir3Bat-

Do you know how many items will be below each month? If that is fixed, you can read everything in to separate variables, and then when you fout or cout, just not include the endl or "\n" so that you can display each month, then on the next line, the first item from each month, etc...

If you really want to get into the formatting, I would suggest looking at the C printf() function.
Nov 5 '06 #2

P: 19
thanks, ill look into that
Nov 5 '06 #3

P: 19
so say if i want to fin >> sMonth 5 times
what mod should i use???
n%? == ?
Nov 5 '06 #4

sicarie
Expert Mod 2.5K+
P: 4,677
Fir3Bat-

What is the ultimate result? Are you trying to display the info as you had up there before? Or are you trying to do something after that?

I don't think you need the modulus, unless you're making a calculation after you read them in.

You're using fin, so you're reading input in from the user, and then displaying it, right? So you

Expand|Select|Wrap|Line Numbers
  1. fout >> "January\tFebruary\n";
  2. fout >> jan1 >> "\t\t" >> feb1;
  3.  
After that you say you need to mod it, but I don't understand why. With this below:
Expand|Select|Wrap|Line Numbers
  1. if (n % 4 ==0)
  2. fin >> Prec#1
  3.  
Breaking down the code, you say that only after you create a new variable and divide it by four, if the remainder is 0, you fin a variable. Why? Are you only supposed to read in a certain number of variables? Or are you trying to get the input to be within a certain range? Maybe a better understanding of what the statistics each month stand for will help.
Nov 5 '06 #5

P: 19
noo here is what i wanted
i need to get everyline of the code into an array
then display the array in my output code
i need it like that
Nov 6 '06 #6

sicarie
Expert Mod 2.5K+
P: 4,677
It sounds to me like you want to read each line of the file into some sort of ordered collection - such as an array or linked list that will allow you to iterate over them, check the values and order (to make sure it's either a number or the name of a month), and then print them out.

You can use getline(fin,length); to get the input, where fin is the name of the input file, length is how many characters you want to pick up. You would probably want to do that inside of a while statement - something like while (!fin.eof()). You might have to google that to get the exact syntax.

After that, just cout or fout each variable as you want.
Nov 6 '06 #7

P: 19
Expand|Select|Wrap|Line Numbers
  1. January
  2. London    
  3. 14.5
  4. 12.5
  5. 10.8
  6. 8.2
  7. 13.8
  8.  
  9. february
  10. Quebec City
  11. 15.4
  12. 15.2
  13. 8.2
  14. 7.5
  15. 9.9
  16.  
Ok, Say i need to find:
1.the total average of the month February, how would i do that... because january comes before it
2. Find Highest Number in (specific month)
3.smallest number in specific city..

just i get frusterated and just cant do this all in my head... im soo lost
Nov 6 '06 #8

sicarie
Expert Mod 2.5K+
P: 4,677
Well, the start is to understand the whole picture. You keep adding lines and objectives, and that makes it more complicated. Is there anything else you need to do besides what you just said?

I was thinking you just wanted to read them in and out before, not manipulate them. You can just read them in and out as strings without actually having to touch the values, but if you need to do things like find the average, it becomes a bit more complicated, but definitely not impossible.

Your objectives are:

open a file
read in information
manipulate information
print out information

If those are correct general principles (and complete - only using one file to read in from, not outputting to the same file, etc...), then we need to know the format of the file we are opening. What is in it exactly? Is it only text, only those precipitation averages, or is it labeled by month and city, and is it by month and city (for example: January-London and then the next January will be January-Prague, or will it just be January, and then the next few lines are London, it's averages, and then Prague, its averages, etc...)? Are the precipitation numbers under each the same (are there 3 data items given under each city)?
Nov 6 '06 #9

P: 19
This is what is in the file
Expand|Select|Wrap|Line Numbers
  1. January
  2. 12.4
  3. 13.5
  4. 12.1
  5. 12.6
  6. February
  7. 8.2
  8. 9.5
  9. 8.0
  10. 7.5
  11. March
  12. 14.9
  13. 13.5
  14. 12.9
  15. 15.3
  16. April
  17. 30.0
  18. 35.5
  19. 40.2
  20. 37.7
  21. May
  22. 24.5
  23. 26.1
  24. 23.4
  25. 24.0
  26. June
  27. 30.3
  28. 4.6
  29. 11.4
  30. 21.6
  31.  
This is what i have to do
Expand|Select|Wrap|Line Numbers
  1.  
  2. a.    the average weekly precipitation for North Bay;
  3. b.    the average precipitation for April;
  4. c.    the total precipitation for all areas for the first six months of the year;
  5. d.    the city with the largest precipitation for June;
  6. e.    the month with the smallest precipitation in London.
  7.  
Just my class isn't to this part yet and our teacher does not tell us how to do this stuff intil tomorrow, and out test is day after tomorrow
Please help if you can. or just do (c) and (a)...
i will repay the favour somehow
Nov 6 '06 #10

sicarie
Expert Mod 2.5K+
P: 4,677
Ok, so if you're looking for the total precipitation, I would just put everything in an integer array, and ignore every fifth line (counting the first one as 5).

Pseudocode:
while !file.eof()
if sentinel == 5
getline() // or however you are most comfortable reading the input in
sentinel = 1 (reset it, because we dont' want it again for 5 more lines)
// read the line in, but do nothing else - dont' try to add a char & an int
else // it's not 5 yet, we ahve a #
getline() // pull the next int value from the file, and put it into a buffer
array[index] = buffer
sentinel ++; index++;
}

so we got out of the file, and have all the input in, so now we just do
for int i = 0, i<24;i++ // there are 4 values each, 6 months, 24 total values (but we start at 0)
sum +=array[i]
average = sum/total(24)

With the average weekly, you want every 6th. So do the same thing as above, but initialize the sentinel to 6, and then only read in when sentinel == 6. (and change the total, as you are only reading in 6 values).

You might have to play around with the numbers a little bit, but that should be the general idea of how to get the rest.
Nov 6 '06 #11

P: 19
Ok, so if you're looking for the total precipitation, I would just put everything in an integer array, and ignore every fifth line (counting the first one as 5).

Pseudocode:
while !file.eof()
if sentinel == 5
getline() // or however you are most comfortable reading the input in
sentinel = 1 (reset it, because we dont' want it again for 5 more lines)
// read the line in, but do nothing else - dont' try to add a char & an int
else // it's not 5 yet, we ahve a #
getline() // pull the next int value from the file, and put it into a buffer
array[index] = buffer
sentinel ++; index++;
}

so we got out of the file, and have all the input in, so now we just do
for int i = 0, i<24;i++ // there are 4 values each, 6 months, 24 total values (but we start at 0)
sum +=array[i]
average = sum/total(24)

With the average weekly, you want every 6th. So do the same thing as above, but initialize the sentinel to 6, and then only read in when sentinel == 6. (and change the total, as you are only reading in 6 values).

You might have to play around with the numbers a little bit, but that should be the general idea of how to get the rest.
wow, ok that made it so easier... but one thing, what is sentinal??
Nov 7 '06 #12

sicarie
Expert Mod 2.5K+
P: 4,677
A sentinel value is any arbitrary marker value. It's usually something a person can type in to end input, but upon execution of this 'sentinel' value, generally an action will occur to alter the program (like a flag, if it's set, do one thing, if it's not set, do another).
Nov 7 '06 #13

Post your reply

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