How do I split a console into two parts? I'm trying to build a simple chat in C and I'd like to have the output on the top of the console and the input on one line at the bottom. I would assign the output handling to one thread and the input handling to another thread. For example:
Connected to server!
User1: Hello!
User2: How are you?
Write something here: simple TCP chat
User1: Hello!
User2: How are you?
You: simple TCP chat
Write something here:
Is it even possible to achieve something like this in plain C?
Thank you in advance!
PS: This question has already been asked some time ago, but the code is in C#, not C.
Related
Hey everyone I'm looking for a way to code a program/bot in VB that would read my chat, but only if a command is used
For example: "!tts Hello"
And It would read it using TTS and show it on screen
$USERNAME$ !tts: Hello
Now the thing I don't know how to set up is the program reading the chat, printing it on screen I believe is the easy part.
Also if it's possible to add an option to select to which audio output it will go.
You need to study more coding...
Im not a master at it myself. but i would make a boolean that could be true if the first letter in a chatline is a "!". And then a command that would add that line to a text-to-speech module
I want to write to the terminal at the same time the user is inputting a command for my program. For example, when the user is trying to print 'help', it could end up looking like this:
heNew packet with length 233
lp
I'd like to be able to have command input just below where everything is outputted, so it would look something like this:
New packet with length 223
Sending x to y..
...
>help
I want to avoid using a library for this. The program is not cross-platform so a linux-only solution would work.
This will require that you manage the raw input and output to the screen. You say that you don't want to use a library but I would strongly encourage you to have a look at ncurses. If you really want to build this from scratch you will be reinventing lots of wheels. :)
Here is a part of my current (working) chatbot code in Notepad so you can get an idea of how I am coding it:
dim n, q
n=inputbox("Hello, I'm a chatbot. What is your name?")
q=inputbox(""& n &" is a great name! Thank you for activating me, "& n &"! How are you?")
I want to be able to make "if and else" parts (for example, if "happy" is typed in after being asked "How are you feeling" it should say "great", if you type anything else it would say something differently.) How do I do this in Notepad?
Also, how do I get it to loop and say something based on keywords (like how Eliza the chatbot will keep talking for infinity just by looping and referring to a bank of keywords and responses) in Notepad?
I know these are really basic questions, but I have have searched the web multiple times; all searches ending up empty handed.
Unfortunately, you asked a very inspecific question making it hard to answer. That is why your post received a downvote and a close down request. However, you seem enthousiastic and I can only encourage that.
First of all, VBScript is not a very nice language. Oh, yes it seems simple, it nicely integrates with some standard windows components and you can run it out of the box. But it is ancient and does some things differently than more common languages. If you want to do more complex things faster, ultimately you'd switch to another (scripting) language.
With VBScript it is not (easily) possible to use notepad as input/output device. For your setup I'd prefer the command line (press your windows key and enter CMD+enter, there it is).
You can write output to the command line with
WScript.StdOut.Write "What is your name?"
You can retrieve output from the command line with
wscript.stdin.read(0)
name = wscript.stdIn.readline()
And to make your chatbot complete:
WScript.StdOut.Write "Hello " & name
Now you want to do some conditional branching. Well, I do not say W3Schools is a very good resource, but for now it is sufficient. Look at their if ... then ... else page (<- clickable link). If you are using IE, you can even try some things yourself.
Finally you can use Do While ... Loop's to make the code repeat itself.
A chatbot like Eliza is a prestigeous project. Don't be discouraged if you only can build a very simple version. The cake is in the coding, not the result.
For my situation, say Tim and Bob are chatting using my C chat client and server. My chat client and server is executed on a bash terminal. The cursor for this chat program is the ~ key.
Tim sends Bob a message that says, "Hey". Now say that Bob receives this message as he's typing a message to Tim. Bob wants to send "Hello" to Tim, but he hasn't finished writing it yet (so he's only written "Hel" so far).
I would like my application to be able to keep Bob's prompt and his entry so far at the bottom of the terminal, and display Tim's message above the prompt. Bob should still be able to finish his message to Tim, as well as being able to see Tim's message. Below is a diagram of what I mean, from the view of Bob's client. Is there any way of accomplishing something like this using C?
........................Before............................................................................................After...............................
............................... .................................
............................... <Tim> Hey
Enter Message> Hel~ Enter Message> Hel~
If you know that you're using an ANSI compatible terminal AND know your prompt length AND cursor position (assuming you allow editing of input so that the cursor may not be at the end of the current input) AND don't allow more than one line of input [ a lot of assumptions there ] then it would seem you can output one or more 'cursor up' sequences then a carriage return, your message from Tim, a carriage return, a linefeed and then lots of 'cursor right' sequences to put the cursor back where it was.
Basically, though, if you want to handle this sort of complication then you're going to head to a curses/ncurses/terminfo kind of library.
I have the following simulation in C for cars in a traffic circle
alt text http://dl.getdropbox.com/u/175564/cars.png
For example,
7 <space> <enter>
gives more cars to the simulation, while
s1 <enter>
puts a top sign to the incoming road #1.
I want to put the simulation to an internet page such that users can try it.
However, I do not know where I should start.
I know Joomla and little about Django. Perhaps, they can be useful.
How can you put a C program to a Webpage such that users can use it?
CGI is what you want; it will let you embed any program you want into a website, it was made for that purpose. Then perhaps embedding a few more options with PHP and HTML will let the user acutally input data into the program via the web. It should not be too hard.
Take a look here for more info: http://www.cs.tut.fi/~jkorpela/perl/cgi.html
I think that is a good pointer in the right direction. I hope that helps.
You could run the C code in a flash app using alchemy:
http://labs.adobe.com/technologies/alchemy/
Here's a good intro to writing CGIs in C: http://www.cs.tut.fi/~jkorpela/forms/cgic.html
However, since you're a beginner, I'd recommend porting your program to PHP. It's a very easy language to pick up, and it's a much easier route than writing a CGI in C.
I assume the C program needs input while it's running and not via command-line arguments? If I'm wrong, you can just use PHP and shell_exec() to run the program. The function returns anything printed to stdout.
Such a page might look like:
$sim = shell_exec("/path/to/binary -a 5 -b 6");
echo $sim;
Where the string passed to shell_exec is exactly what you'd type on the command line.
If you want a dynamic simulation, where the cars moves while you watch , you'll need an applet or flash.
A cgi program renders the page on each http GET/POST (on reload, submit etc)
and that is probably not what you want.
I would start with a flash or java applet wrapper. You can communicate with your application over a tcp connection and display the results in the flash or applet.
I would recommend going with sockets. If your C program could set up and listen on a local or internet socket, you could use the socket facilities in any language to send it arguments and get output.
If that's going to be too much of a pain, have php exec the program while directing the output to some file. Then, have php read that file.
Looking at the output of your program, I think trying to print the results of shell_exec() will result in clobbered output.
So, you could shell_exec("/bin/program -arguments > /tmp/prog-tmp.txt") , then read prog-tmp.txt.