Error window o _ o'

This is a discussion on Error window o _ o' within the C++ Programming forums, part of the General Programming Boards category; I have just started programming and i made a program that'll give me all the factors of the number i ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    2

    Error window o _ o'

    I have just started programming and i made a program that'll give me all the factors of the number i input

    the code is the following
    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
     int x,y,z;
         cout << "Enter a number";
         cin  >> x;
         cout << x << " is divisible by " << x << ",";
         z=x;
         
     for (z=z;z>0;z-1)
          {  
         z=z-1;
              if ((x%z)==0)
              {
              cout << z << ",";
              }
          }       
         return 0;
    }
    It compiled fine and it the factors showed up but after i run the program a window pops up saying
    (Factors.exe has encountered a problem and needs to close. We are sorry for the inconvenience.) (The WXP error window)

    what did i do wrong with my code?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You coded this:
    Code:
    for (z=z;z>0;z-1)
          {  
         z=z-1;
    and you should have coded this:
    Code:
    for (z=z;z>0;z-=1) 
          {
    Last edited by Dino; 09-24-2008 at 06:53 AM. Reason: typo
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Dino View Post
    ...you should have coded this:
    Code:
    for (z=z;z>0;z-=1) 
          {
    Or even:
    Code:
    for (z=x-1;z>0;z--) 
    ...
    .
    and remove the z=x in the line above (doing "z=z" is pointless, so could be omitted, but since you really want z=x-1, that's what you should have there!)

    The reason your application "stops" is that it tries to divide by zero, because the z=z-1 is done INSIDE the loop, rather than at the end of the loop.

    Also, the variable y is not being used.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    Your indentation is messed up.
    What IDE / editor do you use, if you don't mind me asking?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    2
    yeah, i just started so i dont know how to indent and such, i use Dev C++.
    thanks for the help

  6. #6
    Registered User Will Hemsworth's Avatar
    Join Date
    Sep 2008
    Location
    England - Lymm
    Posts
    6
    > i use Dev C++.
    When I used Dev C++, I noticed it had terrible indentation problems, thats when I changed to MSVS 2005, and its just takes away so many problems

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,658
    When I used Dev C++, I noticed it had terrible indentation problems
    By default. Once you turn out the smart tab thing, the indentation is fine, at least from what I remember.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User Will Hemsworth's Avatar
    Join Date
    Sep 2008
    Location
    England - Lymm
    Posts
    6
    By default. Once you turn out the smart tab thing, the indentation is fine, at least from what I remember.
    Perhaps I just wasn't doing it right , but I was playing with the settings for quite a long time, and could never quite get it how I wanted.
    Either way, Im glad I made the switch to MSVS

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    If you want a good free compiler (that doesn't have indenting issues) codeblocks is pretty good.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,658
    If you want a good free compiler (that doesn't have indenting issues) codeblocks is pretty good.
    Code Blocks is a pretty bad compiler, though it is fine as an IDE.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    Code Blocks is a pretty bad compiler, though it is fine as an IDE.
    What Laserlight is trying to say is that you are calling the IDE (which is a layer on top of/around the compiler) a compiler, which it isn't. It uses a very good compiler, gcc, but Code::Blocks is not a compiler in itself [as far as I know, not even in the slightest].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21