can you help me please?

This is a discussion on can you help me please? within the C++ Programming forums, part of the General Programming Boards category; I know i should not but i'm gonna add the source code any ways. #include <iostream.h> #include <fstream.h> #include <stdlib.h> ...

  1. #1
    Unregistered
    Guest

    Question can you help me please?

    I know i should not but i'm gonna add the source code any ways.

    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <time.h>

    int main()
    {
    int Random0to16 = 0;
    int Check = 0;
    int counter = 1;
    int temp;
    char file[9];

    file[0] = 'q';

    while(1)
    {
    counter+=1;

    if(counter > 0 && counter >= 10)
    file[1] = 't';
    else if(counter > 10 && counter >= 20)
    file[1] = 'm';
    else if(counter > 20 && counter >= 30)
    file[1] = 'w';
    else if(counter > 30)
    {
    cout << "breaking";
    break;
    }
    srand(time(NULL));

    Random0to16 = rand()%16;

    Check = Random0to16/10;

    if(Check == 0)
    {
    file[2] = '1';
    file[3] = Random0to16 + 48;
    }
    else if(Check == 1)
    {
    file[2] = '2';
    file[3] = Random0to16-10 + 48;
    }

    temp = rand()%6 + 1 + 48;
    file[4] = temp;

    file[5] = '.';
    file[6] = 't';
    file[7] = 'x';
    file[8] = 't';

    cout << file << endl;
    }

    return 0;
    }


    The problem with the source code is that it a) does not break when counter reaches 30 and b) when file is outputted it adds characters to it's ending.

    I would like to thank you in advance for even reading this and hopefully helping me.

    Dirk van hensbergen

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    This is why your program doesn't stop.

    Code:
            if (counter > 0 && counter >= 10) 
                file[1] = 't'; 
            else if(counter > 10 && counter >= 20) 
                file[1] = 'm'; 
            else if(counter > 20 && counter >= 30) 
                file[1] = 'w'; 
            else if(counter > 30) 
            { 
                cout << "breaking"; 
                break; 
            }
    Take for example the first line. If counter > 0 and counter >= 10. If counter >= 10, then automatically counter > 0. Isn't it? So it would suffice if counter > 0..... but assume counter == 31. Then counter > 0!! So when counter > 0, then your program will ALWAYS go in the line file[1]='t'.

    By the way, don't use that while (1). I, personally, would never use while (1) in a program like this.

    You know that the program should break the loop when counter > 30. Knowing this condition it would be better to it this way:

    Code:
    while (counter++ <= 30)
    The last thing you need to change is the range checking. I assume you want to do this:

    Code:
            if (counter > 0 && counter <= 10) 
                file[1] = 't'; 
            else if(counter > 10 && counter <= 20) 
                file[1] = 'm'; 
            else if(counter > 20 && counter <= 30) 
                file[1] = 'w'; 
            else if(counter > 30) 
            { 
                cout << "breaking"; 
                break; 
            }
    Last edited by Shiro; 01-12-2002 at 08:25 AM.

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