dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Program crashes!!!

  1. #1
    Join Date
    Dec 2006
    Posts
    2

    Program crashes!!!

    when i run my code, and i am inputting how many rows per cell i need it crashes. I think it has to do with my GetUserNumber fucntion. please take a look and let me know.

    #include <iostream>
    #include "conio.h"
    #include <fstream>
    using namespace std;

    #define FILE_ID "123456"
    #define SIZE_CELL_DATA 64
    typedef struct cell_s
    {
    //a character array holds string representation of number
    // only chData is stored in the file
    char chData[SIZE_CELL_DATA];

    long cell_number;
    long row_number;

    }cell_t;


    // struct data_base
    // The header of where it begins.
    //
    typedef struct data_base_s
    {
    long num_rows;
    long cells_per_row;

    cell_t *cells;

    }data_base_t;

    data_base_t * CreateDataBase(long num_rows,long cells_per_row);
    void SetData(data_base_t * db,long row_num, long cell_num, char * chData);
    cell_t *GetCell(data_base_t * db,long row_num, long cell_num);
    void FreeDataBase(data_base_t * db);
    void PrintDataToScreen(data_base_t * db);
    void PrintDataToFile(data_base_t * db, char *file_path);
    data_base_t * ReadDataFile(char * file_path);
    long GetUserNumber();
    data_base_t * PromtUserDataBaseInfo(char * file_path);
    void UserSaveData(data_base_t * db, char * file_path);
    void GoUserEdit(data_base_t *db);



    int main()
    {


    char file_path[160]; //user file path.
    data_base_t *myData = (data_base_t *)0; // the data base

    memset(file_path,0,160);

    //Get user info and alloc new data base or open existing.
    myData = PromtUserDataBaseInfo(file_path);
    if(!myData)
    { system("PAUSE");
    return 0; //error or user abort
    }

    GoUserEdit(myData);//Allow user cell edit.

    UserSaveData(myData,file_path);//Allow user save file

    FreeDataBase(myData);
    system("PAUSE");


    return EXIT_SUCCESS;
    }
    data_base_t * CreateDataBase(long num_rows,long cells_per_row)
    {
    data_base_t *db = (data_base_t *)0;
    long nRow,nCell;
    char buffer[SIZE_CELL_DATA];



    //allocate for header
    db = (data_base_t *)malloc(sizeof(data_base_t));
    memset(db,0,sizeof(data_base_t)); //set all bytes to zero.(habit)
    memset(db->cells,0,num_rows * cells_per_row * sizeof(cell_t));
    db->num_rows = num_rows;
    db->cells_per_row = cells_per_row;

    db->cells = (cell_t*)malloc(num_rows * cells_per_row * sizeof(cell_t));

    //For each cell, fill in some info.

    for(nRow = 0; nRow < num_rows; nRow++)
    {
    for(nCell = 0; nCell < cells_per_row; nCell++)
    {
    //example data...
    memset(buffer,0,SIZE_CELL_DATA);
    sprintf(buffer,"(%d,%d)",nRow,nCell);

    //set it assignes it.
    SetData(db,nRow,nCell,buffer);
    }//x
    }//y

    return db;
    }

    // Sets a string (chData) and info for a given cell.

    void SetData(data_base_t * db,long row_num, long cell_num, char * chData)
    {
    cell_t * cell;

    cell = GetCell(db,row_num,cell_num);

    cell->row_number = row_num;
    cell->cell_number = cell_num;

    memset(cell->chData,0,SIZE_CELL_DATA); //clear first.
    strcpy(cell->chData,chData);

    }

    // returns a pointer to the cell given a row and cell number

    cell_t *GetCell(data_base_t * db,long row_num, long cell_num)
    {
    return &db->cells[row_num * db->cells_per_row + cell_num];

    }

    void PrintDataToScreen(data_base_t * db)
    {
    long nRow,nCell;
    cell_t *cell = (cell_t*)0;

    for(nRow = 0; nRow < db->num_rows; nRow++)
    {
    for(nCell = 0; nCell < db->cells_per_row; nCell++)
    {
    cell = GetCell(db,nRow,nCell);
    cout << cell->chData << " ";
    }
    cout << endl;
    }


    }
    // PrintDataToFile
    // Prints a text file with the data.
    // This will overwrite file if already exists.
    void PrintDataToFile(data_base_t * db, char *file_path)
    {

    ofstream myfile (file_path); //open the file.
    char new_line = 10;
    char buffer[64];
    long max_cells;


    if (!myfile.is_open())
    {
    cout << "Error opening <" << file_path << ">" << endl;
    return;
    }

    //Get a string of our file ID
    //write it with new-line character at end.
    sprintf(buffer,"%s%c",FILE_ID,new_line);
    myfile.write(buffer,(long)strlen(buffer));


    sprintf(buffer,"%d%c",db->num_rows,new_line);
    myfile.write(buffer,(long)strlen(buffer));


    sprintf(buffer,"%d%c",db->cells_per_row,new_line);
    myfile.write(buffer,(long)strlen(buffer));

    //now write each cell.
    long nCell;
    cell_t *cell = (cell_t*)0;

    max_cells = db->num_rows * db->cells_per_row;

    for(nCell = 0; nCell < max_cells; nCell++)
    {
    //write the cell data.
    cell = &db->cells[nCell];
    myfile.write(cell->chData,(long)strlen(cell->chData));
    myfile.write(&new_line,1);

    }

    myfile.close();
    cout << "OK" << endl;

    }//
    //..........................
    //
    // ReadDataFile:
    // Allocates for and reads in a data base file.
    // The data is stored in the cell variable chData.
    // What you do with it from there is up to you.
    //
    data_base_t * ReadDataFile(char * file_path)
    {

    data_base_t *db = (data_base_t*)0;
    ifstream myfile (file_path); //open input stream for the file.
    string line;

    long num_rows, cells_per_row;

    //check file open state.
    if (!myfile.is_open())
    {
    cout << "Error opening <" << file_path << ">" << endl;
    return (data_base_t*)0;
    }


    getline(myfile,line);

    if(line != FILE_ID)
    {
    cout << "File <" << file_path << "> is not a valid data base. Exiting." << endl;
    myfile.close();
    return (data_base_t*)0;
    }

    getline(myfile,line);
    num_rows = atol(line.data());
    getline(myfile,line);
    cells_per_row = atol(line.data());


    cout << "Num rows = " << num_rows << ", cells_per_row = " << cells_per_row << endl;
    cout << "Press c to continue, else abort...";
    if(_getch() != 'c')
    {
    cout << "aborted." << endl;
    return (data_base_t*)0;
    }
    else
    {
    cout << "OK." << endl;
    }


    db = CreateDataBase(num_rows,cells_per_row);

    if(!db)
    { myfile.close();
    return db;
    }

    //Try and read rest of file, the data cells.
    long max_cells,nCell;

    max_cells = db->num_rows * db->cells_per_row;
    for(nCell = 0; nCell < max_cells; nCell++)
    {
    if(myfile.eof())
    { cout << "error. reached end of file before all cells read. aborting." << endl;
    FreeDataBase(db);
    return (data_base_t *)0;
    }
    getline(myfile,line); //line contains the chData.
    strcpy(db->cells[nCell].chData,line.data());//copy into data base cell.
    }

    myfile.close();
    cout << "File read OK." << endl;
    return db;
    }

    // FreeDataBase:
    // Frees allocated memory on cleanup.
    //
    void FreeDataBase(data_base_t * db)
    {
    free(db->cells);
    free(db);
    }
    // PromtUserDataBaseInfo:
    // This functions queries the user for information and returns a data base.
    // If user selects to open an existing file, that is returned.
    // If user opts to create new data base, one is allocated and returned.
    // A null pointer (0) is returned if any errors.
    // filepath recieves the filename user selects or names and must be
    // pre-allocated.
    //
    data_base_t * PromtUserDataBaseInfo(char * file_path)
    {
    char chUserKey;
    long num_rows,cells_per_row;
    data_base_t * db = (data_base_t *)0;


    cout << "Please choose one of the following:" << endl << endl;
    cout << "1...Open an existing file to edit. (Press 1)" << endl;
    cout << "2...Create a new data base. (Press 2)" << endl;
    cout << "Press any other key to cancel." << endl;
    chUserKey = _getch();


    if(chUserKey == '1') //open an existing file
    {
    cout << "Enter name of file to open..." << endl ;
    cin >> file_path;
    db = ReadDataFile(file_path);
    if(!db)
    {
    cout << "Could not read file <" << file_path << ">" << endl;
    return (data_base_t*)0;
    }
    }
    else if (chUserKey == '2')
    {
    cout << "Enter name of file to save as..." << endl ;
    cin >> file_path;


    cout << "How many rows will your data base need?...";
    num_rows = GetUserNumber();
    cout << "How many cells per row will your data base need?...";
    cells_per_row = GetUserNumber();
    db = CreateDataBase(num_rows,cells_per_row);

    if(!db)
    {
    cout << "Error creating data base <" << file_path << ">" << endl;
    return (data_base_t*)0;
    }
    cout << "Data base created." << db->num_rows << " rows,"
    << db->cells_per_row << " cells per row." << endl;
    }
    else
    { cout << endl << "User aborted.";
    return (data_base_t*)0;
    }

    return db;
    }

    // Gets a number from the user.

    long GetUserNumber()
    {
    char temp[80];
    long user_num;

    memset(temp,0,80);
    user_num = 0;
    while(!user_num )
    {
    cin >> temp;
    user_num = atol(temp);
    if(!user_num) cout << "invalid number,please try again..." ;
    }
    }
    // Let user edit cells
    // Assumes data base is valid.

    void GoUserEdit(data_base_t *db)
    {

    char chUserKey;
    long nRow,nCell;
    cell_t *cell = (cell_t*)0;
    char user_data[SIZE_CELL_DATA];


    cout << "Row range is 1 to " << db->num_rows << endl;
    cout << "Cells per row range is 1 to " << db->cells_per_row << endl;

    while(1)
    {
    cout << endl << "To edit more cells press 1, else proceed to save as." << endl;
    chUserKey = _getch();
    if(chUserKey != '1') return;


    cout << endl << "Which row do you want to edit? (1 is first)" << endl;

    while(1)
    {
    nRow = GetUserNumber();
    if((nRow >= 1) && (nRow <= db->num_rows)) break;
    cout << "Row value is out of range. Please try again. " << endl;

    }
    cout << "Which cell in that row do you want to edit?(1 is first)" << endl;
    while(1)
    {
    nCell = GetUserNumber();

    if((nCell >= 1) && (nCell <= db->cells_per_row)) break;
    cout << "Cell value is out of range. Please try again. " << endl;

    }


    nRow--;
    nCell--;

    cell = GetCell(db,nRow,nCell);
    cout << "The current value for this cell is " << cell->chData << endl;
    cout << "Change value? (1 = yes)...";

    chUserKey = _getch();
    if(chUserKey != '1') continue;

    cout << "Enter new value...";
    memset(user_data,0,SIZE_CELL_DATA);
    cin >> user_data;
    SetData(db,nRow,nCell,user_data);


    }
    }
    void UserSaveData(data_base_t * db, char * file_path)
    {
    char chUserKey;

    cout << endl << "Save file <" << file_path << "> ? (press 1)";
    chUserKey = _getch();

    if(chUserKey != '1')
    { cout << endl <<"File not saved." << endl;
    return;


    PrintDataToFile(db,file_path);

    }
    }

  2. #2
    Join Date
    May 2006
    Posts
    176
    Quote Originally Posted by aLittleLost
    when i run my code, and i am inputting how many rows per cell i need it crashes. I think it has to do with my GetUserNumber fucntion. please take a look and let me know. [...]
    I think you have to return a result from GetUserNumber function:
    Code:
    long GetUserNumber()
    {
        . . .
    
        return user_num;
    }
    I hope it helps.

Similar Threads

  1. soft drink Vending machine program
    By divagoddess in forum C++
    Replies: 5
    Last Post: 08-14-2009, 03:12 PM
  2. Help with Server program
    By sedricbenson@ho in forum C++
    Replies: 2
    Last Post: 11-07-2006, 09:18 AM
  3. Connecting to a Server Program HELP
    By sedricbenson@ho in forum C++
    Replies: 2
    Last Post: 11-07-2006, 08:58 AM
  4. Please help with running executable vb program.
    By Gordon Reichhardt in forum VB Classic
    Replies: 2
    Last Post: 01-08-2002, 11:06 AM
  5. How can I build copy protection into my program
    By W.Pierce in forum VB Classic
    Replies: 1
    Last Post: 12-11-2001, 09:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center