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

SpQr4

macrumors newbie
Original poster
Nov 27, 2011
1
0
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
 
Last edited by a moderator:

Mac_Max

macrumors 6502
Mar 8, 2004
404
1
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).
 

Sander

macrumors 6502
Apr 24, 2008
480
7
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.
 

robvas

macrumors 68040
Mar 29, 2009
3,054
517
USA
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.