Combination of while and do-while

I need HELP!!

I can't figure out what's the prob with this code. Can someone help me.

*** This is the output
Enter 1st Number: 3
Enter 2nd number: 7
34567
Do you want to try again [y/n]?


#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int num1, num2, ans;
cout<<"Enter 1st NUmber: ";
cin>>num1;
cout<<"Enter 2nd Number: ";
cin>>num2;
while(num1<=num2)
cout<<num1;
num1++;
cout<<"Do you want to try again [y/n] ?";
cin>>ans;
}while(ans=='y')
getch();
}
There are a number of issues with your program.

1. don't use <iostream.h> - try without .h
2. Use a do while loop and put most of your code inside it.
3. You have declared ans as an int but yet you have it taking a character as its value.
4. Your first while loop needs parentheses {}
5. getch() should be _getch()
6. use #include <cstdlib> and system ("CLS"); instead of clrscr();

This is based on you wanting the program to get 2 numbers from user, test if number1 is less than or equal to number2, if true output number and then add 1 and retest. If number1 is greater number 2 ask user if they want to try again. If yes repeat, if no exit.
I actually using non-standard so, It's okay. Now, I tried it but I still don't get the desired output.
Hi @catherine,

I think that
while loop+for loop
it is a better approach
-but i could be wrong-
anyway

Check this
example:


Enter 1st number: 3
Enter 2nd number: 7
34567

Do you want to try again [y/n] ? Y
Enter 1st number: 7
Enter 2nd number: 3
76543

Do you want to try again [y/n] ? 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
48
49
50
//numbers.cpp
//##

#include <iostream>
using std::cout;
using std::cin;
using std::endl;


int main(){

	char choice;
	bool theUserWantToContinue=true;
	int num1,num2;

	while(theUserWantToContinue){
		cout<<"Enter 1st number: ";
		cin>>num1;
		cout<<"Enter 2nd number: ";
		cin>>num2;

		if(num1<=num2){
			for(int i=num1;i<=num2;i++)
				cout<<i;
		}else{
			for(int i=num1;i>=num2;i--)
				cout<<i;
		}//end if..else
		cout<<endl;
	cout<<"\nDo you want to try again [y/n] ? ";
	cin>>choice;
	
	while(!(choice=='y'||choice=='Y'||choice=='n'||choice=='N')){
		cout<<"[y/n] (y,Y,n,N)? ";
		cin>>choice;
	}//end while

		switch(choice){
			case 'n':
				theUserWantToContinue=false;
			break;
			case 'N':
				theUserWantToContinue=false;
			break;
		}//end switch
	}//end while
	

return 0; //indicates success
}//end main 
Hi @eyenrique.


Thanks for the help, but my instructor said its a while and do-while.
But you have a point.
clrscr(); does not work on my compiler with those libraries.

Here is how to think about it:

1
2
3
4
5
6
7
8
9
10
11
12
13
loop:
step 1: clear screen
step 2: enter a number
step 3: enter another number

   loop if number 1 less than or equal to number 2?
         step 6: output number1
         step 7: add 1 to number1
   end loop
step 8: do you want to try again?
end loop (while yes repeat)

else exit program


You are going to have a while loop within your do while loop so you need to work out where the do while loop will go and the test condition to repeat.
@catherine i am not
agree with your
teacher!
-anyway-

i can imagine
this code using
do-while+while


Enter 1st number: 1
Enter 2nd number: 5
12345

Do you want to try again [y/n] ? Q
[y/n] (y,Y,n,N)? Y
Enter 1st number: 7
Enter 2nd number: 0
76543210

Do you want to try again [y/n] ? 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
48
49
50
//numbers.cpp
//##

#include <iostream>
using std::cout;
using std::cin;
using std::endl;


int main(){

	char choice;
	bool theUserWantToContinue=true;
	int num1,num2;

	do{
		cout<<"Enter 1st number: ";
		cin>>num1;
		cout<<"Enter 2nd number: ";
		cin>>num2;

		if(num1<=num2){
			while(num1<=num2){
				cout<<num1;
				num1++;
			}//end while
		}else{
			while(num1>=num2){
				cout<<num1;
				num1--;
			}//end while
		}//end if..else
		cout<<endl;
	cout<<"\nDo you want to try again [y/n] ? ";
	cin>>choice;
	
        //while choice is not (y/Y or n/N) prompt to the user again
	while(!(choice=='y'||choice=='Y'||choice=='n'||choice=='N')){
		cout<<"[y/n] (y,Y,n,N)? ";
		cin>>choice;
	}//end while

	if(choice=='n'||choice=='N')//if the choice is n/N : Exit
            theUserWantToContinue=false;

	}while(theUserWantToContinue); //end do-while
	

return 0; //indicates success
}//end main 
Last edited on
@RabMac,
so I going to use if else statement??

your using standard that's why clrscr does not work in your compiler.
Last edited on
You don't have to use if else as the do while does it for you.

The way the do while works is that it does everything in the block at least once. When it gets to the end it will test your condition, if that condition is met the do while repeats. If the condition is false then the do while stops.

The format of do while is as follows:

1
2
3
4
do
{
     include what you want it to do in this block
}while(your test condition);
@eyenrique,

I'm not also agree with my teacher but I have to follow her. Anyway, your code seems pretty complicated. Thanks for the effort.
You are nearly there Catherine as there was not much wrong with your initial code. I know you can do it and hopefully the pointers we have given will help you solve it yourself.

If your next attempt does not work post your new code and we will try and give you some more hints.
@catherine i
just can do it
a little more
simpler!

Look at
the code
again!
Last edited on
@RabMac,

thanks, for your willingness to help me. Still does not work. Here's my new code.


#include <iostream.h>
#include <stdio.h>
#include <conio.h>

void main(){
clrscr();
int num1, num2;
char ans;
cout<<"Enter 1st Nmber: ";
cin>>num1;
cout<<"Enter 2nd Number: ";
cin>>num2;
do{
while(num1<=num2){
cout<<num1;
num1++;
}
cout<<"Do you want to try again [y/n] ?";
cin>>ans;
}while(ans=='y');
getch();

}
@eyenrique,
I appreciate your effort.
Well that is looking pretty close to the final solution.

You have declared your variables correctly. Your while loop is working correctly. Your do-while loop is laid out correctly and with the correct test condition.

I am assuming that the program works for the first set of numbers you input but when the user selects y to continue they are not being prompted to enter the numbers again?

If that is the case you need to think about where you place your cin and cout statements.

I hope this helps and please let us know how you get on.
Still does not work.

How does the outcome differ from expected?

What should be repeated?
Now you repeat showing the numbers from num1 to num2, but since num1 exceeds num2 already during first time there is no output on repeats (except the "Do you want ...").

You propabably want to (re)read both num1 and num2 every time.
Last edited on
@RabMac,

I get it now. Thanks..
Topic archived. No new replies allowed.