9 Replies - 378 Views - Last Post: 21 April 2011 - 07:53 AM Rate Topic: -----

#1 xchiodosx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 20-April 11

Is there a way to refresh the screen to display new outputs?

Posted 21 April 2011 - 06:30 AM

Im making a battleship program and I was wondering is there a way to refresh the screen so I can see where I have placed the battleships?
I have a 2D array full of 0's and when I place the ship I want it to display a 1 instead of a 0. Oh and do I need to use a printing function to display the new values on the array? Thanks =)
#include <iostream>
#include <cstdlib>
#include <time.h>

const int LOW = 0;
const int HIGH = 4;
const int HIGH2 = 9;

using namespace std; 

int main()
{
    int board [10][10]=//this is the 2d array for the board
{
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};

for(int row=0;row<10;row++){//this is the loop to display the board
        for(int column=0;column<10;column++)
        {
                cout << board[row][column] << " ";
                }
                cout <<endl;
                }

                  
{//this is the random value generator
           
                int row, column;
                time_t seconds;
                time(&seconds);
                srand((unsigned int)seconds);
                row = rand() % (HIGH2 - LOW + 1) + LOW;
                column = rand() % (HIGH - LOW + 1) + LOW;
                board[row][column] = 1;
                board[row+1][column+1]=1;
                
                cout<< "coordinate is (" << row << ", "
                << column << "}" << endl << endl;   
                            
                }
                                                
             
{{//asking user what position they want their ship to be in
 int answer;
 cout << "Would you want your ship 1 verticaly or 2 horizontaly?" << endl;
 cin>> answer;
 if(answer<=1)
 cout<<"your ship will be placed verticaly" << endl;
 else if (answer <= 2)
 cout<<"your ship will be placed horizontaly" << endl;
 else
 cout<<"enter a value of 1 or 2" << endl;

}  //asking user what coordinate they would like to place the ship
int x,y;
cout << "what coordinate would you like to place your ship" << endl;
cin>> x;
cin>> y;
board[x][y]=1; //changing the value of the choosen coordinate to 1 
 
     }    
              return 0;
}


This post has been edited by JackOfAllTrades: 21 April 2011 - 07:21 AM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Is there a way to refresh the screen to display new outputs?

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,753
  • Joined: 25-December 09

Re: Is there a way to refresh the screen to display new outputs?

Posted 21 April 2011 - 06:42 AM

There is no standard way to clear the screen in C/C++ except to issue enough new lines to clear the screen, although this leaves you at the bottom of the screen. However there may be non-standard platform dependent method depending on your operating system.

EDIT: Please use code tags for your code.

Jim

This post has been edited by jimblumberg: 21 April 2011 - 06:42 AM

Was This Post Helpful? 0
  • +
  • -

#3 xchiodosx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 20-April 11

Re: Is there a way to refresh the screen to display new outputs?

Posted 21 April 2011 - 06:49 AM

oops I thought I did sorry. So if I used code to clear the screen it would place the new values into my array?
thanks

I tried code to clear the screen and it got rid of everything above the clear screen including my array, I just want to replace the values array =( If i do use the clear screen code is there a way to display the array again with the new values?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,753
  • Joined: 25-December 09

Re: Is there a way to refresh the screen to display new outputs?

Posted 21 April 2011 - 07:02 AM

Re-print the array that has the correct values.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 xchiodosx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 20-April 11

Re: Is there a way to refresh the screen to display new outputs?

Posted 21 April 2011 - 07:08 AM

This probably sounds like a stupid question but how do I do that?
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5957
  • View blog
  • Posts: 23,226
  • Joined: 23-August 08

Re: Is there a way to refresh the screen to display new outputs?

Posted 21 April 2011 - 07:21 AM

The right way to use code tags:

:code:

If that's too complicated, see the first link in my signature.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,753
  • Joined: 25-December 09

Re: Is there a way to refresh the screen to display new outputs?

Posted 21 April 2011 - 07:22 AM

How did you print it in the first place?

Jim
Was This Post Helpful? 0
  • +
  • -

#8 xchiodosx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 20-April 11

Re: Is there a way to refresh the screen to display new outputs?

Posted 21 April 2011 - 07:34 AM

Thats how I printed it, How do I call upon this function again to reprint the array :o?

Okay, I think I need to read your guide on code tags

for(int row=0;row<10;row++){//this is the loop to display the board
        for(int column=0;column<10;column++)
        {
                cout << board[row][column] << " ";
                }
                cout <<endl;
                }

Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,753
  • Joined: 25-December 09

Re: Is there a way to refresh the screen to display new outputs?

Posted 21 April 2011 - 07:45 AM

I would probably put the code to print this array into a function and then call the function when I need to print the array.

Check the links in JackOfAllTrades signature if you don't know how to use functions.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 xchiodosx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 20-April 11

Re: Is there a way to refresh the screen to display new outputs?

Posted 21 April 2011 - 07:53 AM

Okay, I will try that, thanks =)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1