Thread: How to read from a file and find mean o in C++ which has integers and characters?

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    5

    How to read from a file and find mean o in C++ which has integers and characters?

    I have a txt file called test.txt which contains:

    ID NUMBER NAME EXAM1 EXAM2 EXAM3
    040140101 Furkan Cingi 89 85 79
    040140102 Faruk Yilmaz 70 70 50
    040140103 Fatma Yildiz 65 20 3
    040140104 Burak Yilmaz 10 11 12
    040140105 Burkina Sarz 8 88 100
    040140106 Bame Mert 18 28 59
    040140107 Barnie Esry 56 20 6
    040140108 Farnie Zarni 80 40 85
    040140109 Neden Eser 78 45 10
    040140110 Son Eser 14 58 45

    How can I get each exam grades and compute mean and then write into result.txt file ?

    I used structure for this but can't read only exam grades.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Maybe you can't because this table has one extra column. The name is broken into two parts, name and surname.

    You should essentially ignore the first 3 words of every line.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    5
    I think we should use struct. But I don't know how to use struct to find grades. I am new

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You need to create a struct that would hold all the values for each student ( id, name, grades, etc ).
    Then you need to make a function( or method, idk if this is C or C++ ) that can read a single line and save the important data into a variable of this struct.
    After that, ignore the first file line, and read all the lines with that function. Use the grades to find the mean( or average idk ) and output the id, name and the new value in results.txt( and a newline, to separate students )

    You may want the output file to have a different structure, that's up to you to choose how to do.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Phase 1 - File I/O

    Step 1: Write a simple program that reads one line of the text file at a time, and prints that line to the screen. When you have that working, proceed to the next step.

    Step 2: Modify the program so that, after reading a line, you extract each field into its own variable. Then print each variable to the screen.

    If you cannot get this far on your own, refer to your learning material (if you don't have a good book, get one) and practice with the examples until you can figure it out. If you get stuck, show us what you have so far so we can help you.

    If you do get this far on your own, proceed to Phase 2.



    Phase 2 - Using Structs

    Step 1: Start with the basics - define a struct, create a single variable of that type, assign values to each member, and print each member to the screen. If you cannot do this, then you need to refer to your learning material. Work through some of the examples until you get the basic idea, then try to do this first step on your own.

    Step 2: Modify the code so that instead of hard-coding the member assignments, you read input from stdin, assign the input to each member, and print each member to the screen.

    Step 3: Modify the code so you read into an array of struct (instead of just one) and print each member of each array element to the screen. This will need surprisingly little modification to achieve.

    If you get stuck anywhere along the way, post your progress and tell us where you're stuck.



    Phase 3 - Putting it together

    Create your array of struct as per Phase 2, and instead of reading input from stdin, read it from the file as per Phase 1. Print the output to ensure you're reading and parsing the input correctly.

    After this, implement any calculations you need. The final step would be modifying the code to write the necessary results to an output file.

    The key here is baby-steps: A development process

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to read from file of integers into 2D array
    By Hana Nasser in forum C Programming
    Replies: 3
    Last Post: 09-14-2015, 11:56 PM
  2. read txt file with strings and integers
    By akosinoah in forum C Programming
    Replies: 4
    Last Post: 11-16-2013, 09:08 AM
  3. read unknown integers from text file
    By underlink in forum C Programming
    Replies: 2
    Last Post: 11-10-2009, 10:23 AM
  4. Convert array of characters in file to integers
    By millsy5 in forum C Programming
    Replies: 4
    Last Post: 10-02-2009, 11:41 AM
  5. how to read integers from a file( novice )
    By Gorgorath in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2002, 04:25 AM

Tags for this Thread