Beginner C++ student here, first programming class. I am trying to write a program that will check to thee is any number is divisible by seven. By that I mean any number from 0 to a billion lets say. I also need to have the program loop and ask the user to try again if a number that is not divisible by 7 is entered in if an invalid input is entered.
Example:
blah
That's not even a number.
100
That's not divisible by 7
1
That's not divisible by 7
hello
That's not even a number.
105
That's divisible by 7.
Program ends.
This is what I have so far. I can't quite figure out how to get it to stop when a correct number is entered and continue on all else. Any help is greatly appreciated!!
a) Your test for divisibility is technically OK, but isn't this much nicer:
1 2 3
bool isDivisible = num%7 == 0;
if (isDivisible)
Second, if user is allowed to type in non-numbers, then the program should first read in the user's input as a string. Then it should verify whether the string can be interpreted as a number. If it can be, convert the string to a number...
1. Is die() doing anything. If not delete it.
2. It will work either way but while ... do is better in this case especially if you make !cin a condition. You can then delete the duplication of cin>> num. I.e. you don't need the 'priming read' which I think should always be avoided.
If I enter a number not divisible by seven. After the first entry I have to enter a second value and the loop then triggers. Then it prompts me to try again. However, if I enter a value divisible by 7, it will still continue the loop.
I think you should change this line unsigned divisible = (num - num / 7 * 7 == 0);
to KevinC's recomondation, purely because intergers have rounding problems with division and if the user was to enter a large number like 70001 it might evaluate it as being a number that is divisiable by 7.
If you don't understand KevinC's suggestion then google modulus operation.
Ok, I took a different route and created a nested loop. I got the most of it to work. Now when the number is not divisible by 7 it will continue to loop until I enter a value that is.
The only issue now is when I enter "blah", it will go on an infinite loop even though it's checking the variable before the Divisible as shown in the screenshot link. Any help is greatly appreciated in getting that part to work.
a)
that while(Divisible) construction is actually an obfuscation of a simpler construction, for example the first one:
1 2 3 4
if(Divisible){
if (!cin)
cout << "That's not a number, try again." << endl;
}
b)
This can't work as long as you are not reading into string.
When you are reading into int, and there is "blah" on input, the "blah" will never get red (therefore, "blah" stays on the input)
#include <iostream>
#include <limits>
usingnamespace std;
int main() {
int num = 0;
bool keepGoing = true;
while (keepGoing)
{
cout << "Please enter an integer: ";
if (cin >> num)
cout << "We have an integer " << num << endl;
else
{
cout << "We don't have an integer" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
}
This might provide a useful framework to build on.
@kemort Nice solution, although I might add that it is a bit short of perfection, at least given the original description of the problem. For example, what if a user types in "123blah"?
You'll find that the 'true' nature of stream input becomes clearer and, yes, you may need to add additional validations but I think a beginner may find lines 17 and 18 a bit daunting without making it even more obscure.
Unfortunately we've all seen academics take great pleasure in seeing freshman brought to tears over this.
PS I'll leave others to decide what state of perfection your contribution has attained.
In fact, not. The original problem does not state what the delimeters are, or that the input should be tokenized as words. So, it is ambiguous and both yours and mine interpretations are valid.
Under one intepretation (sic), this is incorrect output, while under another it is correct.
The stream ( via cin in this case ) is operating exactly as it is designed. There is no mis-interpretation by me or the system, only by you Kevvy. That's a fact. ;)
the cin is operating as designed, but I'm talking about problem description. Where does the problem description say that you can treat "123blah" as two separate tokens? Or that you cannot? Nowhere. Thus, it is ambiguous.
Okie dokie. I am getting a bit closer. Used the hint from Kevin and the very small bits of what I know about cin.clear() and what I could research and put the below together. I got the infinite loop to stop, but now the prog won't recognize numbers entered after that loop is triggered per screenshot in link. As always, help in getting this resolved is immensely appreciated.