I need a for loop to test all the possible divisors from 2 up to half the integer being tested. the user should be able to write this : 20 -2 28 22 56 and it should run through each number outputting results. The code is what I have so far...I tried using loops but it messes up all my code. What am I doing wrong or can someone send me somewhere which might be able to help me learn how to make it work?
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
//initializing int number as 0
int number = 0;
int counter = 1;
int total = 0;
char prompt;
do {
cout << "Enter five integers: " << flush;
for (int i = 0; i < 5; ++i) {
cin >> number;
while (counter < number) {
if (number % counter == 0) {
total += counter;
cout << "+" << counter;
}
counter++;
}
if (number < 1) {
cout << number << " *** ERROR: " << number << " is not greater than 1 ***\n";
}
else {
if (number > total) {
cout << "=" << total << "<" << number << " so " << number << " is deficient\n";
}
else {
if (number == total) {
cout << "=" << total << " so " << number << " is Perfect\n";
}
else {
if (number < total) {
cout << "=" << total << ">" << number << " so " << number << " is abundant\n";
}
}
}
}
}
cout << "Would you like to try again (Y/N)? ";
cin >> prompt;
} while (prompt == 'y' || prompt == 'Y');
return 0;
}
cout << "Enter five integers:" << endl;
cin >> number;
You're trying to enter five integers into a single variable, which doesn't work. You would need to use an array or better yet a std::vector to allow the user to enter an arbitrary amount of numbers. I'll give some code for the array implementation as it's the simplest.
1 2 3 4 5 6
constint max_numbers = 5;
int numbers[max_numbers] = { 0 }; // zero initialise
for( int i = 0; i < max_numbers; i++ ) {
cout << "Enter number " << i + 1 << ": ";
cin >> numbers[i];
}
Hey, I wish I could use this method but my computer science class forbids it.
I can only use basic programming in c++ with using namespace std; as much as I would love to use std:: My biggest problem is I can't use this and have to only use what is learned in class.. They take marks off if I use advanced language.... It is beyond annoying
It must come out looking like this, Im ready at this point to just hand it in how it is..
Enter five integers: 20 -2 28 22 56
1+2+4+5+10=22>20 so 20 is abundant
-2 *** ERROR: -2 is not greater than 1 ***
1+2+4+7+14=28=28 so 28 is perfect
1+2+11=14<22 so 22 is deficient
1+2+4+7+8+14+28=64>56 so 56 is abundant
Do you want to try again (Y/N)? Y
@dhayden I updated to code above...I got it working for one but it wont input the other ones how it should also..I cant figure out how to get the + sign to go away from the front
Avoid deep nesting of loops, conditionals etc., and programming becomes a lot simpler.
Excessive straight-line function length and excessive block nesting depth ... are twin culprits that make functions more difficult to understand and maintain, and often needlessly so. Each level of nesting adds intellectual overhead when reading code because you need to maintain a mental stack (e.g., enter conditional, enter loop, enter try, enter conditional, ...). Have you ever found a closing brace in someone's code and wondered which of the many fors, whiles, or ifs it matched? Prefer better functional decomposition to help avoid forcing readers to keep as much context in mind at a time.
Exercise common sense and reasonableness: Limit the length and depth of your functions.
- Alexandrescu and Sutter in 'C++ Coding Standards: 101 Rules, Guidelines, and Best Practices'