Thread: convert printf int cout

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    12

    convert printf int cout

    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++){

  2. #2
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Can you post more code? That printf is simply like:

    cout << i;

    However more is going on than we can see.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    12
    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;
    }

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    12
    isnt it simply cin>>numb ?

  5. #5
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    scanf() is like

    cin >> numb;

    The printf() is like cout << i;

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    12
    o ok thanks alot. i just wanted to make sure

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    12
    im makeing a battleship program. how do i randomly place ships on the board?

  8. #8
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    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?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i need help with this question:
    By marcuspax in forum C Programming
    Replies: 16
    Last Post: 08-16-2010, 08:12 AM
  2. Can anyone help?
    By javalurnin in forum C Programming
    Replies: 11
    Last Post: 12-02-2009, 06:02 AM
  3. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 08:35 PM
  4. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  5. Replies: 3
    Last Post: 05-13-2007, 08:55 AM