help me in this prg

A

anand devarajan

#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
 
A

anand devarajan

anand said:
#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
 
F

Fred Kleinschmidt

This is not standard C.
Not standard C
Wrong. col is a char. You cannot read it as an integer.
Why are you adding a char to an int?
Not standard C

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.
 
C

Christopher Benson-Manica

anand devarajan said:
#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.
 
O

osmium

:

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.
 
T

T.M. Sommers

anand said:
#include"stdio.h"

Should be said:
#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;
 
M

Martin Ambuhl

anand said:
#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
#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.
 
K

Keith Thompson

osmium said:
:

I don't know what your intent is, but I will offer an idea anyway. Feel
free to ignore what I say.


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.
 
A

Andrew Poelstra

anand devarajan said:
[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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top