Thread: What's wrong with my code?

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    4

    What's wrong with my code?

    I'm just starting out with C++ (and programming in general), and my first program is giving me problems.

    Its purpose is to generate
    "-10-9-8-7-6-5-4-3-2-1 12345678910" as an output, but all I get is the euquivalent of "-9 \n"

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int x = -10;
        int tracker = 1;
        while (tracker != 2) {
               x = x + 1; cout << x;
               if (x = 0) { cout << "        ";}
               else if (x = 10) { cout << "\n"; tracker = tracker + 1; x = -10;}}
         cin.get(); 
    }
    EDIT - some of that code may seem redundant at the moment, but future versions will create a set number of lines of that output.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    if (x = 0)
    else if (x = 10)

    You are using the assignment operator and not the equals operator above.
    You're only born perfect.

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Secondly, it's also a good idea to set out your code properly.

    Like this:-

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int x = -11;
        int tracker = 1;
        while (tracker != 2) 
        {
               x = x + 1; 
               cout << x;
               if (x ==  0)  // This is what you need
               {
                    cout << "        ";
               }
                if (x ==  10) //This is what you need
               { 
                   cout << "\n"; 
                   tracker = tracker + 1;
                   //x = -10;
               }
        }
         cin.get(); 
    }
    It's more readable and will help you when you consider writing programs that are much longer than this one.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    4
    Doh! Thanks Treenef! And Elad, normally I do set out my code as you described, but I had to throw some stuff in the middle of it, and fixing the structure wouldn't have been worth the effort since it is a small program.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    x = x + 1;
    could be
    Code:
    x++;
    And the whole thing could be a for loop.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM