By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,131 Members | 1,977 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,131 IT Pros & Developers. It's quick & easy.

help me in this prg

P: n/a
#include"stdio.h"
#include<conio.h>
#include<math.h>
void main()
{
int row,result;
char col;
clrscr();
printf("enter the row\n");
scanf("%d",&row);
printf("enter the col\n");
scanf("%d",&col);
result=row+col;
if(result<96||result>110)
printf("not valid");
else
{
if(result%2==0)
printf("black");
else
printf("white");
}
getche();
}
this is my prg for color of the squares in chess board i dont know wats
the mistake in this its not getting executed correctly i want this prg
to get the square name eg:a1 from the user and display the output that
it is whether black or white i had initialised col as char and made it
as ascii value using %d in scanf and thus using it in result i even
want a if statement to print the invalid statement if the inout given
is not valid eg:char >h and num>8

Sep 26 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a

anand devarajan wrote:
#include"stdio.h"
#include<conio.h>
#include<math.h>
void main()
{
int row,result;
char col;
clrscr();
printf("enter the row\n");
scanf("%d",&row);
printf("enter the col\n");
scanf("%d",&col);
result=row+col;
if(result<96||result>110)
printf("not valid");
else
{
if(result%2==0)
printf("black");
else
printf("white");
}
getche();
}
this is my prg for color of the squares in chess board i dont know wats
the mistake in this its not getting executed correctly i want this prg
to get the square name eg:a1 from the user and display the output that
it is whether black or white i had initialised col as char and made it
as ascii value using %d in scanf and thus using it in result i even
want a if statement to print the invalid statement if the inout given
is not valid eg:char >h and num>8
i need full executable prg for this

Sep 26 '06 #2

P: n/a
anand devarajan <is**********@gmail.comwrote:
#include"stdio.h"
#include<conio.h>
#include<math.h>
void main()
Three big no-no's already, and that's before you even get to the code.
Try a properly indented C program (which was not what you posted):

#include <stdio.h>
#include <math.h>

int main(void)
{
int row,result,col;
printf("enter the row\n");
scanf("%d",&row);
printf("enter the col\n");
scanf("%d",&col);
result=row+col;
if(result<96||result>110)
printf("not valid");
else {
if(result%2==0)
printf("black\n");
else
printf("white\n");
}
return 0;
}

This obviously isn't correct, but a regular might now be willing to
attempt to help you. Examine the differences between your code and
this closely.

I've snipped the rest of your post, because it was unparseable.
Please write in English, like this. Usenet is not a text message
service.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Sep 26 '06 #3

P: n/a
"anand devarajan" writes:

I don't know what your intent is, but I will offer an idea anyway. Feel
free to ignore what I say.
#include"stdio.h"
#include<conio.h>
#include<math.h>
void main()
{
int row,result;
char col;
clrscr();
printf("enter the row\n");
scanf("%d",&row);
printf("enter the col\n");
scanf("%d",&col);
result=row+col;
Adding rows and columns makes me nervous. Isn't the idea to uniquely
identify a cell? Is 5+3 different than 3+5? I would expect to see a
muilitply in there too.
if(result<96||result>110)
printf("not valid");
else
{
if(result%2==0)
printf("black");
else
printf("white");
}
getche();
}
this is my prg for color of the squares in chess board i dont know wats
the mistake in this its not getting executed correctly i want this prg
to get the square name eg:a1 from the user and display the output that
it is whether black or white i had initialised col as char and made it
as ascii value using %d in scanf and thus using it in result i even
want a if statement to print the invalid statement if the inout given
is not valid eg:char >h and num>8
Toget any serious help you will have to get rid of conio.h and its spawn.
It is not standard and no real need for it leaps out at me.
Sep 26 '06 #4

P: n/a
anand devarajan wrote:
#include"stdio.h"
Should be <stdio.h>
#include<conio.h>
Not part of the C standard, so should not be used in this newsgroup.
#include<math.h>
Not used.
void main()
int main(void)
{
int row,result;
char col;
clrscr();
Not a standard function.
printf("enter the row\n");
From the description of the program below, you should be asking
for the column first.
scanf("%d",&row);
If you are going to use two scanf()s, then you must gobble up the
rest of the first line, including the terminating newline,
before trying the second scanf(). You might want to consider
combining the two, so that the user can enter 'a1' instead of 'a'
and then '1' without getting a superfluous prompt.

You should also check the input for validity before proceeding.
printf("enter the col\n");
scanf("%d",&col);
The correct format for a character is %c. This input also needs
to be checked for validity.
result=row+col;
if(result<96||result>110)
You should never use magic numbers like these. This check is
also wrong. Invalid input passes the test (col: k; row: -5), and
valid input fails (col: h; row: 8). Additionally, not all
character sets have all the letters in contiguous blocks.
printf("not valid");
This and the following printf()s need terminating newlines.
else
{
if(result%2==0)
printf("black");
else
printf("white");
}
getche();
Not a standard function. You also need a return:

return 0;
}
this is my prg for color of the squares in chess board i dont know wats
the mistake in this its not getting executed correctly i want this prg
to get the square name eg:a1 from the user and display the output that
it is whether black or white i had initialised col as char and made it
as ascii value using %d in scanf and thus using it in result i even
want a if statement to print the invalid statement if the inout given
is not valid eg:char >h and num>8

--
Thomas M. Sommers -- tm*@nj.net -- AB2SB

Sep 26 '06 #5

P: n/a

"anand devarajan" <is**********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>
anand devarajan wrote:
>#include"stdio.h"
#include<conio.h>
This is not standard C.
>#include<math.h>
void main()
{
int row,result;
char col;
clrscr();
Not standard C
> printf("enter the row\n");
scanf("%d",&row);
printf("enter the col\n");
scanf("%d",&col);
Wrong. col is a char. You cannot read it as an integer.
> result=row+col;
Why are you adding a char to an int?
> if(result<96||result>110)
printf("not valid");
else
{
if(result%2==0)
printf("black");
else
printf("white");
}
getche();
Not standard C

>}
this is my prg for color of the squares in chess board i dont know wats
the mistake in this its not getting executed correctly i want this prg
to get the square name eg:a1 from the user and display the output that
it is whether black or white i had initialised col as char and made it
as ascii value using %d in scanf and thus using it in result i even
want a if statement to print the invalid statement if the inout given
is not valid eg:char >h and num>8
i need full executable prg for this
A simpler algorithm:
Given a column in row 1, you can determine if it should
be black or white.
If the row is even, invert your answer.

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Sep 26 '06 #6

P: n/a
anand devarajan wrote:
#include"stdio.h"
^^^^^^^^^
This suggests you are using your own private version of "stdio.h"
rather than the system header <stdio.h>. If that is the case, then
nothing in your "stdio.h" is known to us, and we cannot comment on your
use of it.
#include<conio.h>
^^^^^^^^^
There is no such standard header as <conio.h>. Nothing in your
implementation's <conio.his known to us, and we cannot comment on your
use of it.
#include<math.h>
^^
By the way, a single space would not cost you much. The cost (using
some white space) is tiny compared to the the reward (improving
readability). Similar comments hold for your indentation below.
void main()
^^^^
main, by definition, returns an int in a hosted implementation. All
bets are off once you make this tyro's error.
{
int row,result;
char col;
clrscr();
^^^^^^
There is no such standard function.
printf("enter the row\n");
scanf("%d",&row);
printf("enter the col\n");
scanf("%d",&col);
^^^
You are telling scanf that it should store a (decimal) int into what
is actually a char. Unless ints and chars are the same thing for your
implementation -- fairly rare -- then you are in trouble. But you don't
want a value interpreted as an integer, you are expecting a value from
"abcdefgh" (and your program should say say). The specifier you want
here for a char is "%c".
result=row+col;
^^^^^^^
This is obviously wrong. It assumes that 'a'...'h' have certain
encodings not assured by any means. You could easily look up the
address of col in an array holding "abcedefgh", returned as pointer from
strchr, and subrract the base address of the array when the returned
value is not NULL, giving a value [0..8] to use. This would be portable
and not raise many eyebrows.
if(result<96||result>110)
printf("not valid");
else
{
if(result%2==0)
printf("black");
else
printf("white");
}
getche();
}
this is my prg for color of the squares in chess board i dont know wats
the mistake in this its not getting executed correctly i want this prg
to get the square name eg:a1 from the user and display the output that
it is whether black or white i had initialised col as char and made it
as ascii value using %d in scanf and thus using it in result i even
want a if statement to print the invalid statement if the inout given
is not valid eg:char >h and num>8
Sep 26 '06 #7

P: n/a
"osmium" <r1********@comcast.netwrites:
"anand devarajan" writes:

I don't know what your intent is, but I will offer an idea anyway. Feel
free to ignore what I say.
>#include"stdio.h"
#include<conio.h>
#include<math.h>
void main()
{
int row,result;
char col;
clrscr();
printf("enter the row\n");
scanf("%d",&row);
printf("enter the col\n");
scanf("%d",&col);
result=row+col;

Adding rows and columns makes me nervous. Isn't the idea to uniquely
identify a cell? Is 5+3 different than 3+5? I would expect to see a
muilitply in there too.
[...]

Your nervousness is understandable, but in this case I think adding
the row and the column is actually sensible. The program attempts to
indicate whether a given square on a chessboard is black or white.
The square at row 5, column 3 is the same color as the one at row 3,
column 5.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 26 '06 #8

P: n/a
On Tue, 2006-26-09 at 18:59 +0000, Christopher Benson-Manica wrote:
anand devarajan <is**********@gmail.comwrote:
>[bad code]
#include <stdio.h>
#include <math.h>

int main(void)
{
int row,result,col;
printf("enter the row\n");
scanf("%d",&row);
printf("enter the col\n");
scanf("%d",&col);
result=row+col;
if(result<96||result>110)
printf("not valid");
else {
if(result%2==0)
printf("black\n");
else
printf("white\n");
}
return 0;
}

This obviously isn't correct, but a regular might now be willing to
attempt to help you. Examine the differences between your code and
this closely.

I've snipped the rest of your post, because it was unparseable.
Please write in English, like this. Usenet is not a text message
service.
I'll take the liberty of cleaning up the style somewhat (thanks for
making it readable, Chris :-)) I've also replaced some of his ASCII
assumptions with proper C. Finally, I marked all the lines that are
obviously spurious:

#include <stdio.h>
#include <ctype.h>
#include <math.h>

int main(void)
{
int row, result, col;
printf ("Enter the row: ");
scanf("%d", &row);
printf ("Enter the column: ");
scanf("%d", &col);

result = row + col; /* This line is spurious. */
if (!isalpha (result)) /* This line is spurious. */
fputs ("Not valid!", stderr);
else {
if (result % 2 == 0)
puts ("Black.");
else
puts ("White.");
}
return 0;
}
It's actually pretty obvious what it's supposed to do, now. The OP
should note the importance of formatting.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Sep 26 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.