I am trying to learn C++ and currently constructing a program which will list the divisors of a number that is entered. I am not worried about loops or making the code "optimal" but just on how it all works etc.
I have got so far with trying to change a boolean to true based on another variable, but it never seems to change. The final COUT always returns a 0:
Please enter a whole number: 2
you entered 2
The remainder when divided by 2 is: 0
This number is even: 0 <-- should be True / 1!
Well, for a start, there's a lot of of repeated code there. You use the same algorithm for every different number you test, so you could easily have a single function for that, rather than essentially duplicating the same code 11 times.
#include <iostream>
usingnamespace std;
void Divisibility(int number);
int main()
{
int number, flag = 0;
//Get input from console
cout << "Please enter a whole number: ";
cin >> number;
while(!cin)
{
cout << "That was not a number! Please enter a whole number: ";
cin.clear();
cin.ignore();
cin >> number;
}
//set STD::|BoolApoha to return True / False
std::cout << std::boolalpha;
cout << endl;
flag = Divisibility(number);
Divisibility(int number);
return 0;
}
void Divisibility(int number)
{
int i;
bool isFactor = true;
for (i = 1; i <= 12; ++i)
if (number % i != 0)
{
isFactor = false;
cout << "divisible by " << i << " " << isFactor << endl;
break;
}
}
Unfortunately not compiling:
||=== Build: Debug in DivisibilityLoop (compiler: GNU GCC Compiler) ===|
D:\C++\DivisibilityLoop\main.cpp||In function 'int main()':|
D:\C++\DivisibilityLoop\main.cpp|22|error: void value not ignored as it ought to be|
D:\C++\DivisibilityLoop\main.cpp|23|error: expected primary-expression before 'int'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
I think I have the program flowing correctly now, but the loop is not working as i'd hoped:
expectation:
Please enter a whole number: 144
Divisible by 2 true
Divisible by 3 true
Divisible by 4 true
Divisible by 5 false
Divisible by 6 true
Divisible by 7 false
Divisible by 8 true
Divisible by 9 true
Divisible by 10 false
Divisible by 11 false
Divisible by 12 true
Reality:
Please enter a whole number: 144
divisible by 5 false
Process returned 0 (0x0) execution time : 4.803 s
I can see WHY the loop is going wrong, but I cannot see HOW it is going wrong (the loop is only picking out false cases then stopping at the first).
Where have I tripped up?
1 2 3 4 5 6 7 8 9 10 11 12 13
void Divisibility(int number)
{
int i;
bool isFactor = true;
for (i = 1; i <= 12; ++i)
if (number % i != 0)
{
isFactor = false;
}
cout << "divisible by " << i << " " << isFactor << endl;
break;
}
If you don't use braces to define a block of code to iterate in your loop, then only the first statement will be part of the loop. This means that the only lines that iterate as part of the loop are:
1 2 3 4 5
if (number % i != 0)
{
isFactor = false;
}
Lines 11 and 12 aren't part of the loop - they only execute after the loop has finished.
And if you did use braces to make them part of the loop, then that break statement would exit the loop during the first iteration anyway, so you'd still only get one line output.
#include <limits>
//...
while(!cin)
{
cout << "That was not a number! Please enter a whole number: ";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize> ::max(), '\n');
cin >> number;
}
This will ignore the entire string entered into std::cin until the newline character. Your implementation will go through the stream multiple times character by character and output to the screen multiple times, when all you need is to output the error message once.