inFile problems

I'm trying to do a real simple read of an inFile. I'm using a .dat file that contains "100"

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;


int main() {
int a;
ifstream inFile;



inFile.open("number.dat");
inFile >> a;
cout << a;

system("pause");
return 0;


the cout returns a 6 digit number that is not 100. What am I missing here. This is my third c++ assignment.
Your program is correct, I have tested it at my own computer. There must be something wrong with your 'number.dat' file. Make sure that your number in the file is saved as ASCII.
Is your file human-readable? I'd guess that the binary encoding of ASCII "100" (0x313030) is either six or seven decimal digits.

Check to make sure that "number.dat" is actually the right file, and perhaps try changing
1
2
ifstream inFile;
inFile.open("number.dat");
to
1
2
ifstream inFile;
inFile.open("number.dat", std::ios::binary);
Last edited on
closed account (48T7M4Gy)
Initialise a = 12345 in the first line of main and run again. If it prints out 12345 then the data file is not being read because it is in the wrong directory.

You should always initialize variables and you should check that the file is open.

See http://www.cplusplus.com/doc/tutorial/files/ for a couple of samples of how and what to do to check file is open.

Here is what I'm trying to do. I fixed the infile problem by moving deleting the first .dat file and creating another one. I have written the code that reads the integers from the .dat file and then counts how many integers are between the two numbers in the file, but now I'm stuck. Can any of yall give me a hint as to how to do number 2-5. I think i'm supposed to use the modulo operator but I'm not sure how to link that with a while loop. Any input would be helpful
Write a program that reads two integers from the text file integers.dat. The first integer should be considered the low number and the second integer the high number. The program should loop through all integers between the low and high values. The loop should include the low and high integers.

1. Displays the number of integers in the file

2. Displays the number of integers divisible by 5 OR 6, but not both.

3. Displays the sum of the integers from condition 2 as well as the sum of all the integers

4. Displays the largest integer and the smallest integer that satisfy condition number 2.

5. Computes the average of the largest and smallest integer


Assume the contents of integers.dat (the file is being provided for you) are as follows:

100 200

Here's the code I have so far.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main(){
string filename, inputfilename, outputfilename;
ifstream inputfile;
double firstnumber,secondnumber;
int count = 0;
int sum = 0;


cout << "Input Filename: ";//calling user to type file name in
getline(cin, inputfilename);//code required to open file
inputfile.open(inputfilename.c_str());
cout << endl;
inputfile >> firstnumber;
inputfile >> secondnumber;
while (firstnumber <= secondnumber){
firstnumber = firstnumber + 1;
count++;
}

cout << "The number of integers between 100 and 200 is " << count << endl; //after the code runs it changes the value of firstnumber, I don't want that to happen


// after the code runs it changes the value of firstnumber, I don't want that to happen
What do you mean?
after the code runs it changes the value of firstnumber, I don't want that to happen

Then don't increment firstnumber. Use a for loop instead.
1
2
3
4
 
  for (int i=firstnumber; i<=secondnumber; i++)
  {  // Do whatever in your loop
  }


It's not a good idea to use doubles for firstnumber and second number. doubles are approximations.

Please see my response in your other thread regarding selecting numbers divisible by 5 or 6, but not both.
http://www.cplusplus.com/forum/beginner/198534/#msg950987

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


Last edited on
closed account (48T7M4Gy)
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main(){
    string filename, inputfilename, outputfilename;
    ifstream inputfile;
    double firstnumber,secondnumber;
    int count = 0;
    int sum = 0;
    
    cout << "Input Filename: ";//calling user to type file name in
    getline(cin, inputfilename);//code required to open file
    inputfile.open(inputfilename.c_str());
    
    inputfile >> firstnumber >> secondnumber;
    
    //PART 1 read the 2 numbers from the file
    count = secondnumber - firstnumber - 1;
    cout << "Count: " << count << endl;
    
    //PART 2
    for (int i = firstnumber + 1; i < secondnumber; i++)
    {
        if ( (i % 5 == 0 or i % 6 == 0) && i % 30 != 0)
        {
            cout << i << ' ';
        }
        cout << endl;
    }
    
    //PART 3

// add a couple of lines to part 2 to calculate the two sums required and display results here
// you need two sum variables - sum , and sum_of_condition_result
    
    return 0;
}
Last edited on
thank you. I created another post and I apologize for not returning to this one sooner. Here is the code I have

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main(){
	string filename, inputfilename, outputfilename;
	ifstream inputfile;
	int firstnumber, secondnumber;
	int count = 0;
	int sum = 0;
	int resultofdiv=0;
	int resultoftotal = 0;
	int largestnumberdivisible = 0;
	int smallestnumberdivisible = 0;
	double average = 0;
	
	
	cout << "Input Filename: ";//calling user to type file name in
	getline(cin, inputfilename);//code required to open file
	inputfile.open(inputfilename.c_str());
	cout << endl;
	inputfile >> firstnumber;
	inputfile >> secondnumber;
	average = (firstnumber + secondnumber) / 2;
	smallestnumberdivisible = secondnumber;
	cout << "The number of integers between "<<firstnumber<<" and "<<secondnumber<<" is " << (secondnumber-firstnumber)+1 << endl;
	while (firstnumber <= secondnumber){
		resultoftotal += firstnumber; // sum of all integers between first and last
		bool divisible_by_5; 
		bool divisible_by_6;
		divisible_by_5 = ((firstnumber % 5) == 0); //bool statement, divisible by 5, no remainder
		divisible_by_6 = ((firstnumber % 6) == 0); //bool statement, divisible by 6, no remainder
		if (divisible_by_5 != divisible_by_6){ //filtering integers that are divisible by both
			resultofdiv += firstnumber;//sum of integers divisibly by 5 or 6
			count++;//number of integers divisible by 5 or 6
			largestnumberdivisible = firstnumber; //the last number counted will be the largest divisible by 5 or 6
			if (firstnumber <= smallestnumberdivisible){// not sure about this part!
				smallestnumberdivisible = firstnumber;
				}
			}>
		firstnumber = firstnumber + 1;// eventually closes the loop
	
		}
	
	cout <<"There are "<< count <<" numbers divisible by 5 or by 6 but not both"<< endl;
	cout << "The sum of the integers divisible by 5 or by 6 but not both is " << resultofdiv << endl;
	cout <<"the sum of all integers is "<< resultoftotal <<endl;
	cout <<"The largest number divisible by 5 or 6 but not both is "<< largestnumberdivisible << endl;
	cout <<"The smallest number divisible by 5 or 6 but not both is "<< smallestnumberdivisible << endl;
	cout << "The average of the largest and the smallest integer is " << average<< endl;
	system("pause");
	return 0;
}


I had help with a lot of this code.
can anyone explain the how line 40 wokrs?
closed account (48T7M4Gy)
can anyone explain the how line 40 wokrs?

So why did you write it?
The only c++ tutor at my school is japanese and has a real thick accent. I asked her why I was doing that and there's was a translation issue. My assignment is complete but I want to understand the code.
The only c++ tutor at my school is japanese and has a real thick accent. I asked her why I was doing that and there's was a translation issue. My assignment is complete but I want to understand the code.


I think the code from your c++ tutor does not look very good if you ask me.
Well this is only the third code I've written so I'm not too concerned with perfection. The only part I truly don't understand is the line I mentioned. Just looking for clarity on the way it works.
closed account (48T7M4Gy)
The point a couple of us here have tried to make, and it seems clear from the assignment question, is that firstnumber and secondnumber are the only numbers on the file and they should not and don't need to be changed. By choosing the path of modifying the two, you are tieing yourself up in knots unnecessarily.

The two numbers are just the limits of the loop whether that loop is a while loop or a for loop.

Line 40, because of this, is unnecessarily complex. What you should do is make a clear statement (here) of the following:
1. What do you want that part of your code to do?
2. What is the expected outcome?

Just in plain English (or Japanese if you like), not in code.
Topic archived. No new replies allowed.