The program is supposed to ask the user for the number of characters of which the side of a square is to be built, and the characters of which it's to be built.
As so:
enter number for side of square: 3
enter a character: %26amp;
%26amp;%26amp;%26amp;
%26amp;%26amp;%26amp;
%26amp;%26amp;%26amp;
I can't seem to get it to work... Here's what I have:
#include%26lt;stdio.h%26gt;
int fillBox( int n, char c);
int main() {
int n;
char c;
printf("How many characters long would you like your square, sir?\n");
scanf("%d", %26amp;n);
printf("A very wise choice.\n Enter a character you would like your square to be composed of: ");
scanf("%c", %26amp;c);
printf("%c", fillBox(n, c));
}
int fillBox(int n, char c) {
int side, i;
side = n;
for(i=0; i%26lt;=side; i++)
{
printf("%c", c);
if( i % side == 0)
printf("\n");
}
}
Any advice?
C programming... I can't find what's wrong with my code.?
instead of int use void in the fill box function and just use plain call like fillbox(n,c); in place of printf("%c", fillBox(n, c));
now in function u need double loop
for(i=0;i%26lt;side;i++)
{
for(int j=0;j%26lt;side;j++)
{
printf("%c",c);
}
printf("\n");
}
just replace the loop code with this
Reply:I tried to modify your code and get some output but you require atleast one more for loop to get desired loop in fillbox function.
#include%26lt;stdio.h%26gt;
void fillBox( int n, char c);
int main()
{
int n;
char c;
clrscr();
printf("How many characters long would you like your square, sir?\n");
scanf("%d", %26amp;n);
fflush(stdin); //it is used to clear buffer(memory)
printf("A very wise choice.\n Enter a character you would like your square to be composed of: ");
scanf("%c", %26amp;c);
fillBox(n, c);
getch();
}
void fillBox(int n, char c)
{
int side, i;
side = n;
for(i=1; i%26lt;=side; i++)
{
printf("%c", c);
if(i%side==0)
printf("\n");
}
}
Reply:I'm not sure what is going wrong. (where do you see the problem at) but from looking for it you only have one for loop. This would only do one line, you need another for loop to do either next rows or the columns Just add another for loop after the current one.
for(i=0; i%26lt;=side; i++)
{
for(r=0; r%26lt;=side;r++)
{
printf("%c", c);
Reply:1. The int fillBox( int n, char c) function is void, it just prints.
2.When reading characters the variable must be a string, thus c is char* c, thus we have scanf("%s",%26amp;c)
3.The function does the printing you don't need to call it in the printf command
4.It needs a nested loop as mentioned before.
5.In a loop when you start from 0 and you want 'side' iterations you have to write i%26lt; side
Reply:You need two loops (nested).
for (i=0; i %26lt; side; i++)
{
for(j=0; j %26lt; side; j++)
{
printf("%c", c);
}
printf("\n");
}
what you are doing will not work. When you are traversing the loop, see what happens:
Say n = 3 and c = '%26amp;'
you enter the loop, i = 0
Output :%26amp;
0 % 3 != 0
then you enter loop again
and keep printing %26amp;
Output is :
%26amp;%26amp;%26amp;%26amp;%26lt;newline%26gt;
So better go with a simple nested loop.
I have not tried it out and it may need some tweaking. You can do that. After you write a program, try to work out on a paper what a computer would do. It is possible for small programs like these and will help you to understand the concepts.
Good Luck.
Reply:This code won't compile exactly as written here, but I'm assuming you've tweaked your code so it does compile.
Your main problem is the use of scanf with %c. Scanf with %c does not ignore whitespace, so I think you are immediately scanning in white space instead of waiting for user input.
Check out this link for a discussion of this. It suggests using getchar instead. http://www.geocities.com/learnprogrammin...
Once you get that working, I think you'll have to tweak your for loop to get the exact output you want. You'll be on your way, though!
Good luck.
Reply:check for obvious such as spelling mistakes and wrong symbols spaces etc
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment