0

Here is the code I am working with right now, I get some odd output when I type in my file location of some memory location (8128231) which I find odd. I understand I need to add a parameter if the user inputs the wrong file location and will prompt them to do it again, I'll do that later.

I am currently stuck with the while loop where, I want the while loop to read through my text file that has 4 columns with different variable types in the order that is shown below within the while loop:

name        gender school score

I know that I can add a string type of some sort like, while(inFile >> lineIn) and then read the file based off what I have, but I want to do it the way that I'm currently working with that include the eof statement, because I know there's a way to get around the white space so that the file outputs what needs to be shown from the file. How can I do this?

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main() {
//declarations

    char M, F;
    string name, gender, school, fileLocation, lineIn;
    int femaleScore, maleScore, ccScore, univScore, ccTotal, univTotal, femaleTotal, maleTotal, score;


    //stream variables
    ifstream inFile;
    ofstream outFile;

    //prompt for user for file location
    cout << "Enter file and name for the scores " << endl; //C:\Users\ctach\Documents\CS161\Text Downloads
    cin >> fileLocation;
    //opens the text file
    inFile.open(fileLocation); 

    while (!inFile.eof()) { //reads file until end of file (eof)

        inFile >> name >> gender >> school >> score; //reads the .txt file in this order

        cout << name << gender << school << score << endl; //outputs the .txt file in this order

         if (gender == "M") {

         }
    }
cout << "Press any key to continue..." << endl;
cin.ignore();
cin.get();
return 0;
Biffen
  • 5,354
  • 5
  • 27
  • 32
Iseez Ice
  • 19
  • 2
  • Okay that thread helped for the second bit, so why might I be getting an odd output when putting in the path file location when being given that specific prompt? – Iseez Ice Feb 12 '18 at 05:57
  • What about inspecting your code by stepping through line by line with the debugger? –  Feb 12 '18 at 06:00
  • Did you debug it (step by step)? – Scheff's Cat Feb 12 '18 at 06:00
  • yeah I found the issue with wherever my .txt file was located, whatever I was copying from my file explorer, wasn't working until I moved the file somewhere else... thanks for the help – Iseez Ice Feb 12 '18 at 06:17

0 Answers0