I am new to C and so I wanted to get more experience with the libraries function. When I started fiddling with getch() I could't for the life of me get it to work. All that happened was that the characters were echoed back to the console and I could type multiple and even press enter without it continuing throughout the program. Here is a little test code I made. Any idea where I am going wrong?
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(){
char i = '9';
i = getch();
printf("%c", i);
}
EDIT: Thank you all for your answers although I could've been more clear on what I wanted. I want to be getch() to be used almost like fgets() except I don't want it to echo and I want the console to accept the character entered immediately without any enter key pressed. Is getch the wrong tool to do this with?
Change this
i = getch();
to
i = getchar();
getchar() is a standard function that gets a character from the stdin.
getch() is non-standard. It gets a character from the keyboard (which could be different from stdin) and does not echo it.
getch() is a function is used to end or close main function used in windows with conio header file.
there is no use of stdlib header function it is used while taking string
as a function.
%c is used to print characters.
if u wants to print i than go like as mention below:-
#include <stdio.h>
#include <conio.h>
void main()
{
int i = 9;
printf("%d", i);
getch();
}
Related
This question already has an answer here:
C Cyrillic console input with standard char data type instead of wchar_t
(1 answer)
Closed 6 days ago.
The purpose of the program is to get a symbol and then immediately display it on the screen. But the problem is that it outputs a different character, or nothing at all. I found that the output is in Windows-1251 encoding, and the input is in CP866. How do I solve this problem? How to make both output and input in Windows-1251 encoding.
Post Scriptum: the problem appears when you enter a Cyrillic character.
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int main(void)
{
setlocale(LC_ALL, "");
printf("Уведіть символ.\n");
char ch = getchar();
printf("\n");
printf("%c", ch);
getchar();
getchar();
return 0;
}
I tried to use wchar_t (respectively procedures wprintf(), getwchar()), but the situation did not change.
Maybe this will help?
https://stackoverflow.com/a/44167461/8893124
they suggest to set:
system("chcp 1251");
setlocale(LC_ALL, "UTF8");
I found another way to solve this problem. Maybe someone will find it useful. However, it works only for Windows.
For this you need to include the header file <windows.h> and write before entering the following:
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
Then the input/output will be in Windows-1251 encoding.
I'm using eclipse in Windows, and minGW.
I write the easy code like this.
#include <stdio.h>
int main()
{
char c;
printf("input: ");
scanf("%c",&c);
return 0;
}
Buffer error is rised, and I search the stackoverflow. It told me to use fflush().
And it works.
But, I must write fflush() after each printf().
I don't wanna do this.
I want to solve this problem in one statement.
The purpose of this program is to count the digits in an alphanumeric input. However, I used a loop to not execute the program unless the input is alphanumeric.
This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
int main(){
int input,isanum,digitcount;
printf("\nEnter a number: ");
scanf("%d",&input);
isanum=isalnum(input);
while(isanum==0){
printf("\nIncorrect input. Try again: ");
scanf("%d",&input);
isanum=isalnum(input);
}
digitcount=0;
while(input!=0){
input=input/10;
digitcount++;
}
printf("\nNumber of digits = %d",digitcount);
return 0;
}
The problem is with the loop. It keeps looping infinitely and ignores the scanf statement and I don't know why. Am I using isalnum() incorrectly here?
You need to check the return value of scanf().
isalnum() takes a character, but you are passing an int. '1' is not the same as 1 in C.
You probably should consume an entire line each time, e.g. with fgets(), and then check if it fits your desired input format, e.g. with sscanf().
As it stands, you never consume anything, you just keep trying to read a number but there isn't one there so it fails every time. Failing to check the return value of scanf() is a contributing factor to your not noticing this.
Look at how isalnum() is defined: it expects a char *. You, however, give it an int *. The data is stored completely different there.
Besides, if you are reading in an int, you know beforehand that it will be alphanumeric, right?
My instructions: Write a program that repeatedly (until end-of-file) reads in a character
from the input stream. If the character is upper case, change it to lower case
and write it to the output stream. For all other characters, write the
character unchanged to the output stream.
Use getchar() for input, Use putchar() for output, and use input redirection
for connecting the input file to the program
My project name is Input and my textfile is input.txt. When I run it I type "Input < input.txt" The program just mimics that on the command window though so how do I get it to read from the text file?
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
char c, x;
c=getchar();
while (c != EOF)
{
c=getchar();
x = tolower(c);
if (isupper(c))
{
putchar(x);
}
else
{
putchar(c);
}
}
system("Pause");
}
I believe the problem is that you do not want to go to the program and type in Input < input.txt. Instead, you want to open a terminal program, change directory into the directory containing the project, and then run the program from the command line by writing Input < input.txt. This starts up the program and uses the contents of input.txt as the standard input stream, rather than reading text from the console.
That said, there are two bugs in your code. First, note that you have the line
c = getchar();
outside of your loop that does the input reading. You then immediately call
c = getchar();
again inside the loop. This means that you are discarding the very first character that you read in.
Second, your loop runs one more time than it needs to. If the second call to getchar() returns EOF, you do not detect this until after the current loop iteration finishes. This is because your check for EOF is at the top of the loop, which isn't reached until after you've already printed out the EOF character. To fix this, consider using the loop-and-a-half idiom and breaking out of the loop in the middle:
for (;;) {
/* Read data. */
if (/* no data left */) break;
/* Process data. */
}
Stylistically, are you sure that you need both the if and else branches here? Recall that tolower will not change the values of characters that aren't upper-case, so having one case for upper-case letters and one for lower-case is redundant. You can just output the character produced by tolower without the special cases.
Hope this helps!
The idiomatic way of doing this for ASCII text only is:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int c;
while((c = getchar()) != EOF)
putchar(tolower(c));
exit(EXIT_SUCCESS);
}
As textbooks are so fond of saying, UTF-8 is left as an easy exercise to the reader ;-)
I don't really know about C. So, my questions and ideas may be misleading or ridiculous or embarrassing. Please forgive me..
The question states that the program should get the user input from the command prompt. the input text will have numbers, alphabets and space characters. they will have multiple lines too..
The example program is using <stdio.h> and hence printf and scanf.
Using cin and cout is not encouraged according to the textbook. In order to use cin and cout, I need <iostream> but I can't include <iostream>. Even if I could include <iostream>, but I then can't include <stdio.h> anymore.
I also can't use string. I've heard that C doesn't allow the use of string, but I don't really understand why.
My question is; when you ask users to type in something with printf("Enter your text: ");, they can only type in one line.. once they hit Enter, the program receive it.
Is there anyway to use scanf to allow users to type in multiple lines by typing Shift+Enter for example.
As string is not available, should I use a char array like char inputText[999]? Will this give me enough space?
My two questions are; How to allow users to type multiple lines in command prompt, and what data type should I use to save the entered text?
I googled it but they mix C++ and C..
Thanks to all and once again, i apologize if my question is a waste of time for you..
Hi all, I have done it.. but I only have one more problem..
#include <stdio.h>
#include <stdlib.h>
int nc=0,nw=0,bs=0,c, nq=0, nl=0;
int main()
{
printf("Text Analysis Program\n\n");
printf("Enter your text:");
while((c=getchar())!=EOF)
{
if( (c>='A' && c<='Z') || (c>='a' && c<='z') )
{
nc++;
}
else if( c>='0' && c<='9' )
{
nq++;
}
else if (c==' ')
{
nw++;
bs++;
}
else if (c=='\n')
{
nw++;
nl++;
}
}
printf("Number of characters: %d;\n",nc);
printf("Number of words: %d;\n",nw);
printf("Number of numerical quantities: %d;\n",nq);
printf("Number of blank spaces: %d;\n",bs);
printf("Number of lines entered: %d;\n",nl);
exit(0);
}
At the end, after the answers are shown, cmd screen closes straight away.. I removed exit(0) it is still the same. Is there any commands to freeze the screen and appears Press any key to continue
These:
#include <iostream>
#include <string>
cin
cout
are C++-specific. They simply don't exist in C. Remember that C and C++ are two different languages.
The fact that your window is closing when the program terminates is a function of the development system you're using. If you run the program from a command prompt, that won't happen -- or there may be an option in your IDE to tell it to keep the window open until you close it explicitly. But if you don't want to do that, J.F. Sebastian's solution is a good one.
Another hint: take a look at the isalpha() and isdigit() functions in <ctype.h>.
Just add another getchar() call:
#include <stdio.h>
int main() {
printf("Press any key to continue. ");
fflush(stdout);
getchar();
return 0;
}
It actually doesn't return until you type a full line or it hits EOF but it should be good enough.