OS-X Xcode Question, as well as code.

I have to write a program as an assignment homework that outputs from a textfile as infile, the Low average temps, high average temps, Lowest temp and that city, highest temp and from that city. As I went to run this within xcode, it said it could not find the text file. Any suggestions??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  #include <iostream>
#include <iomanip>
#include <fstream>
#include <string>


using namespace std;
    
    
    int main()
    {
        ifstream infile; //input file of temperatures
        
        string City, LowCity, HighCity;
        
        int Low;
        int Lowest = 99999;
        int High;
        int Highest = 0;
       
        
        double Cities = 0;
        double Lowsum = 0;
        double Highsum = 0;
        
        
        infile.open("temps.txt");
        
        if (infile.fail())
        {
            cout << "Could not open file temps.txt" << endl;
            system("pause");
            return 0;
        }
        
        
        
        while (infile >> City >> Low >> High)
            {
            
            Lowsum += Low;
            if (Low < Lowest)
            {
                Lowest = Low;
                LowCity = City;
            }

            Highsum += High;
            if (High > Highest)
            {
                Highest = High;
                HighCity = City;
            }
            Cities++;
            
            
        }
        
        
        double Lowavg = 1.0 * (Lowsum / Cities);
        double Highavg = 1.0 * (Highsum / Cities);
        
        cout << endl;
        
        
        cout << "The average low temperature : " << fixed << setprecision(1) << Lowavg << endl;
        cout << "The average high temperature : " <<  fixed << setprecision(1) << Highavg << endl;
        cout << "The lowest low temperature : " << Lowest << LowCity << endl;
        cout << "The highest high temperature : " << Highest << HighCity << endl;
       
        
    
    system("pause");
    return 0;
    
}
I'm not familiar with the particular system you're running on, but it sounds like it may just be a problem with your program looking in the wrong place for the input file. Visual Studio causes problems like this, as it puts the executable program in different subfolders of your project depending on the type of build you make (debug, release, etc...) Have you tried copying the executable program and the data file to the same folder manually and running the program from a command prompt?

another option might be to just put the data file into a specific folder and forcing the program to look there: infile.open( "C:\Junk\temps.txt" );
that's not pretty, and probably won't work if you have to turn this in as an assignment, but it'll get the job done.
Thank you, that was the issue. For some reason XCode compiles the executable in a different location than all of the files. I had to direct link it.
Topic archived. No new replies allowed.