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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
usingnamespace 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.
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?
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)
returnfalse; // Don't include this number
if (divisible_by_5 || divisible_by_6)
returntrue; // Include this number
returnfalse; // 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?
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/
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.