can someone help me convert this into cout?
Code:scanf( "%d", &numb ); cout << " A B C D E F G H I J K L\n"; cout << "----------------------------\n";Code:for(i = 0; i < 12; i++){ printf( "%d", i ); for(j = 0; j < 12; j++){
This is a discussion on convert printf int cout within the C++ Programming forums, part of the General Programming Boards category; can someone help me convert this into cout? Code: scanf( "%d", &numb ); cout << " A B C D ...
can someone help me convert this into cout?
Code:scanf( "%d", &numb ); cout << " A B C D E F G H I J K L\n"; cout << "----------------------------\n";Code:for(i = 0; i < 12; i++){ printf( "%d", i ); for(j = 0; j < 12; j++){
Can you post more code? That printf is simply like:
cout << i;
However more is going on than we can see.
the whole thing is there and the scaf is if highlighed
Code:#include <iostream> #include <windows.h> using namespace std; int choose(){ int numb; scanf( "%d", &numb ); system("color 12"); cout << " S I N K M Y B A T T L E S H I P!!!!!\n"; if(numb < 0 || numb > 12){ cout<<"Invalid choice, choose again.\n"; numb = choose(); } return numb; } void board(int graph[][12]) { int i; int j; cout << " A B C D E F G H I J K L\n"; cout << "----------------------------\n"; for(i = 0; i < 12; i++){ printf( "%d", i ); for(j = 0; j < 12; j++){ if(graph[i][j] == 0) cout<< " " ; else if(graph[i][j] == 1) cout<< "X" ; else if(graph[i][j] == 2) cout<<"-" ; } cout<<"\n" ; } cout<<"\n" ; } void shoot(int graph[][12], int ships[][12], int y, int x, int *game) { if(graph[y][x] == 1 || graph[y][x] == 2){ cout<<"This space has already been chosen, choose another space\n" ; cout<< "Row:" ; y = choose(); cout<<"Column:"; x = choose(); shoot(graph, ships, y, x, game); } else{ if(ships[y][x] == 1){ cout<<"HIT!\n"; graph[y][x] = 1; ships[y][x] = 0; } else if(ships[y][x] == 0){ cout<< "Miss.\n" ; graph[y][x] = 2; } } int k; int l; int check = 0; for(k = 0; k < 12; k++){ for(l = 0; l < 12; l++){ if(ships[k][l] == 1) check = 1; } } if(check == 1) game = 0; } int main() { int graph1[12][12] = {{0}, {0}}; int ships1[12][12] = {{0}, {0}}; int graph2[12][12] = {{0}, {0}}; int ships2[12][12] = {{0}, {0}}; int y; int x; int gamestatus = 1; while(gamestatus == 1){ cout<< "\nPlayer 1's turn\n" ; board(graph2); cout<<"Row:" ; y = choose(); cout<<"Column:" ; x = choose(); shoot(graph2, ships2, y - 1, x - 1, &gamestatus); if(gamestatus == 0){ cout<< "PLAYER 1 WINS!\n" ; break; } cout<<"\nPlayer 2's turn.\n" ; board(graph1); cout<<"Row:" ; y = choose(); cout<<"Column:" ; x = choose(); shoot(graph1, ships1, y - 1, x - 1, &gamestatus); if(gamestatus == 0) cout<<"PLAYER 2 WINS!\n" ; else cout<<"\nNext round.\n" ; } return 0; }
isnt it simply cin>>numb ?
scanf() is like
cin >> numb;
The printf() is like cout << i;
o ok thanks alot. i just wanted to make sure
im makeing a battleship program. how do i randomly place ships on the board?
Generate a random number which initializes some loop. The loop then calls function which returns a random x/y coordinate. This loop executes a random number of times.
The end result, a random number of ships placed at random points on the board. Use mod to help narrow down the random numbers to something acceptable.
Cprogramming.com FAQ > Generate random numbers?
In the future, make sure to indent your code. We don't want to wade through ugly, unintended code to help people. You'll get more responses that way and it will help you find own mistakes, as well.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^