please help with intro c++ code (input file)

Discussion in 'Mac Programming' started by SpQr4, Nov 27, 2011.

  1. SpQr4, Nov 27, 2011
    Last edited by a moderator: Nov 27, 2011

    macrumors newbie

    Nov 27, 2011
    Good Evening. I am taking an intro c++ class. No one in the class appears to be on a mac and the book i have is using visual basic, i have spent hours just trying to figure out xcode and how to get into c++.

    This is my program, nothing happens when i run it but i get no errors.
    I think i saved payroll1.txt in the correct place, it is in xcode, right below the "groups and files" left pane

    #include <iostream.h>
    #include <iostream>
    #include <fstream>
    using namespace std;
    int main (){	
    ifstream fin("payroll1.txt");
    	int employeeid, hoursworked, hourlyrate;
    	float grosspay;
    		cout<< "The Employee ID is"<<employeeid<<"The Gross Pay is" << grosspay<<endl;

    I checked file paths and they are both in the same file, is this correct? should it read from the file given where it is located?

  2. macrumors 6502

    Mar 8, 2004
    You need to place the text file where the executable file is. Generally this will be in a /bin folder (I'm not in front of my Mac right now so I can't take a look at the exact folder).
  3. macrumors 6502

    Apr 24, 2008
    Note that as of Xcode4, it's quite difficult to locate the built executable yourself. Use Xcode's "reveal in Finder" option. A different approach would be to use a full path for your text file, i.e. something like

    ifstream fin("/users/john/payroll1.txt");
    Then no matter where your executable is run from, it will always find the file.
  4. macrumors 68000

    Mar 29, 2009
    Don't bother with Xcode for such simple things.

    Edit your source code files with TextWrangler or SublimeText2.

    When it's time to compile, simply open a terminal and change to the directory that has your files:

    $ gcc module2_3a.cpp -o module2_3a
    $ ./module2_3a

Share This Page