CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: My chessboard

  1. #1
    Join Date
    Apr 2011
    Posts
    11

    My chessboard

    here's my code and i need to get te outpu into a text file.
    the output is a simple chessboard. i tried using fstream, printf but couldn't make it work

    sorry for all the confusion in the earlier posts, i'm kinda desperate.
    this code works fine, but only the ouput should be displayed like it is and also saved to a textfile
    which must be named by the user.

    the code:
    #include<iostream>

    using namespace std;

    int main()
    {
    string character, b=" ",stop_go;//initialisation of variables
    int size;
    int n=1;

    while (n>0) //while loop so the user can draw multiple times

    {

    cout<<"Give Chessboard Size:"<<endl; //read-in chessboard information and save in variables
    cin>>size;
    //so only odd numbers will be used
    if(size%2==0)

    {
    cout<<"Only enter odd numbers; please try again"<<endl;
    continue;
    }
    cout<<"Give a Character for your chessboard:"<<endl;
    cin>>character;


    //begin drawing chessboard
    for(int k=1; k<=size //outer loop for vertical drawing


    {

    if (k%2==0)

    for (int i=1; i<=size;i++) //inner loop voor horizontal drawing

    {

    if (i%2==0) cout <<character;
    else cout<< b;

    }
    else


    for (int i=1; i<=size;i++)

    {

    if (i%2==0) cout <<b;
    else cout<< character;

    }

    cout<<endl;
    k++;


    }

    //give user the choice to exit or draw again
    cout<<"Press 'e' to exit or 'd' to try again \n please draw chessboard"<<endl;
    cin>>stop_go;
    if (stop_go=="e") n=0;
    if (stop_go=="d") n=1;



    }


    system("pause");
    return 0 ;

    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: My chessboard

    You think people are telling you to use code tags just because they like typing?

    You'll find help drying up if you keep starting multiple posts with the same question and don't take advice that's given to you.

  3. #3
    Join Date
    Apr 2011
    Posts
    11

    Re: My chessboard

    what are code tags?

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: My chessboard

    Quote Originally Posted by sam2711
    what are code tags?
    For example:
    [code]foo(bar, baz);[/code]
    would result in:
    Code:
    foo(bar, baz);
    It does not matter so much for a single short line, but once things like indentation comes into play, such post formatting becomes important for readability.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Apr 2011
    Posts
    11

    Re: My chessboard

    sorry for all the code without tags
    Code:
    #include<iostream>
    
    using namespace std;
    
      int main()
    {
      string character, b=" ",stop_go;//initialisation of variables
      int size;
      int n=1;
    
     while (n>0)    //while loop so the user can draw multiple times
    
     {
    
     cout<<"Give Chessboard Size:"<<endl; //read-in chessboard information and save in variables
     cin>>size;
                         //so only odd numbers will be used
        if(size&#37;2==0)
    
            {
            cout<<"Only enter odd numbers; please try again"<<endl;
            continue;
            }
     cout<<"Give a Character for your chessboard:"<<endl;
     cin>>character;
    
    
      //begin drawing chessboard
      for(int k=1; k<=size;)   //outer loop for vertical drawing
    
    
          {
    
             if (k%2==0)
    
               for (int i=1; i<=size;i++)   //inner loop voor horizontal drawing
    
                     {
    
                     if (i%2==0) cout <<character;
                     else cout<< b;
    
                     }
              else
    
    
                 for (int i=1; i<=size;i++)
    
                     {
    
                     if (i%2==0) cout <<b;
                     else cout<< character;
    
                     }
    
           cout<<endl;
           k++;
    
    
           }
    
           //give user the choice to exit or draw again
        cout<<"Press 'e' to exit or 'd' to try again \n please draw chessboard"<<endl;
        cin>>stop_go;
        if (stop_go=="e") n=0;
        if (stop_go=="d") n=1;
    
    
    
     }
    
    
       system("pause");
       return 0  ;
    
    }

  6. #6
    Join Date
    Apr 2011
    Posts
    11

    Re: My chessboard

    that's my code, i need to get the ouput into a textfile that will be name by the user.
    i tried to integrate the isdigit function, but it didn't work.
    the program crashes when a letter entered as size instead of a number, can that be resolved?
    thanks in advance

  7. #7
    Join Date
    Sep 2010
    Posts
    39

    Re: My chessboard

    I modified your code as follows so that it ouputs the board to a user specified file as well.
    Code:
    #include<iostream>
    #include <fstream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
    	string character, b=" ",stop_go;//initialisation of variables
    	int size;
    	int n=1;
    
    	string fname;
    	cout<< "File name please: ";
    	cin >> fname;
    	ofstream outf(fname.c_str());
    
    	while (n>0)    //while loop so the user can draw multiple times
    
    	{
    
    		cout<<"Give Chessboard Size:"<<endl; //read-in chessboard information and save in variables
    		cin>>size;
    		//so only odd numbers will be used
    		if(size%2==0)
    
    		{
    			cout<<"Only enter odd numbers; please try again"<<endl;
    			continue;
    		}
    		cout<<"Give a Character for your chessboard:"<<endl;
    		cin>>character;
    
    
    		
    		//begin drawing chessboard
    		for(int k=1; k<=size;)   //outer loop for vertical drawing
    
    
    		{
    
    			if (k%2==0)
    
    				for (int i=1; i<=size;i++)   //inner loop voor horizontal drawing
    
    				{
    
    					if (i%2==0) { cout <<character; outf << character; } 
    					else { cout<< b; outf << b;}
    
    				}
    			else
    
    
    				for (int i=1; i<=size;i++)
    
    				{
    
    					if (i%2==0) { cout <<b; outf << b;}
    					else {cout<< character; outf << character; }
    
    				}
    
    				cout<<endl;
    				outf << endl;
    				k++;
    
    
    		}
    
    		//give user the choice to exit or draw again
    		cout<<"Press 'e' to exit or 'd' to try again \n please draw chessboard"<<endl;
    		cin>>stop_go;
    		if (stop_go=="e") n=0;
    		if (stop_go=="d") n=1;
    
    
    
    	}
    
    
    	system("pause");
    	return 0  ;
    
    }

  8. #8
    Join Date
    Apr 2011
    Posts
    11

    Re: My chessboard

    many thanks mate, works like a charm

  9. #9
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: My chessboard

    Quote Originally Posted by sam2711 View Post
    many thanks mate, works like a charm
    Do you also understand why it works like a charm ?

  10. #10
    Join Date
    Apr 2011
    Posts
    11

    Re: My chessboard

    yes i do, i integrated some badchars code so its even better now...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured