How to run more the one input on one line

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?

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
using namespace 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;
}
Last edited on
You'll need a variable for each and you'll need to use cin for each one. You can string them in the same line.

But if the user hits "Enter", there will be a newline character left in the buffer that you'll have to take care of, just keep that in mind.

 
cin >> number1 >> number2 >> number3 >> number4 >> number5;
1
2
  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
const int 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];
}
Hello kattykat243,

One possibility you could do id wrap lines 14 - 42 in a do while loop. Then move line 14 above the do. Then at the end this is what I did:

1
2
3
4
5
    }  // Closig of the 1st else
    lp++;  // <--- Added next 3 lines.
    counter = 1;
    total = 0;
} while (lp < 5);  // <--- End of Add. 


lp is just defined as an int to allow the do while to loop 5 times.

Hope that helps,

Andy

Edit: This is what I did at the beginning of the program:

1
2
3
4
5
6
7
8
9
10
11
12
13
	int number = 0, lp{ 0 };  // <--- Added lp.
	int counter = 1;
	int total = 0;

	std::cout << "Enter five integers > than 1:\n" << std::endl;  // <--- Moved up.
	
	do
	{
		std::cout << "Number " << (lp + 1) << ": "; // <--- Added.
		std::cin >> number;
		std::cout << std::endl;

		while (counter < number) {
Last edited on
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


Start simple. Here's a loop that prompts for 5 numbers and prints them out:
1
2
3
4
5
    cout << "Enter 5 numbers: " << flush;
    for (int i=0 i<5; ++i) {
        cin >> number;
        cout << "Looking at " << number << '\n';
    }

Now add code inside the this loop to find divisors. Note that this will involve another loop.

Once you can print out the divisors, add code to add them up.

Then add code after the inner loop that will say whether the number is perfect, deficient, or abundant.
Thank you this is very helpful. :) I'm going to try it out tonight and post an update
I updated my code...I'm having issues with the + sign and getting it to output = and the sum greater then or less then.
@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'


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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>

int get_sum_divisors( int number, bool noisy = false )
{
    int sum_divisors = 1 ;
    if(noisy) std::cout << "number == " << number << "  sum of divisors = 1" ;

    for( int div = 2 ; div <= number/2 ; ++div )
    {
        if( number%div == 0 )
        {
            if(noisy) std::cout << " + " << div ;
            sum_divisors += div ;
        }
    }

    if(noisy) std::cout << " = " << sum_divisors ;
    return sum_divisors ;
}

void print_results_for( int number ) // invariant: number > 1
{
    const int sum_divisors = get_sum_divisors( number, true ) ; // true for noisy

    if( sum_divisors > number ) std::cout << " (greater than " << number << ", abundant)\n" ;
    else if( sum_divisors == number ) std::cout << " (equal to " << number << ", perfect)\n" ;
    else std::cout << " (less than " << number << ", deficient)\n" ;
}

bool try_again()
{
    std::cout << "\ndo you want to try again (y/n)? " ;
    char c ;
    std::cin >> c ;
    return c == 'Y' || c == 'y' ;
}

int main()
{
    const int NUM_INTEGERS = 5 ;

    do
    {
        std::cout << "\nenter " << NUM_INTEGERS << " integers greater than one: " ;

        int number ;
        for( int i = 0 ; i < NUM_INTEGERS && std::cin >> number ; ++i )
        {
            if( number < 2 ) std::cout << "*** ERROR: " << number << " is not greater than 1 ***\n" ;
            else print_results_for(number) ;
        }

    } while( try_again() ) ;
}

Topic archived. No new replies allowed.