PDA

View Full Version : please help with intro c++ code (input file)




SpQr4
Nov 27, 2011, 07:30 PM
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;

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



Mac_Max
Nov 28, 2011, 12:11 AM
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
Nov 28, 2011, 03:41 AM
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.

robvas
Nov 28, 2011, 09:39 AM
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