Long story short I'm running Eclipse CDT. Below is my code, a simple input character function. However when I run it (without errors*) it requres me to input a character for the variable 'c' before displaying the printf statement. I have tried using a puts statement as well as making two printf lines to no avail. Can someone please direct me to the best solution, or tell me if this is more likely a eclipse cdt issue.
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char c;
printf("Please input a character: %c\n", c = getchar());
if ((c >= 'a') && (c <= 'z'))
printf("%c is a lower-case letter.", c);
else if ((c >= 'A') && c<= 'Z')
printf("%c is a capital letter.", c);
else if ((c >= '0') && c <= '9')
printf("%c is a digit!", c);
return EXIT_SUCCESS;
}
Thank you for any and all help!
These two lines:
char c;
printf("Please input a character: %c\n", c = getchar());
are not correct and are the reason you have to input a char before the print occurs.
The following will work correctly:
int c;
printf("Please input a character: ");
fflush( stdout );
c = getchar();
You might also want to look at:
isupper()
islower()
isdigit()
which are (normally) found in <ctypes.h>
In your code, you have c = getchar()as a parameter to printf. This parameter has to be evaluated before printf can be called.
You might have better luck using:
printf("Please input a character\n");
c = getchar();
Although, as I read the comments added above, it sounds like there may be a buffering problem as well that will prevent the output from being written. But try this first.
Related
I'm trying to write a program that outputs non-vowel characters (without if statements and using formatted scanf input). The code I currently have does not apply the %*[] ignored characters to the first %c character scanned, but the restriction applies for the other characters. For example, "Andrew" becomes "Andrw" instead of "ndrw". I'm suspecting this could be due to the %c at the beginning. Could someone help me please? :)
#include <stdio.h>
#include <string.h>
int main(void) {
char c;
while (scanf("%c%*[aeiouAEIOU]", &c) == 1)
printf("%c", c);
return 0;
}
The scanf formats are matched in order so %c is matched first for the A. You need to use 2 separate scanfs for this, or precede the loop with the initial-vowel eating scanf:
scanf("%*[aeiouAEIOU]");
while (scanf("%c%*[aeiouAEIOU]", &c) == 1) {
printf("%c", c);
}
The question is is this any clearer and better than
int c;
while ((c = getchar()) != EOF) {
if (! strchr("aeiouAEIOU", c)) {
putchar(c);
}
}
I have an opinionated answer...
Can someone please provide me some examples? Thank you :)
#include <stdio.h>
int main()
{
int tcount = 0, ccount = 0, dcount = 0;
char ch;
printf("Enter your characters (! to end): \n");
/* What program code to write to get the result? */
printf("digits: %d\n", dcount);
printf("letters: %d\n", ccount);
return 0;
}
is it using for loop?
for (tcount=0;tcount<10;tcount++)
{
scanf("%c",&ch);
if(ch == '!')
break;
}
Test Result:
hello 5432 user#
digits: 4 letters: 9
I would recommend you to use getchar() instead of scanf() for reading single characters.
Or if you have to, you have to skip leading whitespaces
scanf(" %c",&ch);
^ Note the space
Here is a simple example which could be helpful to you, using functions isdigit() and isalpha() from ctype.h library.
int c, numberCounter = 0, letterCounter = 0;
while ((c = getchar()) != '!')
{
if (isalpha(c))
{
letterCounter++;
}
else if (isdigit(c))
{
numberCounter++;
}
}
If you can't use additional libraries like ctype.h, take a look at the ASCII table, for example
if (c >= '0' && c <= '9') // '0' == 48, '9' == 57
{
// c is digit
}
Try something like:
do
{
char c = getchar();
if(c!='!')
{
... do something ....
}
}
while(c != '!');
Yes you need to use a loop for, or a while:
for (tcount=0;tcount<10;tcount++)
{
scanf("%c",&ch);
if(ch == '!')
break;
}
or the while code:
while(ch != '!'){
scanf("%c",&ch);
printf("There are nothing to see here");
}
The POSIX getdelim function does exactly what you're asking for (most code floating around uses getline, but it is exactly the same other than the extra argument). Beware the possibility that the delimiter does not occur within the buffer size.
Also, for interactive input, you might want to put the TTY in raw mode, or the user will have to press enter anyway.
I am just learning c, and I am using Linux terminal. I have written the following simple code, but when I type the inputs, the file does not exit and thus does not count the number of characters. Can anyone help me with it? I have tried other input codes as well. It's the same with all my input related codes. What am I doing wrong? Please help.
main()
{
/* count characters in input */
printf("Type some characters and the program will count the number of characters: ");
int c = getchar();
while(c!=EOF && c!= '\n')
++c;
printf("Number of characters typed: %1d\n", c);
}
So to take notice of the helpful comments;
#include <stdio.h>
int main(){
int c;
int count = 0;
while((c=getchar()) != '\n' && c != EOF)
count++;
printf("%d\n", count);
};
This code works as expected.
Hi I'm relatively new to programming so please bear with me.
I would like to make a program that takes the input bcdefghijklmnopqrstuvwxy and outputs
else if (c == 'x')
++nx;
where x is a letter of the input, and where the output is repeated for each letter of the input.
This is what I have written so far:
#include <stdio.h>
main() {
int c;
while((c = getchar()) != EOF) {
printf("else if (c == '%d')\n", c);
printf("\t++n%d;\n", c);
}
return 0;
}
Instead of returning the output I want, the output is
else if (c == '98')
++n98;
else if (c == '99')
++n99;
else if (c == '100')
++n100;
else if (c == '101')
++n101;
else if (c == '102')
++n102;
...
Why is c not working as a variable?
Thanks so much for your help!
You want c == '%c' to compare by character or c == %d (without the single quotes) to compare by ordinal value, but you should really learn to use arrays. It looks like you are trying to code something the hard way, and use a code generator to save you some typing. Instead:
int n[256] = {0}; /* storage for counters, initialized to zero */
and:
n[c]++; // increment the counter for character c;
You're code will be much shorter.
When writing in C and printing a string pointed by format to stdout you'll need to make sure you're using the right format specifiers. This will ensure that your argument is formatted correctly and inserted into the resulting string as you would expect.
In your case, you need to use %c. However, you have %d currently, which is the equivalent to %i for integers. You can google more on format specifiers to learn more about other options as well.
Here's an interesting read on the subject:
http://www.codingunit.com/printf-format-specifiers-format-conversions-and-formatted-output
You used %d in your printf but %d if for integers. You want to print a char so it should be %c.
Replace %d by %c
More on this link: http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/FUNCTIONS/format.html
Try:
#include <stdio.h>
main() {
char c;
while((c = getchar()) != EOF) {
printf("else if (c == '%c')\n", c);
printf("\t++n%c;\n", c);
}
return 0;
}
what you want is characters not ints
Try using
printf("else if (c == '%c')\n", c);
printf("\t++n%c;\n", c);
See printf specifiers
The %d specifier is for intergers
I am trying to make the program print out "OK" for as long as i enter 'y' as a choice at the end, but it is not looping, it just prints out "OK" and ends the program even if i enter a 'y' at the end. Please help.
#include <stdio.h>
int main()
{
char c = 'y';
while (c == 'y')
{
printf_s("OK\n");
scanf_s("%c", &c);
if (c != 'y')
{
break;
}
}
return 0;
}
On first iteration when you press Enter key then a newline character \n is passed to the input along with y. On second iteration scanf_s reads \n.
Change
scanf_s("%c", &c);
to
scanf_s(" %c", &c);
^Notice the space before %c
A space before %c specifier can consume any number of white-space characters.
Change the scanf_s line as follows
scanf_s("%c", &c, 1);
This extra parameter is specifying the size of the c argument. Plain old scanf doesn't require this argument but the versions ending with _s do
Also the if block with the break statement is unnecessary because the conditional on the while loop effectively does the same thing. It could be written as follows
while (c == 'y')
{
printf_s("OK\n");
scanf_s("%c", &c, 1);
}
OK I used scanf instead of scanf_s, that solved the problem, thanks everyone.
You are wrong with scanf
if you use like this u will see it is working ..
#include <stdio.h>
int main()
{
int c = 1;
while (c == 1)
{
printf_s("OK\n");
scanf_s("%d", &c);
if (c != 1)
{
printf_s("hello\n");
continue;
}
}
return 0;
}
Put a space before %c to skip whitespace.