LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-28-2005, 09:09 AM   #1
hubabuba
Member
 
Registered: Jan 2002
Location: UK
Distribution: Slack 14.1
Posts: 193

Rep: Reputation: 30
Problem with reading and printing a character


Hi there!

Have a little program for the exercise i'm doing, where I have to enter a number and a character and the program will print the character entered number of times in a line and the same number of lines. So if i enter 3 and enter the character '#", the program will print:

###
###
###

The problem though, is that after i enter the number, program will not let me enter a character and just terminates looking like this:

[root@dhcppc0 prog]# ./program
Enter the square's side:2
Enter a character:





[root@dhcppc0 prog]#

The code of the program:

Code:
#include <stdio.h>

void square(int side, char fillCharacter);

int main()
{
  int a;
  char c;
  
  printf("Enter the square's side:");
  scanf("%d", &a);
  printf("Enter a character: ");
  scanf("%c", &c);
    
  square(a, c);
  
  return 0;
}

void square(int side, char fillCharacter)
{
  int width, height;
  
  for(height = 1; height <= side; height++)
  {
    for(width = 1; width <= side; width++)
    {
      printf("%c", fillCharacter);
    
      if(width == side)
        printf("\n");
    }
  }   
}
Thanks

Last edited by hubabuba; 02-28-2005 at 09:10 AM.
 
Old 02-28-2005, 10:05 AM   #2
ortho-orange#42
LQ Newbie
 
Registered: Feb 2005
Distribution: Slackware
Posts: 16

Rep: Reputation: 2
Re: Problem with reading and printing a character

Code:
#include <stdio.h>

void square(int side, char fillCharacter);

int main()
{
  int a;
  char c;
  
  printf("Enter the square's side:");
  scanf("%d", &a);
  printf("Enter a character: ");
  scanf("%c", &c);
    
  square(a, c);
  
  return 0;
}
<snip>
}

I've never been able to store scanf's results in an int, or char (maybe my own lack of experience). however, you can define a temporary char pointer
Code:
char *s;
char *c;
pass that to scanf:
Code:
scanf("%s", s);   // get squares side
scanf("%s", c);  // get character
and then use atoi to convert that to an int.
Code:
a=atoi(s);
Then, only pass the first element of the string to your square function.
Code:
square(a, c[0]);
Also, you'll need to include stdlib.h.
This worked for me, but not being too big into C, there's probably better, or more correct ways to do it. Unless I changed something and forgot, your square function is, well...functional

HTH
 
Old 02-28-2005, 11:09 AM   #3
hubabuba
Member
 
Registered: Jan 2002
Location: UK
Distribution: Slack 14.1
Posts: 193

Original Poster
Rep: Reputation: 30
The program is now working, all I had to do is to put a space between double quote and the remainder operator in the scanf expression like this:

Code:
scanf(" %c", &c);
instead of:

Code:
scanf("%c", &c);
, but I don't understand why this made it work

Can anyone explain?

thanks
 
Old 02-28-2005, 02:41 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
When the first scanf() runs, you press ENTER to submit the number you just typed. The ENTER puts a newline character ('\n') in the input buffer, and still needs to be "read away" before scanf() will read the next item. This is so because scanf() expects whitespace (one or more spaces, tabs, or newlines) between items.

When you put a space in scanf()'s format string, this tells scanf() to "read away" any whitespace, after which it will accept the next item. In your case this next item will be the char (%c).

I don't like scanf()....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ncurses printing character array prob. slzckboy Programming 3 07-31-2005 08:51 AM
reading and printing out a text file in html mrobertson Programming 1 07-08-2005 11:47 PM
problem with character vacilus Linux - Software 0 02-17-2005 05:34 AM
Keyboard Character problem anorman Linux - Software 0 12-06-2004 04:53 PM
Problem with THAI character nandipinto Programming 0 08-24-2004 02:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration