c++ (wrong code)

Thread Starter

TAKYMOUNIR

Joined Jun 23, 2008
352
please can some one fix this code i try to do
ask the user to enter positive number if he enter negative number the computer ask him again to enter positive number and keep ask him until the user enter positive number when the user enter positive number the computer make summation for this number and out put result on screen for example if he enter 10 the result should be 55 (1+2+3+4+5+6+7+8+9+10)
can some one please check what wrong i did
thanks
 

Attachments

Last edited:

steveb

Joined Jul 3, 2008
2,436
please can some one fix this code i try to do
ask the user to enter positive number if he enter negative number the computer ask him again to enter positive number and keep ask him until the user enter positive number when the user enter positive number the computer make summation for this number and out put result on screen for example if he enter 10 the result should be 55 (1+2+3+4+5+6+7+8+9+10)
can some one please check what wrong i did
thanks
It's bad practice to ask others to look at uncommented code. Granted this is a simple program that most people can figure out, but you are asking others to spend their time. Why not make it easier for them?

Further, if you organize your code and comment it carefully, it is much easier for you to find your own mistakes. Learning the art of debugging takes practice and you should do it yourself to get that valuable experience.
 

debjit625

Joined Apr 17, 2010
790
Why you are posting the code in image format,we have code tags neat time please post your code in text format using code tags.
There are many errors but the one which makes this code not to work is the conditional loop
Rich (BB code):
while(x>0);
If "x" is positive then "x" will always be greater then zero and the loop will never stop.

What is the reason to define another sum variable I have no idea,useless

Anyway,as per your logic its a way to do it
Rich (BB code):
#include<iostream>
usingnamespace std;

int main()
{
int x = 0;
int sum = 0;
do
{
cout<<"Enter a positive value\n";
cin>>x;
}
while(x<0);
for(int i=0;i<=x;i++)
{
sum+=i;
}
cout<<"The summation is : "<<sum<<endl;
system("pause");
return 0;
}
But remember this is not the way to calculate summation there are better ways.
There are many ,just google about it...

Good Luck
 
Top