Loops

How do you stop the infinite loop?

When I test it out by putting in the number 2. It gives me infinite statement regardless if I put >= or <=.
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
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

int main(){

	int userNum = 0;
	int factor = 0;
	cout << "Please enter a number between 3 and 99: ";
	cin >> userNum;

	while (userNum < 3 || userNum > 99){
			cout << "Please follow the directions!" << endl;
		

	}
	if(userNum % 3){
		factor = userNum / 3;
		cout << userNum << " is multiple number " << factor << " of 3." << endl;
	}




	return 0;
}
That happened because you didn't change the value of variable (userNum)
within the loop..
you should use do..while instead

1
2
3
4
5
6
7
8
9
10
11
12
13
do 
{
cout << "Please enter a number between 3 and 99: ";
cin >> userNum;

if (userNum < 3 || userNum > 99)
{
cout << "Please follow the directions!" << endl;
} 

} while (userNum < 3 || userNum > 99) ;

Its in C, pretty similar to C++. Try it. Have Fun :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <conio.h>
int main(){
    int num,f,count=0;
    printf("\nEnter # b/w 3-99: ");
    scanf("%d",&num);
    f=(num/3);
    if(num>=3 && num<=99)
        {
        printf("Your Entry is b/w 3-99");
        count++;
        }
    else
    {
        printf("Please follow the directions");
    }

        if(count>0 && (num%3)==0)
    {
        printf(" %dx3 = %d",f,num);
    }
    getch();
    return 0;
}
@Subscriber360

Our teacher didn't teach us the use of printf, scanf, and etc. so, I don't know what that means.
Like he said, that is becuase it is from C not c++.

printf = cout.
scanf = cin.

printf form - printf("Your Entry is b/w 3-99");
cout form - cout << "Your entry is b/w 3-99";

printf form - printf(" %dx3 = %d",f,num);
cout form - cout << f << "x3 = " << num;

scanf form - scanf("%d",&num);
cin form - cin >> num;
It's running well until I try to see if it work for a number that is not divisible by 3. For example, I put in the number 17, I get:

"Please enter a number 3 and 99: 17
17 is multiple number 5 of 3.
Press any key to continue . . ."

It shouldn't say it is a multiple, but it is "not" a multiple.

Here is my code:
[code]
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main(){
int num = 0;
int f = 0;
int count = 0;

cout << "Please enter a number 3 and 99: ";
cin >> num;
f=(num/3);
if(num>=3 && num<=99)
{
cout << num << " is multiple number " << f << " of 3." << endl;
count++;
}
else if (count>0 && (num%3)==0)
{
cout << num << " is not a multiple of 3.";
}
else{
cout << "Please follow the directions";
}



return 0;
}
[code]
Sorry, this may be a bit off topic but curious about something.

What is this statement you have in your program below:

1
2
while (userNum < 3 || userNum > 99){
			cout << "Please follow the directions!" << endl;


I've heard of a while loop, but this does not seem to be set up like a loop. Is there a such thing as a while statement?

Sorry, beginner here..
@Arslan It is set up as a loop. It means exactly what it says. "Run this loop as long as userName is lower than 3 OR higher than 99." e.g 1,2, 150, 544 etc.
@Arslan It is set up as a loop. It means exactly what it says. "Run this loop as long as userName is lower than 3 OR higher than 99." e.g 1,2, 150, 544 etc.


So what the difference between that and an if-statement. Wouldnt an if-statement have done the same thing?
It would have done exactly the same but only once. In the original code by the OP, the while loop is used in a wrong way. It should be like this -

1
2
3
while (userNum < 3 || userNum > 99){
cout << "Please follow the directions!" << endl;
cin >> userNum;


And it should be a do-while loop. Either way, what Im trying to say is.

if we used an if statement, we would just check if userNum was between 3 and 99 ONCE.
if we used a loop like the code I just showed above, then what is in the loop will keep getting checked until userNum IS between 3 and 99. And as long as the user keeps inputing a number that is less than 3 and more than 99, it will keep telling him to please follow directions.

calisabeth (25)
@Subscriber360

Our teacher didn't teach us the use of printf, scanf, and etc. so, I don't know what that means.


printf = cout
scanf = cin

Sorry i should have made it in c++... but C is Neat :P
Topic archived. No new replies allowed.