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