I'm pretty new to writting c++ and i made this file to create a random x and y coordinate and place a bitmap image upon it, i made the program save the coordinates to .dat files *or txt i dont really care at this point* now i want a way of reading these coordinates one line at a time (or any way to write a valid x == x_taken function) and applying them to a string called x/y_taken so i can make sure i dont place a bitmap in the same spot twice, heres my code (keep in mind its a sloppy rough draft, and any help would be greatly appreciated):

#include <allegro>
#include <fstream>
#include <iostream>

using namespce std;
//--------------------------------------variables-------------------------------------------
BITMAP *human_upSprite;
BITMAP *human_downSprite; // human bitmaps.
BITMAP *human_leftSprite;
BITMAP *human_rightSprite;	

BITMAP *zombie_downSprite;
BITMAP *zombie_upSprite;  // zombie bitmaps.
BITMAP *zombie_rightSprite;
BITMAP *zombie_leftSprite;

BITMAP *hero_downSprite;
BITMAP *hero_upSprite;  //hero bitmaps.
BITMAP *hero_leftSprite;
BITMAP *hero_rightSprite;
//-----------------------------random coor functions-----------------------------------------
int random_x_unit(x)
{
    rand() % 640 + 1 = x;

    ofstream a_file ("x_coor.dat");
    a_file( x, ios::app );
    a_file.close();
    
    return(x)'
};

int random_y_unit(y)
{
    rand() % 420 + 1 = y;
    
    ofstream a_file ("y_coor.dat");
    a_file( y, ios::app );
    a_file.close();
    
    return(y);
};
//------------------------------------------------------------------------------------
void spawn_human() //try to find a way to make a parameter that calls the function
{                  //x amount of times so you dont have to spam spawn_human().
     int x;
     int y;
     
     string x_taken;  // calls a string that will be used to apply the file to.
     string y_taken;  
     //------------------------------X------------------------------------------------
     random_x_unit(x);
     
     
     
     ifstream b_file("x_coor.dat");
     b_file>> x_taken;
     
     if(x_taken == x)
       spawn_human();
     else
       ofstream a_file("\n", ios::ate); //hopefully creats a newline and sets to end for         
                                                   //  reading next coor
       a_file.close();                  //without deleting anything.
     random_y_unit(y);
    //--------------------------------y--------------------------------------------------
     
     ifstream b_file("y_coor.dat");
     bfile>> y_taken;
     
     if(y_taken == y)
       spawn_human();
     else
       ofstream a_file("\n", ios::ate);
       a_file.close();
     //-----------------------------ENDING----------------------------------------------
     
     {
         humandSprite = load_bitmap( "human_downSprite.bmp", NULL );
         
         draw_sprite( buffer, humandSprite, x, y);
     };
};
Ancient Dragon commented: Thanks for taking the time to read the rules and use code tags correctly :) +23

I think that before you try doing screen graphics, you should go back to square one on writing C++ code. What you have stands no chance of compiling, much less doing what you think you want it to do.
Just looking at your very first function

int random_x_unit(x)
{
    rand() % 640 + 1 = x;

    ofstream a_file ("x_coor.dat");
    a_file( x, ios::app );
    a_file.close();
    
    return(x)'
};

Line 3 - remember that assignment is from the Right Hand Side to the Left Hand side, or LHS <- RHS
Line 6 - does nothing. If you meant to open the file x_coor.dat in append mode, the ios::app needs to be in line 5. To write the value of x to the file, try a_file << x; Line 7 - a correct statement!
Line 9 - end with a semicolon, not single-quote.
Line 10 - no semicolon needed to close function definition. It probably does no harm, as it's an empty statement.

In your spawn_human( ) function, you call this function above, passing it an unitialized argument, returning the random generated value. You don't store the returned value, but your program assumes that it's x now has some value. So, the number generated by this function is lost. You can transmit it to the caller by value or by reference. Pick one. Use it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.