Need help with input file.

When I compile this program I do not receive an error message, but when I run it, the age and factor will not output. I have never wrote a program where the input file is a constant file. I always ask for the user to input the file name and then go from there.. So I believe my problem has to do with the file not being opened.

my professor said that the input file as to be named "adjustFactors"

Thanks in advance for any help.

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
 
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <fstream>
   using namespace std;

   void GetAdjustFactors(int, float&);

   int main()
{
   int age;
   float factor;

   GetAdjustFactors(age, factor);


   return 0;
}

   void GetAdjustFactors(int age, float& factor)
   //WHAT DOES FUNCTION DO
{

   ifstream inFile;
   string adjustFile;
   int inAge;
   float inFactor;


   cout << "Enter the participant's age: ";
   cin >> age;

   inFile.open(adjustFile.c_str());

   inFile >> inAge >> inFactor;

   while (inFile)
   {
      if (age == inAge)
      {
        inFactor = factor;
      }
      inFile >> inAge >> inFactor;

   }
   cout << age << factor;

}



Can you post the file contents?
20 0.94
25 1
30 0.95
35 0.93
40 0.9
45 0.85
50 0.82
55 0.79
60 0.77
65 0.75
70 0.7
75 0.66
80 0.59
85 0.55
90 0.54
95 0.51
Line 27
 
    string adjustFile;

The string is not assigned any value, so it is initialised with the default which is an empty string "".

Line 35 - the program attempts to open the file named "".

my professor said that the input file as to be named "adjustFactors"

Maybe you are confusing the variable name with the value contained in that variable.
Last edited on
These are his exact words from the rubric, "The adjustment factors are in the file called adjustFactors. Do use this exact filename"

I have always had the program prompt for the user to input the file name and then open it up.. I've never had to open a file without prompting the user.
Yes, that was understood from the previous posts. So you need to open the file named "adjustFactors", rather than the file named "" as in your current code. (In any case the empty string isn't a valid file name).
1
2
3
4
5
	string apple;
	cout << "apple: " << apple << '\n';	
	
	string fruit = "apple";
	cout << "fruit: " << fruit << '\n';

note the output:
apple:
fruit: apple
apple is an empty string, while fruit contains the string "apple".
Last edited on
Here is the updated code with your suggestions... However the program is still not opening the file up. Am I doing something incorrectly?


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
 void GetAdjustFactors(int age, float& factor)

{

   ifstream inFile;
   string adjustFile = "adujustFile";
   int inAge;
   float inFactor;

   cout << "Enter the participant's age: ";
   cin >> age;

   inFile.open(adjustFile.c_str());

   inFile >> inAge >> inFactor;

   while (inFile)
   {
      if (inAge == age)
      {
        factor = inFactor;
        break;
      }
      inFile >> inAge >> inFactor;

   }
Last edited on
Your file name is adujustFile, but what's the extension of the file? is it .txt? or is it .dat? You need to specify the file extension.

e.g, adujustfile.txt
Topic archived. No new replies allowed.