Hi there!
I am working on a simple text based console application for a game of battleship. I am not really looking for a raw code just ideas on where to go next with my code. Some things i would like to put in it that I am flat out confused about: the computer should narrow down its target area when it scores a hit, the player can either randomly or manually set their ships (I started these functions but am a little lost), and how the main function driver should run because mine is almost blank at the moment.

Here are the functions I have written so far:

int welcome_screen(void)
{
    int choice = 0;
    
    /* prints out the menu */
    printf("*****************************************");
    printf("***************Battleship****************");
    printf("*****************************************");
    printf("1. To start playing please press one (1).\n2. Or to quit please press two (2).\n");

    /* sets the value of choice to the user's choice. */
    scanf("%d", &choice);
    
    /* returns choice */
    return choice;
}

void initialize_game_board (char game_board[10][10], int num_rows, int num_cols)
{
	int row_index = 0, col_index = 0;

	for (row_index = 0; row_index < num_rows; row_index++)
	{
		for (col_index = 0; col_index < num_cols  ; col_index++)
		{
			game_board[row_index][col_index] = '~';
		}
	}
}

void print_game_board (char game_board[10][10], int num_rows, int num_cols)
{
	int row_index = 0, col_index = 0;

    printf(" ");
    for (col_index = 0; col_index < num_cols; col_index++)
    {
        printf(" %d", col_index);
    }
    putchar ('\n');

	for (row_index = 0; row_index < num_rows; row_index++)
	{
		printf("%d ", row_index);
        for (col_index = 0; col_index < num_cols  ; col_index++)
		{
			printf("%c ", game_board[row_index][col_index]);
		}
		putchar ('\n');
	}
}

void select_who_starts_first(int *first_turn)
{
    int random = 1;
    
    random = rand() % 1 + 1;

    switch (random)
    {
    case 1: 
        *first_turn = 1;
        break;
    case 2:
        *first_turn = 2;
        break;
    default:
        printf("Error: first turn player could not be randomly selected.");
        break;
    }
}

int check_shot(char target)
{
    if (target == '~')
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int is_winner(int number_of_ships)
{
    if (number_of_ships == 0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

void update_board(int hit_or_miss, char *target)
{
    if (hit_or_miss == 1)
    {
        *target = '*';
    }
    else
    {
        *target = 'm';
    }
}

int print_menu (void)
{
    int choice = 0;

    /* prints out the menu */
    printf("*************************\n");
    printf("********* Menu **********\n");
    printf("*************************\n");
    printf("1. To manually place your ships press one (1).\n2. Or have them randomly placed press two (2).\n");
    
    /* sets the value of choice to the user's choice. */
    scanf("%d", &choice);
    
    /* returns choice */
    return choice;
}

void manually_place_ships_on_board(char *game_board[10][10])
{
    int row_index1 = 0, row_index2 = 0, row_index3 = 0, row_index4 = 0, row_index5 = 0;
    int col_index1 = 0, col_index2 = 0, col_index3 = 0, col_index4 = 0, col_index5 = 0;
    /*
    int row_index[5] = {0, 0, 0, 0, 0};
    int col_index[5] = {0, 0, 0, 0, 0};
    int count = 1;*/
    
    printf("Please enter the five cells to place the Carrier across: ");
    scanf("%d %d %d %d %d %d %d %d %d %d", &row_index1, &col_index1, &row_index2, &col_index2, &row_index3, &col_index3, &row_index4, &col_index4, &row_index5, &col_index5);
    /*
    for (count = 1; count <= 5; count++)
    {
        if (game_board[row_index[count]][col_index[count]] == '~')
        {
            *game_board[row_index[count]][col_index[count]] = 'c';
        }
    */
    game_board[row_index1][col_index1] = 'c';
    game_board[row_index2][col_index2] = 'c';
    game_board[row_index3][col_index3] = 'c';
    game_board[row_index4][col_index4] = 'c';
    game_board[row_index5][col_index5] = 'c';
    
   
}

void generate_direction (int *dir)
{
    *dir = rand () % 2;
}

void generate_start_pt (int len_ship, int *row_coord, int *col_coord)
{
    int dir = 0;
    
    *row_coord = rand () % 10;
    *col_coord = rand () % 10;

    generate_direction(&dir);

    switch (dir)
    {
    case 0:
         *row_coord = rand () % 10;
         *col_coord = rand () % (10 - len_ship);

Like I said most of it is still half written and Im still fidgitting with it. And any help would be appreciated, but I am still starting in C and would prefer to keep it fairly simple so I can actually learn from any feedback I get. So if any replies could be kept to simple constructs up until about arrays, pointers, and if, for, while statements it would be very helpful. But all suggestions are appreciated! Thank you in advance for any feedback.

Your first problem is that

Like I said most of it is still half written and Im still fidgitting with it.

Stop fidgitting with it. Take one task, write it. Test it. Perfect it. Then move to the next task.

I'd recommend starting with the display function. Write it completely first -- passing parameters so you can display either player's board.

Then write the ship placement function. Takes board, ship, starting position, and either the direction (up, left,...) or ending position.

Now the user input function, which calls the perfected ship placement function. Then modify the user input function to create the computer 'input' function.

Etc....

Thank you for the advice. I have ironed out most of the problems that i was stuck on before. But I have never really used array searching so I am fairly green in the area. My question is do you think that this would be the best way to determine if a ship has been sunk and if there are still ships on the board? And if so is sequential search the best way to go about it, its one of the few I am even familiar with?

Any insights would be appreciated and an example of the search algorithm or code would be appreciated.

You could do a search that would be faster, but there is absolutely no need for that, in this game. A sequential search is fine!

Some idea code - untested!

if(gameOn) 
  loop for another player's turn
else
  give congrats and goodbye message and exit this game
  in that function ask them if they want to play again

int gameOn( game array, int numRows, int numCols) {

for(r = 0, gameon = 0; r < numRows; r++) {
  for(c = 0; c < numCols; c++) {
    if(game[r][c] == value of opponents ship) {
       gameon = 1;  //gameon is just an int variable you will declare
       break;  //enemy still has an unsunk ship, so game on!
    }
  }
}

return gameon;

//your calling function should handle gameon == 1, or  gameon == 0

Ship input for placement:
Enter each cell for the carrier? Sure, I want 1,2; 4,4; 2,3; 6,7 and 8,4
How about:
X,Y,Direction (1=up,2=down...) Then just place the ship specified in the locations. It's easier to test if the ship won't fit:

if (X - shipSize < 0) bad
if (X + shipSice > 10) bad

something like that.
And use the ship number, not a character. That way you know immediately which ship has been hit.

Thank you again for the help with the search snippet. I utilized a version of it. However I am getting my program to build but the random ship placement seems to only work about half of the time and it is just flat out confusing me. Whenever I try to use my debugger it gets stuck at a different spot, whether it is placing user ships or computer ships. I am not sure if I seeded the value wrong, which is very possible.

I am just going to post the entire source code. I apologize for the length and simplicity of the code.

Any suggestions or ideas would be extremely appreciated at this point. Thanks!

#include <stdio.h>
#include <stdlib.h>

int welcome_screen(void);
void initialize_game_board (char game_board[10][10], int num_rows, int num_cols);
void print_game_board (char game_board[10][10], int num_rows, int num_cols);
void select_who_starts_first(int *first_turn);
int check_shot(char target);
int is_winner(char game_board[10][10]);
void manually_place_ships_on_board(char *game_board[10][10], char ship);
void randomly_place_ships_on_board(char *game_board[10][10], char ship, int dir);
void generate_start_pt (int len_ship, int *row_coord, int *col_coord, int dir);
void generate_direction (int *dir);
int print_menu (void);
void get_target (int *target_row, int *target_col);
void rand_target (int *target_row, int *target_col);
void update_board(int hit_or_miss, char *target);
int search_ship(char game_board[10][10], int num_rows, int num_cols, char ship);

int main (void)
{
    char game_board_1[10][10] = {{'~', '~'}, {'~'}};
    char game_board_2[10][10] = {{'~', '~'}, {'~'}};
    char game_board_target[10][10] = {{'~', '~'}, {'~'}};
	int direction = 0, row_coord = 0, col_coord = 0, choice = 0, turn = 0, hit_or_miss = 0, target_row = 0, target_col = 0;
    int number_of_ships_1 = 0, number_of_ships_2 = 0;

	srand ((unsigned int)time(NULL));

    choice = welcome_screen();

    if (choice == 1)
    {

    
        initialize_game_board(game_board_1, 10, 10);
        initialize_game_board(game_board_2, 10, 10);
        initialize_game_board(game_board_target, 10, 10);

        select_who_starts_first(&turn);
        
        choice = print_menu();

        if (choice == 1)
        {
            print_game_board(game_board_1, 10, 10);

            system("pause");
            system("cls");
            manually_place_ships_on_board(game_board_1, 'c');
            print_game_board(game_board_1, 10, 10);
            
            system("pause");
            system("cls");
            
            manually_place_ships_on_board(game_board_1, 'b');
            print_game_board(game_board_1, 10, 10);
           
            system("pause");
            system("cls");

            manually_place_ships_on_board(game_board_1, 'r');
            print_game_board(game_board_1, 10, 10);

            system("pause");
            system("cls");
            
            manually_place_ships_on_board(game_board_1, 's');
            print_game_board(game_board_1, 10, 10);
            
            system("pause");
            system("cls");
            
            manually_place_ships_on_board(game_board_1, 'd');
        }
        else if (choice == 2)
        {
            generate_direction(&direction);
            randomly_place_ships_on_board(game_board_1, 'c', direction);

            generate_direction(&direction);
            randomly_place_ships_on_board(game_board_1, 'b', direction);

            generate_direction(&direction);
            randomly_place_ships_on_board(game_board_1, 'r', direction);

            generate_direction(&direction);
            randomly_place_ships_on_board(game_board_1, 's', direction);

            generate_direction(&direction);
            randomly_place_ships_on_board(game_board_1, 'd', direction);
        }
        

        generate_direction(&direction);
        randomly_place_ships_on_board(game_board_2, 'c', direction);

        generate_direction(&direction);
        randomly_place_ships_on_board(game_board_2, 'b', direction);

        generate_direction(&direction);
        randomly_place_ships_on_board(game_board_2, 'r', direction);

        generate_direction(&direction);
        randomly_place_ships_on_board(game_board_2, 's', direction);

        generate_direction(&direction);
        randomly_place_ships_on_board(game_board_2, 'd', direction);
        
        print_game_board(game_board_1, 10, 10);
        printf("Target Board\n");
        print_game_board(game_board_target, 10, 10);
        
        number_of_ships_1 = 5;
        number_of_ships_2 = 5;

        if (turn == 1)
        {
            get_target(&target_row, &target_col);
            hit_or_miss = check_shot(game_board_2, target_row, target_col);
            update_board(hit_or_miss, game_board_2[target_row][target_col]);
            update_board(hit_or_miss, game_board_target[target_row][target_col]);
            printf("Target Board\n");
            print_game_board(game_board_target, 10, 10);
            system("pause");
            system("cls");
        }
        else
        {
            rand_target(&target_row, &target_col);
            hit_or_miss = check_shot(game_board_1, target_row, target_col);
            update_board(hit_or_miss, game_board_1[target_row][target_col]);
            printf("Player 1 Board\n");
            print_game_board(game_board_1, 10, 10);
        }
        
        turn = (turn + 1) % 2 + 1;
        
        while (number_of_ships_2 != 0 || number_of_ships_2 != 0)
        {
            if (turn == 1)
            {
               get_target(&target_row, &target_col);
               hit_or_miss = check_shot(game_board_2, target_row, target_col);
               update_board(hit_or_miss, game_board_2[target_row][target_col]);
               number_of_ships_2 = is_winner(game_board_2);
               printf("Target Board\n");
               print_game_board(game_board_target, 10, 10);
               system("pause");
               system("cls");
             }
            else
            {
                rand_target(&target_row, &target_col);
                hit_or_miss = check_shot(game_board_1, target_row, target_col);
                update_board(hit_or_miss, game_board_1[target_row][target_col]);
                number_of_ships_1 = is_winner(game_board_1);
                printf("Player 1 Board\n");
                print_game_board(game_board_1, 10, 10);
            }
        }

        if (number_of_ships_1 == 0)
        {
            printf("Sorry you lost. Better luck next time.\n");
        }
        else if (number_of_ships_2 == 0)
        {
            printf("Congratulations! You won!\n");
        }

    }

    return 0;


}

int welcome_screen(void)
{
    int choice = 0;
    
    /* prints out the menu */
    printf("*****************************************\n");
    printf("***************Battleship****************\n");
    printf("*****************************************\n");
    printf("1. To start playing please press one (1).\n2. Or to quit please press two (2).\n");

    /* sets the value of choice to the user's choice. */
    scanf("%d", &choice);
    
    /* returns choice */
    return choice;
}

void initialize_game_board (char game_board[10][10], int num_rows, int num_cols)
{
	int row_index = 0, col_index = 0;

	for (row_index = 0; row_index < num_rows; row_index++)
	{
		for (col_index = 0; col_index < num_cols  ; col_index++)
		{
			game_board[row_index][col_index] = '~';
		}
	}
}

void print_game_board (char game_board[10][10], int num_rows, int num_cols)
{
	int row_index = 0, col_index = 0;

    printf(" ");
    for (col_index = 0; col_index < num_cols; col_index++)
    {
        printf(" %d", col_index);
    }
    putchar ('\n');

	for (row_index = 0; row_index < num_rows; row_index++)
	{
		printf("%d ", row_index);
        for (col_index = 0; col_index < num_cols  ; col_index++)
		{
			printf("%c ", game_board[row_index][col_index]);
		}
		putchar ('\n');
	}
}

void select_who_starts_first(int *first_turn)
{
    int random = 1;
    
    random = rand() % 1 + 1;

    switch (random)
    {
    case 1: 
        *first_turn = 1;
        break;
    case 2:
        *first_turn = 2;
        break;
    default:
        printf("Error: first turn player could not be randomly selected.");
        break;
    }
}

int check_shot(char game_board[10][10], int target_row, int target_col)
{
    if (game_board[target_row][target_col] == '~')
    {
        printf("Miss...");
        return 0;
    }
    else
    {
        printf("Hit!");
        return 1;
    }
}

int search_ship(char game_board[10][10], int num_rows, int num_cols, char ship)
{
    int row = 0, col = 0, status = 0;

    for (row = 0; row < num_rows; row++)
    {
        for (col = 0; col < num_cols; col++)
        {
            if (game_board[row][col] == ship)
                status = 1;
            break;
        }
    }
    return status;
}

void update_board(int hit_or_miss, char *target)
{
    if (hit_or_miss == 1)
    {
        *target = '*';
    }
    else
    {
        *target = 'm';
    }
}

int print_menu (void)
{
    int choice = 0;

    /* prints out the menu */
    printf("*************************\n");
    printf("********* Menu **********\n");
    printf("*************************\n");
    printf("1. To manually place your ships press one (1).\n2. Or have them randomly placed press two (2).\n");
    
    /* sets the value of choice to the user's choice. */
    scanf("%d", &choice);
    
    /* returns choice */
    return choice;
}

void manually_place_ships_on_board(char game_board[10][10],char ship) /*int row_index1, int col_index1, int row_index2, int col_index2, int row_index3, int col_index3, int row_index4, int col_index4, int row_index5, int col_index5)*/
{
     int size = 0, row_index = 0, col_index = 0, count = 0;
     

     if (ship == 'c')
     {
         size = 5;
         printf("Please select where to place the Cruiser by entering five sets of coordinates.\n");
     }
     else if (ship == 'b')
     {
         size = 4;
         printf("Please select where to place the Battleship by entering four sets of coordinates.\n");
     }
     else if (ship == 'r' || ship == 's')
     {
         size = 3;
         if (ship == 'r')
         {
            printf("Please select where to place the Cruiser by entering three sets of coordinates.\n");
         }
         else 
         {
             printf("Please select where to place the Submarine by entering three sets of coordinates.\n");
         }
     }
     else
     {
         size = 2;
         printf("Please select where to place destroyer by entering two sets of coordinates.\n");
     }

     while (count < size)
     {
         printf("Enter a set of coordinates: \n");
         printf("Row: ");
         scanf("%d", &row_index);
         printf("Column: ");
         scanf("%d", &col_index);
         
         if (game_board[row_index][col_index] == '~')
         {
            game_board[row_index][col_index] = ship;
            count++;
         }
         else
         {
             printf("Error illegal ship placement. Please enter another set of coordinates.");
         }
     }
}

void generate_direction (int *dir)
{
    *dir = rand () % 2;
}

void generate_start_pt (int len_ship, int *row_coord, int *col_coord, int dir)
{
    
	switch (dir)
	{
		case 0:
			*row_coord = rand () % 10;
			*col_coord = rand () % (10 - len_ship + 1);
			break;
		case 1:
			*row_coord = rand () % (10 - len_ship + 1);
			*col_coord = rand () % 10;
			break;
	}
}

void randomly_place_ships_on_board(char game_board[10][10], char ship, int dir)
{
     int row_coord = 0, col_coord = 0, size = 0, count = 0;
  

     if (ship == 'c')
     {
         size = 5;
     }
     else if (ship == 'b')
     {
         size = 4;
     }
     else if (ship == 'r' || ship == 's')
     {
         size = 3;
     }
     else
     {
         size = 2;
     }

     generate_start_pt(size, &row_coord, &col_coord, dir);

     while (count < size)
     {
         
         if (game_board[row_coord][col_coord] == '~')
         {
            game_board[row_coord][col_coord] = ship;
            if (dir == 0)
            {
                col_coord++;
            }
            else
            {
                row_coord++;
            }
            count++;
         }
     }

}

void get_target (int *target_row, int *target_col)
{
    printf("Please enter a target coordinate: \n");
    printf("Row: ");
    scanf("%d", target_row);
    printf("Column: ");
    scanf("%d", target_col);
}

void rand_target (int *target_row, int *target_col)
{
    *target_row = rand() % 9 + 1;
    *target_col = rand() % 9 + 1;
}

int is_winner (char game_board[10][10])
{
    int number_of_ships = 0;

    number_of_ships += search_ship(game_board, 10, 10, 'c');
    number_of_ships += search_ship(game_board, 10, 10, 'b');
    number_of_ships += search_ship(game_board, 10, 10, 'r');
    number_of_ships += search_ship(game_board, 10, 10, 's');
    number_of_ships += search_ship(game_board, 10, 10, 'd');

    return number_of_ships;
}

Again sorry for the length. Any suggestions are welcome.

I suggest, since you asked, you start actually explaining what is going wrong. And pointing to the section of code rather than making us search through 454 lines that we didn't write. Your description

but the random ship placement seems to only work about half of the time and it is just flat out confusing me. Whenever I try to use my debugger it gets stuck at a different spot, whether it is placing user ships or computer ships.

tells us nothing useful. That's like telling the doctor "Doc, I have a pain, it's somewhere" and expecting him to fix it. Think of us a your doctor. If you want to spend 3-10 hours (or more) waiting for an answer because we have to ask more questions, OK. But aren't you wasting a lot of time doing nothing this way? Details.... Give is details.

One problem I see is that you're using the word "random" as a variable. Change that name, because "random" is a reserved word in C.

If that doesn't fix it, post back and I'll run your program and see what else looks troublesome.

One problem I see is that you're using the word "random" as a variable. Change that name, because "random" is a reserved word in C.

It is? What's it used for? Please verify before replying.

Sorry in hindsight that probably would have been much more efficient to specify my problem. I haven't commented my code out yet so its pretty confusing. The function that it has been stopping at in the debugger is

void randomly_place_ships_on_board(char game_board[10][10], char ship, int dir)
{
     int row_coord = 0, col_coord = 0, size = 0, count = 0;
  

     if (ship == 'c')
     {
         size = 5;
     }
     else if (ship == 'b')
     {
         size = 4;
     }
     else if (ship == 'r' || ship == 's')
     {
         size = 3;
     }
     else
     {
         size = 2;
     }

     generate_start_pt(size, &row_coord, &col_coord, dir);

     while (count < size)
     {
         
         if (game_board[row_coord][col_coord] == '~')
         {
            game_board[row_coord][col_coord] = ship;
            if (dir == 0)
            {
                col_coord++;
            }
            else
            {
                row_coord++;
            }
            count++;
         }
     }

}

And it utilizes:

void generate_start_pt (int len_ship, int *row_coord, int *col_coord, int dir)
{
    
	switch (dir)
	{
		case 0:
			*row_coord = rand () % 10;
			*col_coord = rand () % (10 - len_ship + 1);
			break;
		case 1:
			*row_coord = rand () % (10 - len_ship + 1);
			*col_coord = rand () % 10;
			break;
	}
}

And this is how I used it in my main driver. I call it either 10 or 5 times depending on if the player chooses to randomly place his ships along with the computer's ships.:

randomly_place_ships_on_board(game_board_1, 'c', direction);

And this is the statement I used to seed the value in the main driver:

srand ((unsigned int)time(NULL));

The program builds just fine and then when I run it it either stops after making my second menu choice or after my first shot. It does not crash or give me a run time error. Just after hitting enter the cursor moves on the next line and just stays there accepting no input and seemingly doing nothing. Both of these statements utilize scanf, but I'm not sure how that would cause an error.

As far as the random being a reserved word in C I also was not aware of that. I did change it though and still ran into the same error with the cursor moving to the next line and stopping.

I hope that this narrows down my problem to something more readable. I apologize for the lengthy and somewhat ridiculous post. I was simply pretty lost on where i had gone wrong. As always any input is welcome and thank you for posting.

if (game_board[row_coord][col_coord] == '~')

If this is true, count is never incremented.

It is? What's it used for? Please verify before replying.

[B]random[/B]   Macro that returns an integer.
 Syntax:
   random(num);
   int random(int num);

Defined in  stdlib.h

 Remarks:
random returns a random number between 0 and (num-1).

random(num) is a macro defined in stdlib.h. Both num and the random number
returned are integers.

 Return Value:
Returns an integer between 0 and (num-1).

 Portability:
A corresponding function exists in Turbo Pascal.

 See Also:
  rand    randomize    srand

 Example:
 #include <stdlib.h>
 #include <stdio.h>
 #include <time.h>

 /* prints a random number in the range 0 to 99 */
 int main(void)
 {
    randomize();
    printf("Random number in the 0-99 range: %d\n", random (100));
    return 0;
 }

Not every compiler may have this, but some do.

[B]random[/B]   Macro that returns an integer.
 Syntax:
   random(num);
   int random(int num);

Not every compiler may have this, but some do.

Sorry, that doesn't cut it. If not all compilers (only TurboC as far as I can tell) have it, it is not a reserved word. And any use as a variable will override the macro definition.

Or are you also implying that when using DevC I can't create a variable named clrscr or gotoxy because it's defined in TurboC3?

Ok well I looked into the count not being incremented if that statement was true. Then I rethought my logic and made the function use 2 loops. The first one should check if the start point and direction input will allow the ship to fit into the game board without overlapping another ship. The second loop is to actually place the ship onto the board. I have run through my debugger quite a few times and the same things are happening. It will just stop the sursor a line down and accept no input and not print the board or continue to go through statements. Here is the revised function. Any thoughts are as always very appreciated.

void randomly_place_ships_on_board(char game_board[10][10], char ship, int dir)
{
     int row_coord = 0, col_coord = 0, size = 0, count = 0, row_test = 0, col_test = 0;
  

     if (ship == 'c')
     {
         size = 5;
     }
     else if (ship == 'b')
     {
         size = 4;
     }
     else if (ship == 'r' || ship == 's')
     {
         size = 3;
     }
     else
     {
         size = 2;
     }

     generate_start_pt(size, &row_coord, &col_coord, dir);
     row_test = row_coord;
     col_test = col_coord;

     while (count < size)
     {
         if (dir == 0)
         {
            if (game_board[row_test][col_test] == '~')
            {
                col_test++;
                count++;
            }
            else
            {
             generate_start_pt(size, &row_coord, &col_coord, dir);
             row_coord = row_test;
             col_coord = col_test;
             count = 0;
            }

         }
         else if (dir == 1)
         {
            if (game_board[row_test][col_test] == '~')
            {
                row_test++;
                count++;
            }
            else
            {
             generate_start_pt(size, &row_coord, &col_coord, dir);
             row_coord = row_test;
             col_coord = col_test;
             count = 0;
            }
         }
         else
         {
             printf("bad direction");
         }
     }

     count = 0;

     while (count < size)
     {
         game_board[row_coord][col_coord] = ship;
         if (dir == 0)
         {
            col_coord++;
         }
         else if (dir == 1)
         {
            row_coord++;
         }
         count++;
     }

}

Same crap. Except you added it in TWO places this time. As long as count < size, the loop will never exit. What cold possibly cause that in you new and improved code?

WaltP: please don't put words into my posts that aren't there. I'm saying no such thing. My comments were intended for Jay.

Jay, I'd have to change a few things in your program, to make it work, would that be OK with you?

WaltP: please don't put words into my posts that aren't there. I'm saying no such thing. My comments were intended for Jay.

Such as? Only thing we have a disagreement on is Random being a reserved word. It isn't.

Or are you also implying that when using DevC I can't create a variable named clrscr or gotoxy because it's defined in TurboC3?

I was implying nothing of the sort. I said nothing about DevC, you, or any variable you can or can not create.

It was directed at Jay.

Got out of the infinite loop. Yes! thanks for the help. And I do appreciate the advice on the variable. I did switch it because I honestly didn't know if it was reserved in my compiler, I use Visual Studio 2008 by the way. But it seems like a good habit to avoid the variable in general. Thanks so much for posting! And if I get stuck on a later section of the code I will probably post again lol. Thanks again!

One more question for anyone willing to read this. I also have to output stats to a file. One of the stipulations on this part is I am required to structs. And I have never ever used them. one of the stats I have to output is hit to miss percentage. So I made a function to calculate the hit percentage and then send the value back to the piece of the struct in main. the struct name is stat_t and here is the function that should change the value in main.

void calculate_hit_to_miss (stat_t *stat)
{
    if (stat->misses != 0)
    {
        stat->hit_to_miss_pct = ((double) stat->hits / (double) stat->misses) * 100.0;
    }
    else
    {
        stat->hit_to_miss_pct = 100.0;
    }
}

And i have tried to call it both of these ways. from my understanding of pointers though the way with the address of operator would be correct. But if I do it that way it wont even build:

calculate_hit_to_miss(&stat_player);
calculate_hit_to_miss(stat_player);

Any suggestions or ideas would be very appreciated. Thanks in advance.

For a percent of hits, the equation is:

number of hits / number of guesses

not divided by number of misses. Missing percentage would be:

100 - percent of hits = missing percentage.

And always times 100, if you want to express it that way.

So in your function, you need to only return one value - the percent of hits could be that value.

Instead of using a pointer (did you set that pointer to point to the base of the struct?), you could just assign the struct member, with the return from your function:

with a call like this:

yourStruct.percentHits = calculate_hits(int hitNum, int guessesNum);

And changing 
void calculate_hits(int int) to
float calculate_hits(int int)

Nothing wrong with using a pointer to a struct, but there's nothing wrong with using the return from a function - which is what you describe the function as doing, btw. ;)

Ah i feel a bit stupid. Thanks for the help! and luckily now I'm done with it. Thanks to everyone who gave me suggestions.

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.