-
April 27th, 2011, 05:41 PM
#1
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 ;
}
-
April 27th, 2011, 08:39 PM
#2
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.
-
April 27th, 2011, 09:56 PM
#3
-
April 27th, 2011, 10:09 PM
#4
Re: My chessboard
Originally Posted by sam2711
what are code tags?
For example:
[code]foo(bar, baz);[/code]
would result in:
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.
-
April 27th, 2011, 10:29 PM
#5
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%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 ;
}
-
April 27th, 2011, 10:32 PM
#6
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
-
April 28th, 2011, 07:52 AM
#7
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 ;
}
-
April 28th, 2011, 05:36 PM
#8
Re: My chessboard
many thanks mate, works like a charm
-
April 29th, 2011, 02:29 AM
#9
Re: My chessboard
Originally Posted by sam2711
many thanks mate, works like a charm
Do you also understand why it works like a charm ?
-
April 29th, 2011, 03:36 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|