![]() |
| ||
| Please I need help formatting an output file and sorting the names! Hello: I need help with a project. I have to read an input file that contains data for 17 students. This is how they are: first ID, then first name, then last name, then 10 test scores. For this data, I have to read it and process it to send it to a new file that only contains: the last name, a comma, the first name, the ten scores, and the average for them followed by a letter grade. However, the names have to be in alphabetical order, and it has to be formatted. This is the code I have now: It outputs the last name, a comma, first name, scores, average and letter grade. However, it doesn't put the names in order neither makes them look formatted. Any ideas? #include <cstdlib><< moderator edit: added [code][/code] tags >> |
| ||
| Re: Please I need help formatting an output file and sorting the names! what is the format that it has to be in? |
| ||
| Re: Please I need help formatting an output file and sorting the names! For example: The output for let's say two students would be: Cannon, Scott 70 75 78 88 83 85 79 91 88 90 82.7 B Espinola, Angelica 97 98 89 88 90 92 95 100 92 88 99.1 A In alphabetical order and name to the left, and scores, average and letter grade to the right! I've got the names to the left, but I need to sort them and move scores and average to the right all aligned! this thing doesn't let me put them justified but at least it is similar! |
| ||
| Re: Please I need help formatting an output file and sorting the names! I've working with this, and I have already solved the formatting problem, my only problem left is how to put the names in order! This is how I changed it a little, I just included some width and precision functions, as well as left and right justifiers. #include <cstdlib><< moderator edit: added [code][/code] tags >> |
| ||
| Re: Please I need help formatting an output file and sorting the names! The sorting you can do in many ways. One way could be putting all the data in let's say a class and then sort the vector of objects with ssort(), qsort() or whatever function you want. Other way could be putting the unsorted lines in a temp file, taking the first letter of every line and comparing the first letter from the next line with it (Tip: after the first letter, which is at position 0 in the file every next letter will be after a '\n'). If the corresponding condition is true you can swap the lines and go on. There are other ways probably better ones, this is what comes to me now. The formatting you can do by creating an imaginary "name field" for the names. When you get them from the file and create the fullname you can check it's length and save it as a temporary. Consequtive checks can give you the size of the longest name. When writing the temp output file you can check the name of the student before streaming it to the file and add to it (if necessary) the number of white spaces it needs to reach the name with the max size, i.e. int bla = longest_name - fullname.size(); and add 'bla' blanks to fullname. Then you can continue with copying the scores. Since I don't know the inbuilt C++ libraries too good I can't offer you a more subtle way to do the formatting, but this should work. |
| ||
| Re: Please I need help formatting an output file and sorting the names! You are going to need to store the information as you read it in, do the sort and then write it back out. Can't write each line as you read it in AND sort at same time. my c/c++ is a little rusty but you want something like the following. Was in a little bit of a hurry and was doing this in notepad, so there might be some mismatched brackets, but that should give you an idea #include <cstdlib> |
| ||
| Re: Please I need help formatting an output file and sorting the names! I am still working on this problem, but I cannot find a way to finish it without mixing up all the scores and the names. Is there a way to sort the names without mixing the scores with anyone else's scores. I can't use pointers because I do not know them yet. Nor, classes! Please, any help is really apprecciated, I am really confused. The code sent to me is kind of confusing because it makes me loose track of what it is doing! Can anyone explain it to me, I find it difficult to understand two dimensional arrays! |
| ||
| Re: Please I need help formatting an output file and sorting the names! post the code your are using to sort |
| ||
| Re: Please I need help formatting an output file and sorting the names! Doing this without using struct/classes will be tedious. You could have an array of firstnames only, an array of last names only, an array of middle names only, an array for each persons first exam grades, an array for each persons second exam grades, ..., an array for each persons grade average, and an array for each letter grade. A given index in a given array has the information for the same person. Therefore, if you have to rearrange the order of the names after loading all the file data, to keep everything correlated by index you could rearrange the order for each of the other arrays in the same manner. So, for example, if you are using bubble sort to sort the array of last names and you end up swapping LastName[i] with LastName[i + 1] Then you will need to swap: FirstName[i] with FirstName[i + 1]; Exam1[i] with Exam1[i + 1]; Exam2[i] with Exam2[i + 1]; . . . Exam10[i] with Exam10[i + 1]; AveNumResult[i] with AveNumResult[i + 1]; and, finally LetterGrade[i] with LetterGrade[i + 1]; etc. After setting all that up just once, I can assure you, you will appreciate what struct/classes can do for you when you learn about them. Since you don't know pointers then you won't be able to use lists, so if you try to sort with insertion rather than after completely reading the file you will need to be able to shift all contents coming after desired insertion location in all arrays to make room for the new entry. Having said that however, it might be easier than sorting post insertion. Your call. In the end, however, I suspect the exercise is either to get you used to working with arrays |
| ||
| Re: Please I need help formatting an output file and sorting the names! Do you mean that I can open the first file and once opened I can insert the ID, first name, last name, 10 scores, for each of the 17 students in different arrays, and after that sort them alphabetically? So, after sorting calculate average and letter grade? Or, first calculate average and letter grades and then sort the names! That is what confuses me because I do not know if first I have to read it and process them and then sort them or viceversa! Now, i have them all formatted and average processed, and letter grade too. But, i did not put names into arrays, so I do not know if i have to redo the whole project, or if I can work from what I have! |
| All times are GMT -4. The time now is 11:31 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC