C++ problem

Discussion in 'Mac Programming' started by CuteBoA, Mar 12, 2010.

  1. CuteBoA macrumors newbie

    Joined:
    Mar 12, 2010
    #1
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main () {
    	
    	ifstream fin;
    	fin.open ("Marks.txt");
    	
    	string n;
    	double m;
    	 
    	 cout << "Number of students: " << numStuds;
    	
    	fin.close();
    	
    	return 0;
    }


    so, basically im trying to read the datas that are in the file, and then put it on the the arrays, and output how many datas there are..
    if the datas are like this :
    james 89
    doni 80
    ben 87

    but, when i output it, the number of students stays 0. why is that?
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    So name and mark are arrays. You need to subscript them to use >>. you also need to put a lot more than you have in the loop. You want to read into name and mark sub n.

    -Lee
     
  3. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #3
    I see you've edited your original post to reflect changes implied with lee1210 response.

    My question would be - have you determined if it successfully opened the file "Marks.txt"?
     
  4. CuteBoA thread starter macrumors newbie

    Joined:
    Mar 12, 2010
    #4
    yes, ive checked it...
    the program works fine when i tried to compile it in microsoft's computer..
    so, whats wrong with it?
     
  5. jav6454 macrumors P6

    jav6454

    Joined:
    Nov 14, 2007
    Location:
    1 Geostationary Tower Plaza
    #5
    I had this problem for my C++ course. My teacher warned us against using the 'string' function when using the array. He basically told us you'll get an F.

    Long story short, we had to use

    Code:
    #iostream
    #fstream
    using namespace std;
    
    char students[50];
    int grades[50];
    .
    .
    .
    
    void input_stu_n_grades(char students[], int grades[], ....){
    fin.open("filename.txt")
    if {
    .
    .
    
    }
    else
    .
    .
    }
    
    void output_file(students, grades, ....){
    .
    .
    }
    
    void output_screen(....){
    .
    .
    .
    }
    
    int main(){
    
    input_stu_n_grades(students, grades, ....);
    output_file
    output_screen
    
    return 0;
    }
    
    
    Along those lines... Try using it that way?
     
  6. CuteBoA thread starter macrumors newbie

    Joined:
    Mar 12, 2010
    #6
    How can u use char to read it?

    because we need to read " Jean" but not only "J" right?
    how does it make sense then?

    How can it run and work perfectly fine with microsoft's compiler when it doesnt even work here when i used XCODE? any thought about it?
     
  7. i.yalovecky macrumors member

    Joined:
    Feb 24, 2010
    #7
    did you ran in debug mode? what is value of n after the loop?
     
  8. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #8
    An array of chars can store more than one character :).
     
  9. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #9
    Turbo C (by Borland) is really old. How are you running it "in Turbo C" on your mac..? I'm not aware of any port of BGI (Borland Graphics Interface, if I remember correctly) to current Macs. Perhaps you can check here?
     
  10. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #10
    I believe the file is NOT being opened successfully not because it doesn't exist but because it is not where you, or the code, thinks it is!

    In the "Project" menu select "Edit Active Executable <your project name here>".

    Make sure the "General" view tab is selected.

    Set the radio-button group "Set the working directory to" "Project Directory" if the file "Marks.txt" is sitting in the same folder as your Xcode project file.

    Run it again, using the Debugger is necessary.
     
  11. CuteBoA thread starter macrumors newbie

    Joined:
    Mar 12, 2010
    #11
    what do u mean by Set the radio-button group "Set the working directory to" "Project Directory" if the file "Marks.txt" is sitting in the same folder as your Xcode project file.?? i cant find it anywhere


    EDIT : OMG.. ive found it and it WORKS properly.. thanks alot lloyddean for your help!! because this so important as my teacher's assignment!! thanks alot!!!!
    so, whenever i want to call a file , I will need to set the working directory to " PROJECT DIRECTORY"? is it like that?

    thanks!!!
     
  12. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #12
    During development when invoking CLI tools from within Xcode - YES!

    If invoked from the terminal change your working directory to the one containing the data file, or supplying a complete path/name pair, and then invoking the executable and it'll of course work as expected.
     

Share This Page