How do I write a loop in my code? I tried to but didn't work.

Hello! I apologize posting here twice in a day (shout put to @Furry Guy for helping me out). I was told to add a loop so I added a loop and it worked until I typed in 0 in the divisor then, I re-arranged my structure but it has a lot of errors.

This is my while loop code. Can someone show me where to place it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	while (number <= 0 )
	{
		if ( number < 0)
			cout <<"\n" << " Enter a positive integer only." << endl;
		
		cout <<"\n" <<" Please enter a number: " ;
        cin>>number;
	
	    cout <<" Please enter a divisor: ";
        cin>>divisor;
        
        cout << "\n" << " " << divisor << " divides the number " << number << "." << endl;
        cout << " " << number <<"/"<< divisor << "=" << qoutient << endl;
	}


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
  #include <iostream>
using namespace std;

int main (){
	
	int number, divisor;
	
	cout<<" Please enter a number: ";
	cin>>number;
	
	cout<<" Please enter a divisor: ";
	cin>>divisor;

	if (divisor == 0 ){
	cout <<"\n" << " Error: cannot divide by zero." << endl;	
	}
	else if ( number < 0 ){
	cout <<"\n" << " Enter a positive integer only." << endl;
	}	
	else
	{
    	int qoutient = number / divisor;
    	int remainder = number % divisor;
    	
    	if (remainder)
		{
        	cout << "\n" << " " << divisor << " does not divide the number " << number << "." << endl;
        	cout << " " << number <<"/"<< divisor << "=" << qoutient << "." << remainder << endl;
		}
	    
		else 
		{
			cout << "\n" << " " << divisor << " divides the number " << number << "." << endl;
	        cout << " " << number <<"/"<< divisor << "=" << qoutient << endl;
		}
	}

	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main() {

    int number = 0 ; // initialised to a non-positive value

    while( number < 1 ) { // repeat till the number becomes positive

        std::cout << "please enter a positive integer: " ;
        std::cin >> number ;
        // note: big trouble with this loop if the user enters a non-integer as input eg. 'abcd'.
        //       we will ignore that alarming possibility for now
    }

    std::cout << "the number is " << number << '\n' ;
}
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
#include <iostream>

int main()
{
   int number { }, divisor { };

   char again { 'y' };

   while ('y' == again)
   {
      std::cout << "Please enter a number: ";
      std::cin >> number;

      std::cout << "Please enter a divisor: ";
      std::cin >> divisor;
      std::cout << '\n';

      if (0 == divisor)
      {
         std::cout << "Error: cannot divide by zero.\n";
      }
      else if (0 > number || 0 > divisor)
      {
         std::cout << "Enter a positive integer only.\n";
      }
      else
      {
         int qoutient  { number / divisor };
         int remainder { number % divisor };

         if (remainder)
         {
            std::cout << divisor << " does not divide the number " << number << " without a remainder.\n";
            std::cout << number << " / " << divisor << " = " << qoutient << "." << remainder << '\n';
         }
         else
         {
            std::cout << divisor << " divides the number " << number << ".\n";
            std::cout << number << " / " << divisor << " = " << qoutient << '\n';
         }
      }

      std::cout << "\nDo you want to enter new numbers? (enter y or n): ";
      std::cin >> again;
      std::cout << '\n';
   }
}

Please enter a number: 12
Please enter a divisor: 5

5 does not divide the number 12 without a remainder.
12 / 5 = 2.2

Do you want to enter new numbers? (enter y or n): y

Please enter a number: 15
Please enter a divisor: 5

5 divides the number 15.
15 / 5 = 3

Do you want to enter new numbers? (enter y or n): t
Last edited on
I apologize posting here twice in a day

You've written three separate threads on the same program/assignment. (Just an observation, not a reprimand.) There is nothing wrong with multiple threads if they are on different topics.

It would be better if you had continued asking questions with the first thread you created instead of splitting it. With one thread everyone reading it sees what others did to help you.

Keeping the questions together helps us help you, as well as others who have similar questions. Less duplication of effort all around.

And more people will look at a lengthy single thread and maybe offer some help. :)

When I replied to you in the previous thread I was thinking the next part of the assignment would be to add a loop. If you had continued on with that thread I would have noticed the new question(s) sooner.
Last edited on
Oh sorry about that! I will do that next time :) Thank you so so so much lol
Topic archived. No new replies allowed.