Help with C-code

slimbizzy

call me watchu want
OP
Member
Joined
Apr 7, 2020
Messages
301
Trophies
1
Location
Calgary
Website
linktr.ee
XP
1,013
Country
Canada
Alright, so I don't actually know how to explain this the right way, but I will try my absolute hardest.

What I want to do, is make an executable file in C code, and have it so whatever I write in the RaspberryPi terminal, show up on my LCD screen I am exploring with. See now that's is the thing though, I cannot find out how or what to look up in Google because see I just don't know what I'm supposed to be looking for! I am trailing off anyways, let me see if I can explain better...

So, in theory; I run my script. The one I made...

I am greeted with something like this:

Code:
Please write something: {I write "hello" for example}

You wrote: hello {or whatever text I put in}

And it is that simple. Yeah I know I have no idea how to work that out please excuse me, I am new to C. But see I did manage to work out something sort of what I want, but not exactly.

Code:
#include <stdio.h>

int main()
{
	char ch;
	//infinite loop
	while(1)
	{
		printf("Enter any character: ");
		//read a single character
		ch=fgetc(stdin);
		
		if(ch==0x0A) //enter key
		{
			printf("ENTER KEY is pressed.\n");
			break;
		}
		else //if not enter key
		{
			printf("%c is pressed.\n",ch);
		}
		//read dummy character to clear
		//input buffer, which inserts after character input
		ch=getchar();
	}
	return 0;
}

So what it does is that it let's you write something in the terminal to detect if you pressed the ENTER key. but if you pressed anything else and hit enter it would tell you what you wrote. In this case it would be a key on your keyboard. If you were to write the letter "h", it would say "{letter} is pressed". But I cannot figure out how to get it to accept more than one character? To clear that up, this is what happens when I write more than one letter:

Code:
Enter any character: hh
h is pressed.

Yeah, so I don't want that! That isn't what I am trying to get... I am trying to have the script read... Lets say a sentence that I wrote, and write it back to me. But I don't know how! I would totally look it up on the internet if I knew what to ask exactly. So, could someone please help me out and guide me on what I need to do/write? Sorry I am such a noob lol but I gotta start somewhere :) Thanks in advance nerds.
 

JeffRuLz

Well-Known Member
Member
Joined
Sep 14, 2018
Messages
161
Trophies
0
XP
2,531
Country
United States
"char ch" means that the variable ch will only hold a single character (one 8-bit value.)
You will need a char array to hold a string (many characters, like a sentence.)

If you google "char array c" you should find what you're looking for.
 
  • Like
Reactions: slimbizzy

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    Psionic Roshambo @ Psionic Roshambo: @SylverReZ, Indeed lol