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

    Joined:
    Nov 27, 2011
    Messages:
    1
    #1
    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

    Code:
    #include <iostream.h>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    int main (){	
    
    ifstream fin("payroll1.txt");
    	int employeeid, hoursworked, hourlyrate;
    	float grosspay;
    	
    	cin>>employeeid>>hoursworked>>hourlyrate;
    	while(fin>>employeeid>>hoursworked>>hourlyrate){
    		grosspay=hoursworked*hourlyrate;
    		cout<< "The Employee ID is"<<employeeid<<"The Gross Pay is" << grosspay<<endl;
    		cin>>employeeid>>hoursworked>>hourlyrate;
    		
    
    }//While
    	fin.close();
    
    }//Main

    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?

    /Users/malishka31/Documents/module22/Module2_3A.cpp
    /Users/malishka31/Documents/module22/payroll1.txt
     
  2. macrumors 6502

    Joined:
    Mar 8, 2004
    Messages:
    404
    #2
    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

    Joined:
    Apr 24, 2008
    Messages:
    471
    #3
    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

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

    Joined:
    Mar 29, 2009
    Messages:
    1,782
    Location:
    USA
    #4
    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