The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

[SOLVED] C++ help - compiler ignoring FOR loop?

ArcibiArcibi Registered User regular
edited April 2008 in Help / Advice Forum
So I'm writing this console application in Dev-C++ to script a POV-Ray file that generates a bunch of random spheres, right

This is the source code (spoilered for long maybe):
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <time.h>

using namespace std;

int main(int argc, char *argv[])
{
     fstream file_op("random.pov",ios::out);
     int x;
     int y;
     int z;
     int i = 0;
     int rad = 10;
     float r;
     float g;
     float b;
     srand(time(NULL));
     
     file_op << "camera{location <0,50,-100> look_at <0,0,0> }" << endl;
     file_op << "background{ color blue 1 green .5 red .5 }" << endl;
     file_op << "light_source{<30,50,-30> color red 1 green 1 blue 1}" << endl;
     file_op << "light_source{<-30,50,30> color red 1 green 1 blue 1}" << endl;
     
     cout << "camera{location <0,50,-100> look_at <0,0,0> }" << endl;
     cout << "background{ color blue 1 green .5 red .5 }" << endl;
     cout << "light_source{<30,50,-30> color red 1 green 1 blue 1}" << endl;
     cout << "light_source{<-30,50,30> color red 1 green 1 blue 1}" << endl;
     
     for(i = 0; i > 100; i++)
     {
         x = (rand() & 100 - 1) - 50;
         y = (rand() & 100 - 1) - 50;
         z = (rand() & 100 - 1) - 50;
         r = rand()/(float(RAND_MAX)+1);
         g = rand()/(float(RAND_MAX)+1);
         b = rand()/(float(RAND_MAX)+1);
         
         file_op << "sphere { <" << x << ", " << y << ", " << z << " >, "
                 << rad << " texture{ pigment {color rgbf<"
                 << r << ", " << g << ", " << b << ", 0.6>}"
                 << "finish {phong 1}}}" << endl;
         
         cout << "sphere { <" << x << ", " << y << ", " << z << " >, "
              << rad << " texture{ pigment {color rgbf<"
              << r << ", " << g << ", " << b << ", 0.6>}"
              << "finish {phong 1}}}" << endl;
     }
     
    file_op.close();
    system("PAUSE");
    return EXIT_SUCCESS;
}

cout is basically there just to put visual confirmation of what I'm writing to the file, on the screen

Problem is, it's ignoring the FOR loop entirely. Nothing being written to the screen, and nothing being written to the file after the initial camera, background, and lightsource commands

Any ideas why the loop seems to be totally ignored?

GameTrailers | Goozex | Check out: Arcibi's Dev Blog and Robot House Games
tmntsigshrunkre4.jpg
Wii: 5024 6786 2934 2806 | Steam/XBL: Arcibi | FFXI: Arcibi / Bahamut
Arcibi on

Posts

  • His CorkinessHis Corkiness Registered User regular
    edited April 2008
    The condition "i > 100" is never true. The loop will only start, and continue, if that condition is true. Most likely you want "i < 100".

    Not quite related, but Dev-C++ is outdated. A newer IDE is Code::Blocks, or alternatively you can use Visual Studio Express Edition.

    His Corkiness on
  • DrFrylockDrFrylock Registered User regular
    edited April 2008
    for(i = 0; i > 100; i++)

    Switch the greater-than sign to a less-than sign. The middle component of a for loop is the condition that must be true for the loop to iterate. Since you set i = 0 to begin with, and then see if i > 100, the loop will never iterate since i is never greater than 100.

    E:F,B

    DrFrylock on
  • ArcibiArcibi Registered User regular
    edited April 2008
    Well, I feel like an idiot

    Thanks for the help, the program works as intended now

    Arcibi on
    GameTrailers | Goozex | Check out: Arcibi's Dev Blog and Robot House Games
    tmntsigshrunkre4.jpg
    Wii: 5024 6786 2934 2806 | Steam/XBL: Arcibi | FFXI: Arcibi / Bahamut
This discussion has been closed.