Web Analytics Made Easy -
StatCounter Reading data from a text file? - CodingForum

Announcement

Collapse
No announcement yet.

Reading data from a text file?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reading data from a text file?

    Hello friends!

    Okay, so I am new...

    Both to coding and to the forum. I read over the Rules and Guidelines

    but if I'm doing anything wrong just yell at me and I'll fix it.


    For an assignment I need to write a program that will read data from a .txt file and calculate a final grade for a student.

    All input must come from the .txt file. The program should open the file and display the students name and final grade for the class.

    the data in the file is arranged like this.

    Sandra King 75 62 84 98 96 86 70
    Jason Briggs 82 71 65 72 81 95 90
    Tina Simpson 55 60 62 54 55 68 31

    I've made two string variables for the first and last name and 7 "double" variables for the grades.

    what i've written so far is

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;



    int main() {

    string FN,LN

    double Q1,Q2,Q3,Q4,Q5,MT,FNL;


    ifstream inputFile;


    inputFile.open("grades.txt");



    inputFile >> Q1;

    cout << FN << Q1;




    system("pause");


    return 0;
    }

    I wrote this as sort of a test to see what information would print to screen. But all im getting are numbers in scientific notation.

    I know it's not much. But if I could just figure out how to get the program to display the data I feel like I could figure the rest out.

    How on earth do I get the program to distinguish between the first and last name on the line? and each number after?

    I've been searching through the internet and nothing I've found has helped me out much.

    I'm not asking you guys to do my homework. But If you could please nudge me in the right direction it would be greatly appreciated.

    I dont mind looking things up myself. So even if you could just give me the correct terms I should be searching for.

    All i need to know how to do is have the program pull the correct variables when I call for them and I'm set.

    Thanks for having a look! Cheers.

  • #2
    Figured it out.

    Hello again folks. Well I see no one replied, that's fine. I was pretty lost.

    I ended up figuring it out. I thought I would go ahead and post it to help anyone else that was in my position.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    
    
    
    int main(int argc, char** argv) {
    
    // This line declares the ifstream object as 
    //"myFile" you can name it whatever you want. 
    
    ifstream myFile;
    
    // This line is for the name variables 
    
    string fname, lname;
    
    //and this is for the numbers 
    
    double Q1,Q2,Q3,Q4,Q5,MDT,FNL;
    
    // This line opens your file. I use the direct address here because my assignment asked for it.
    // but you could also use "myfile.open ("example.txt");" in the same way. 
    
    myFile.open("C:/Users/USERNAME/Downloads/grades.txt"); 
    
    
    
    //This while loop cycles through the data and allows you to name your variables. 
    //C++ automatically moves the cursor through white space to each new word or letter. 
    // so all you need to do is put in your variable names in the correct order. 
    
    while (myFile >> fname >> lname >> Q1 >> Q2 >> Q3 >> Q4 >> Q5 >> MDT >> FNL)
    {
    	//this line prints the data. In the same line you can perform the required mathematics for your variables and print to screen.
     
    	cout << lname << (", ") << fname << endl;
    }
    
    
    //closes file
    
    myFile.close(); 
    
    return 0;
    
    }
    Thanks to anyone that took the time to look at my problem.
    Good luck to anyone stuck on this.

    Comment

    Working...
    X