c++ (wrong code)

Discussion in 'Programmer's Corner' started by TAKYMOUNIR, Oct 22, 2011.

  1. TAKYMOUNIR

    Thread Starter Active Member

    Jun 23, 2008
    351
    1
    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
     
    • c++.PNG
      c++.PNG
      File size:
      13.8 KB
      Views:
      24
    Last edited: Oct 22, 2011
  2. TAKYMOUNIR

    Thread Starter Active Member

    Jun 23, 2008
    351
    1
  3. steveb

    Senior Member

    Jul 3, 2008
    2,433
    469
    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.
     
  4. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    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
    Code ( (Unknown Language)):
    1.  
    2. while(x>0);
    3.  
    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
    Code ( (Unknown Language)):
    1.  
    2. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<iostream>[/COLOR][/SIZE]
    3. [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] std;[/SIZE]
    4. [SIZE=2][COLOR=#0000ff]
    5. [SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] main()[/SIZE]
    6. [SIZE=2]{[/SIZE]
    7. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] x = 0;[/SIZE]
    8. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sum = 0;[/SIZE]
    9. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]do[/COLOR][/SIZE]
    10. [/COLOR][/SIZE][SIZE=2]{[/SIZE]
    11. [SIZE=2]cout<<[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Enter a positive value\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    12. [SIZE=2]cin>>x;[/SIZE]
    13. [SIZE=2]}[/SIZE]
    14. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]while[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](x<0);[/SIZE]
    15. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i=0;i<=x;i++)[/SIZE]
    16. [SIZE=2]{[/SIZE]
    17. [SIZE=2]sum+=i;[/SIZE]
    18. [SIZE=2]}[/SIZE]
    19. [SIZE=2]cout<<[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"The summation is : "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]<<sum<<endl;[/SIZE]
    20. [SIZE=2]system([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"pause"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    21. [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] 0;[/SIZE]
    22. [SIZE=2]}[/SIZE]
    23.  
    But remember this is not the way to calculate summation there are better ways.
    There are many ,just google about it...

    Good Luck
     
Loading...