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 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
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
//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
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);
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.
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.
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.