473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
12 2237
sicarie
4,677 Expert Mod 4TB
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
Fir3Bat
19
thanks, ill look into that
Nov 5 '06 #3
Fir3Bat
19
so say if i want to fin >> sMonth 5 times
what mod should i use???
n%? == ?
Nov 5 '06 #4
sicarie
4,677 Expert Mod 4TB
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
Fir3Bat
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
4,677 Expert Mod 4TB
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
Fir3Bat
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
4,677 Expert Mod 4TB
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
Fir3Bat
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
4,677 Expert Mod 4TB
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
Fir3Bat
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
4,677 Expert Mod 4TB
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

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

Similar topics

2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
1
by: Alejandro | last post by:
Hi, I Have a form with Collection of 52 picturebox. Public cl As New Collection Private m_Bitmap As Bitmap Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)...
16
by: robertmeyer1 | last post by:
Hey, I am working on creating a query which will export the information to excel. I have a specific excel document which has line by line items (corresponds to access query). Here's the...
10
blazedaces
by: blazedaces | last post by:
Alright guys, so the title explains exactly my goal. The truth is I'm going to be reading in a lot of data from an xml file. The file is too large and there's too much data to store in arraylists...
7
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to change the font color and weight at a specific position in the text. I am not sure where or what I need to do. It will be at position 155/156 from the left on each line. Here is the...
2
by: Francesco Pietra | last post by:
Please, how to adapt the following script (to delete blank lines) to delete lines containing a specific word, or words? f=open("output.pdb", "r") for line in f: line=line.rstrip() if line:...
11
by: jo3c | last post by:
hi everybody im a newbie in python i need to read line 4 from a header file using linecache will crash my computer due to memory loading, because i am working on 2000 files each is 8mb ...
1
by: sxwend | last post by:
I am trying to use the following post results (http://www.thescripts.com/forum/thread189759.html) and add another requirement. I need to send the results to just the email addresses that the query...
6
by: Ramesh | last post by:
Hello, I am using the ofstream class to create a text file with keys and values like: Key1=Value10 Key2=Value15 Key3=Value20 In case I need to set a new value for Key2, say value50 - I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.