accepting input while writing to screen

This is a discussion on accepting input while writing to screen within the C Programming forums, part of the General Programming Boards category; i am still doing basic programs just to see what i can do, and i wanted to write a small ...

  1. #1
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49

    accepting input while writing to screen

    i am still doing basic programs just to see what i can do, and i wanted to write a small little chat program. how would i accept the user typing in a message, and still let the server send out messages from other users?

    i only know scanf, and scanf pauses the program from running untill the user enters something. i need the messages to be updated while the user is typing, or else it isnt much of an instand messenger.

    thanks
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."

    "They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."

    "Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
    [all]- Terry Pratchett

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Look in to threading. It let's you have two parts of your program running simulataneously. Of course, I wouldn't recommend this if you still consider yourself a newbie to the language,

  3. #3
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    ill start reading up on threading, but it seems like it is alot of work for just a stupid little chating program.
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."

    "They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."

    "Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
    [all]- Terry Pratchett

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    you can try something like..

    Code:
     char c[256];
     int i=0;
     while((c[i]=getchar())!='\n')
      i++;
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  5. #5
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    would \n be the user hitting enter then?
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."

    "They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."

    "Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
    [all]- Terry Pratchett

  6. #6
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    Quote Originally Posted by variable
    would \n be the user hitting enter then?
    yes, thats the user pressing enter. also, be sure to check whether the array is completely filled. you could also use dynamic memory allocation if you dont want to put a maximum length limit.
    Last edited by PING; 02-04-2005 at 10:59 PM.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    PING, the input function is blocking - you can't do anything until the input function returns, and it won't return unless the user presses enter. This doesn't do the job for a chat program.

  8. #8
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    aye, true, it would have to be threaded then eh?
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."

    "They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."

    "Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
    [all]- Terry Pratchett

  9. #9
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    the input function?? which one is that ??
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  10. #10
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    i think he means getchar()
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."

    "They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."

    "Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
    [all]- Terry Pratchett

  11. #11
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Code:
    while((c[i]=getchar())!='\n')
      i++;
    Nothing else in your program is going to happen while this loop is being implemented. That is what sean is talking about when he says your input function is blocking.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  12. #12
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    of course, for that he will look into threading..cant deny that
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  13. #13
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    yep, well it looks like im off to read up on some multithreading.
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."

    "They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."

    "Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
    [all]- Terry Pratchett

  14. #14
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    anyone know if allegro and wsock32 can work together?

    this code will not work
    Code:
    #include <allegro.h>
    #include <windows.h>
    #include <winsock.h>
    #include <stdio.h>
    
    int main (void) 
    {
        WORD sockVersion;
    	WSADATA wsaData;
    	int nret;
    	char buffer[256];
    	
         allegro_init();
         install_keyboard();
    	
    	sockVersion = MAKEWORD(1, 1);
    	
    	WSAStartup(sockVersion, &wsaData);
    	
    	SOCKET mysocketa1;
    	
    	mysocketa1 = socket( AF_INET , SOCK_STREAM , IPPROTO_TCP );
    	
    	SOCKADDR_IN serverInfo;
    
    	serverInfo.sin_family = AF_INET;
    	serverInfo.sin_port = htons(8888);
    	serverInfo.sin_addr.s_addr = inet_addr("10.1.50.30");
    	
    	ZeroMemory(buffer, 256);
    	
    	scanf("%s", &buffer[0]);
    	scanf("%s", &buffer[1]);
    	printf("%s",buffer);
    	
    	connect(mysocketa1, (LPSOCKADDR)&serverInfo , sizeof(struct sockaddr));
        
        send( mysocketa1 , buffer , strlen(buffer) , 0 );	
    	
    	
    	closesocket(mysocketa1);
    	
    	WSACleanup();
    	getch();
    	
    	allegro_exit();
    	
    	return 0;
    	
    }
    END_OF_MAIN();
    it only gives errors when i use allegro with it.

    wanted to do this so that i could use the nonblocking functions in allegro.
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."

    "They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."

    "Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
    [all]- Terry Pratchett

  15. #15
    Registered User
    Join Date
    May 2004
    Posts
    114
    I don't know if it would work or not using allgero and winsock, but you might want to try sdl and sdl_net combination instead if it wont.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 09:44 AM
  2. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  3. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 03:41 AM
  4. Writing input from a file into shared memory
    By RazielX in forum C Programming
    Replies: 2
    Last Post: 09-23-2004, 01:34 PM
  5. Getting input data fram screen with a function ?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-19-2002, 04:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21