ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
#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
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.