# include %26lt;iostream.h%26gt;
# include %26lt;iomanip.h%26gt;
using namespace std;
int main ( )
{
char cont='y';
char cont2='n';
int num=0;
int curnum=1;
int math=1;
cout %26lt;%26lt; "Enter a number between 1 and 100: ";
cin %26gt;%26gt; num;
while (num%26gt;100 || num%26lt;1)
{
cout %26lt;%26lt; "Enter a number between 1 and 100: ";
cin %26gt;%26gt; num;
}
if(num==1)
cout %26lt;%26lt; curnum %26lt;%26lt; "=" %26lt;%26lt; num;
else
{
cout %26lt;%26lt; curnum;
curnum++;
while(curnum%26lt;=num)
{
math+=curnum;
cout %26lt;%26lt; "+" %26lt;%26lt; curnum;
curnum++;
}
cout %26lt;%26lt; "=" %26lt;%26lt; math;
}
do
{
cout %26lt;%26lt; "\nDo you wish to continue? Enter y or n: ";
cin %26gt;%26gt; cont;
system ("cls");
}
while (cont='y');
return 0;
}
It doesn't repeat what I want it to repeat(which is 'enter a number between 1 through 100') it only repeats
cout %26lt;%26lt; "\nDo you wish to continue? Enter y or n: ";
cin %26gt;%26gt; cont;
How do I fix this?
Do loop, c++?
You have two separate loops. You have a "while" loop and then a "do...while" loop. You probably what your "do...while" loop to encompass more of your program...will edit when I read the rest.
For your if statement, do you mean to check if num==1 everytime or do you really want if currnum == num. Otherwise, if your loop did work, you could print out a statement saying 5=1, etc.
I think your condition in your original while is incorrect. It seems like you actually want to continue while your number is less than 100 and greater than 1. Instead, you are continuing if your number is greater than 100 or less than 1.
Reply:Your initial condition is wrong : you have
while (num%26gt;100 || num%26lt;1)
...and if the user enters a number between 1 and 100, it will never be greater than 100, which is what you have. It should be
while (num%26lt;100 || num%26gt;=1)
...change your signs, and you should be good (I think - didn't look past that part).
Ok - I looked at the rest of it, and my initial response wasn't exactly correct, although it would work better if you did it that way. Although, I think I would do it like this (pseudocode)
- do
- ask user to enter a number, or n or y
- if response is not 'n', do some calcs
- while (response%26lt;%26gt;'n')
- // finishing stuff
I think something like this would work.
Reply:Move your two lines
do
{
to be above your line
cout %26lt;%26lt; "Enter a number between 1 and 100: ";
hawthorn
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment