struggling with while loops

I am trying to write a code for my class that uses data from a file that contains two numbers, 100 and 200. My assignment is to write a program that counts the number of integers between the two numbers, using a while loop. I am then supposed to develop a code that displays the sum of the integers between 100 and 200 that are divisible by 6 or by 5, but not both. I also need to print the lowest number divisible by 5 or 6 and the highest number divisible by 5 or 6. I've gone through the text and am up to date in the class, but I am struggling with this code and lost. any help would be greatly appreciated. Any help would be useful here is what I have so far

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
  #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){ //counting the integers between
		inputfile >> firstnumber;
		inputfile >> secondnumber;
		firstnumber = firstnumber + 1;
		count++;
	}
	
	
	cout << "The number of integers between 100 and 200 is " << count << endl;.
Line 23-24: Why are you reading from the file again? According to the problem statement there are only two numbers in the file. You read those numbers at lines 20-21.

if I omit those two lines the program does not run
*edit actually I took those two lines out and it runs fine, I think I just left them in there when I was tinkering around with the code.
Last edited on
filename, outputfilename are unused.

 
double firstnumber,secondnumber;

Why use doubles for whole numbers?

1
2
inputfile >> firstnumber;
inputfile >> secondnumber;

can be shortened to just
 
inputfile >> firstnumber >> secondnumber;

Nope, never mind.

Similarly,
firstnumber = firstnumber + 1;
is the same as
firstnumber++
Last edited on
Ok, Ill clean the code up with these suggestions.
any ideas on how to determine the number of integers between 100 and 200 that are divisible by 5 or 6 but not both?
Use the modulo operator (%).
could you point me in the direction of learning how to use that with a while operator, multiple variables, and counting?
Here's a simple function that will return true if a number is divisible by 5 or 6, but not both.
1
2
3
4
5
6
7
8
9
10
11
12
bool filter (int num)
{   bool divisible_by_5;
    bool divisible_by_6;
    
    divisible_by_5 = ((num % 5) == 0);
    divisible_by_6 = ((num % 6) == 0);
    if (divisible_by_5 && divisible_by_6)
        return false;   //  Don't include this number
    if (divisible_by_5 || divisible_by_6)
        return true;    //  Include this number
    return false;       //  Some other number
}    

but what im trying to do is run this with all the integers between the two numbers in the .dat file. any suggestions on how combine this with the while statement?
This is getting very confusing jumping between your two threads.
Both kemort and I gave you an example of a for loop in the other thread.

kemort did with his line 28, what I did with the above filter() function, although I disagree with his adding 5 to firstnumber in his loop.
1
2
3
4
5
    for (int i = firstnumber; i <= secondnumber;  i++)
    {  if ( filter(i)) 
        {  //  Do whatever with number that is divisible by 5 or 6 but not both
        }
    }




I apologize for the confusion. I responded to kemort's post and updated the code that I have. I'll switch to just that thread. Would you mine taking a look at my updated code?
http://www.cplusplus.com/forum/general/198394/
closed account (48T7M4Gy)
although I disagree with his adding 5 to firstnumber in his loop


The first number between the two limits is first number + 1.

<= as the upper limit is incorrect.
Last edited on
The first number between the two limits is first number + 5 since firstnumber is divisible by 5.

There is nothing in the problem statement that says the first number must be divisible by 5. The example given just happens for have first number divisible by 5. Consider if firstnumber is 99. You would miss 100.

<= as the upper limit is incorrect.

Depends if you interporet "between" as an inclusive or exclusive range. If you interpret it as an exclusive range. In that case, shouldn't the loop start at firstnumber+1? For the purposes of this exercise, I interpreted the range as inclusive.

Last edited on
Topic archived. No new replies allowed.