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 ; }




Reply With Quote