#include <iostream>
using namespace std;
int main()
{
int j, numb, sum;
do
{
cout << "Enter an integer between 1 and 100" << endl;
cin >> numb;
}
while ( (numb >=1)||(numb <= 100) );
sum = 0;
for ( j=numb; j>=0; j=j+1 )
{
cout <<"\n <<j<< " << endl;
sum = sum - j;
}
cout << "\n Sum = " << sum << endl;
cin.get();
}
15 Replies - 695 Views - Last Post: 04 October 2012 - 07:49 PM
#1
C++ do while loop not working right
Posted 04 October 2012 - 05:17 AM
Replies To: C++ do while loop not working right
#2
Re: C++ do while loop not working right
Posted 04 October 2012 - 05:22 AM
#3
Re: C++ do while loop not working right
Posted 04 October 2012 - 06:30 AM
#include <iostream>
using namespace std;
int main()
{
//Variables
int userInput;
int summationOfNumbers;
//
cout << "Starting Number:";
cin >> userInput;
for (int i = userInput; i < 100; i++){
userInput++;
summationOfNumbers = summationOfNumbers + userInput;
cout << userInput << endl;
}
cout << summationOfNumbers << endl;
return 0;
}
Not really sure what the rule is about spoonfeeding code around here since i just joined to help but it was easy and you should use a for loop if you need to count something. It does what you need it to do
#include <iostream>
using namespace std;
int main()
{
//Variables
int userInput;
int summationOfNumbers = 0;
//
cout << "Starting Number:";
cin >> userInput;
for (int i = userInput; i < 100; i++){
userInput++;
summationOfNumbers = summationOfNumbers + userInput;
cout << userInput << endl;
}
cout << summationOfNumbers << endl;
return 0;
}
Omg whoops forgot so assign the summation variable to 0, plese use this instead.
#4
Re: C++ do while loop not working right
Posted 04 October 2012 - 06:50 AM
while ( (numb >=1)||(numb <= 100) );
Well! Here you are asking the user to re-enter the number again if he/s enters something between 1 and 100. So, if with this condition it asks the user to enter the number again then if you flip it then it won't...
for ( j=numb; j>=0; j=j+1 )
{
cout <<"\n <<j<< " << endl;
sum = sum - j;
}
Man! What are you trying to do here? You are asked to print the sequence from that number till 100 and also sum up that sequence. Did you do that?
Your loop must start from that number as you did already and go till 100. Print the number, and add the current index to the total.
You messed up here also:
sum = sum - j;
Why use - if you want the sum?
Hope this Helps....
#5
Re: C++ do while loop not working right
Posted 04 October 2012 - 06:50 AM
The right way for the condition is putting && between the conditions. This is because, -1 will be less than 100, so one of the conditions will result true and the other will be false. So if you put || instead of && then the loop will take numb as -1. But if we put &&, then both conditions have to be satisfied. So the loop will again ask for the number.
Now, I tried with &&, but this loop still behaves in the same way. So it is better to write with an if condition.
INPUT:
cout << "Enter an integer between 1 and 100" << endl;
cin >> numb;
if(!((numb>=1)&&(numb<=100)))
{
goto INPUT;
}
This works perfectly. But I think it wont show the correct answer as there are more errors following it.
|I| for (j=numb; j>=0; j++)
This will lead you to infinite loop. As j (numb) will always be greater than 0! So the condition you should put is:
for (j=numb; j<=100; j++) //or j=j+1 (in the updation statement
|II| cout <<"\n <<j<< " << endl;
I have no idea what you are trying to do in this statement. But if you want to show the value of j every time then it should have been
cout << "\n" << j << endl;
|III| sum = sum - j;
It should be sum = sum + j;. We are adding numbers and not subtracting.
------------------------------------------
So, I'll give you a corrected one.
#include <iostream.h>
int main()
{
int j, numb, sum;
INPUT:
cout << "Enter an integer between 1 and 100" << endl;
cin >> numb;
if(!((numb>=1)&&(numb<=100)))
{
goto INPUT;
}
sum = 0;
for ( j=numb; j<=100; j=j+1 )
{
cout <<"\n"<< j << endl;
sum = sum + j;
}
cout << "\n Sum = " << sum << endl;
cin.get();
}
This post has been edited by jimblumberg: 04 October 2012 - 08:36 AM
Reason for edit:: Removed email address.
#6
Re: C++ do while loop not working right
Posted 04 October 2012 - 08:02 AM
#7
Re: C++ do while loop not working right
Posted 04 October 2012 - 08:14 AM
Blaylok, on 04 October 2012 - 06:50 AM, said:
Now, I tried with &&, but this loop still behaves in the same way.
As you obviously found out, just replacing the && with || was incorrect.
Blaylok, on 04 October 2012 - 06:50 AM, said:
INPUT:
cout << "Enter an integer between 1 and 100" << endl;
cin >> numb;
if(!((numb>=1)&&(numb<=100)))
{
goto INPUT;
}
This works perfectly.
Please do not encourage people to use goto's. That could have have been written using a do-while loop like the original code.
Blaylok, on 04 October 2012 - 06:50 AM, said:
Do not just give the answers. Help people learn on their own.
#8
Re: C++ do while loop not working right
Posted 04 October 2012 - 09:09 AM
Skydiver, on 04 October 2012 - 08:44 PM, said:
I used the goto statement because the do while statement somehow did not work, and the while statement was not the best one for that.
#9
Re: C++ do while loop not working right
Posted 04 October 2012 - 09:14 AM
#10
Re: C++ do while loop not working right
Posted 04 October 2012 - 09:57 AM
Ryano121, on 04 October 2012 - 09:14 AM, said:
+1!
Unless you just have the craving for congee... goto. But even then it's a heterogeenous mixture, rather than a solution (homogeneous mixture).
#11
Re: C++ do while loop not working right
Posted 04 October 2012 - 10:07 AM
Blaylok, on 04 October 2012 - 09:09 AM, said:
Skydiver, on 04 October 2012 - 08:44 PM, said:
I used the goto statement because the do while statement somehow did not work, and the while statement was not the best one for that.
For me this works nicely as a do-while loop, and the intent is clear:
int input;
do
{
cout << "Enter an integer in the range [-5, 5]: ";
cin >> input;
} while((-5 <= input && input <= 5) == false);
#12
Re: C++ do while loop not working right
Posted 04 October 2012 - 10:28 AM
In C#5 and VB11 GoTo is used extensively ( inside the scenes compiler generated state-machine), to power the Async & Await feature.
When it comes to the next version of C++11, I'd put money on it that it'll also use GoTo.
Edit: @Blaylok I upvoted that post, cos I'm feeling generous.
This post has been edited by AdamSpeight2008: 04 October 2012 - 10:33 AM
#13
Re: C++ do while loop not working right
Posted 04 October 2012 - 11:37 AM
Does that mean GOTO should never be used? No, but it does mean it should NOT be used where another better solution exists, such as this case. It is extremely rare that GOTO is the right decision.
#14
Re: C++ do while loop not working right
Posted 04 October 2012 - 12:35 PM
Quote
Set j to start at the number the user enters. And loop to 100.
for(j = num; j < 100; j++)
Quote
Why are you subtracting j from sum if we want the sum. Use addition
#15
Re: C++ do while loop not working right
Posted 04 October 2012 - 01:15 PM
There are a few times where goto might be worth using. A bare few. But I submit that if you find yourself in that situation, you probably could refactor the code around it to make goto less necessary in the first place.
|
|

New Topic/Question
Reply




MultiQuote







|