Thread: what's wrong while statement

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    56

    what's wrong while statement

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
          //basic devines formila int;s
          int x = 1;
          int y = 1;
          int answer = 1;
          int tt = 1;
          //end
          //work process
          while (x < 525){   //wait till x = 525
                int answer = x * y + answer + 3;
                int x = x + tt ;   //x is x +1 (tt)
                int y = y + tt ;  //y is y +1 (tt)
                }
          cout <<"answer is = "<<answer<< "got it";     //print the answer
          system("PAUSE");
          return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    6
    you are declaring a local 'x' within that while loop.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    6
    you are also declaring a local "answer" so your output will always be 1 (the value of answer local to main). remove the "int"s within that while loop and you should be fine.

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    42
    I am not too sure what you want but probably u can replace the part of your code with the loop with this

    Code:
    while (x < 525) {
      answer += (x++ * y++) + 3;
    }
    x++ & y++ represents your increment after usage. Your loop will execute 524 times only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong with my if statement
    By joker_tony in forum C Programming
    Replies: 6
    Last Post: 05-10-2008, 02:11 AM
  2. what is wrong with my if statement
    By joker_tony in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 12:26 AM
  3. switch case statement
    By stanlvw in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 05:06 AM
  4. What's wrong with my success statement??
    By cool_dude07 in forum C Programming
    Replies: 7
    Last Post: 07-21-2007, 11:22 PM
  5. Something wrong with this if statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-30-2002, 05:19 PM