Thread: text field

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    12

    Question text field

    hi, i'm new to this forum

    i have been learning c programmin for a bit now, I have this problem, i want to have make something like a message box which allows you to enter 25 line MAX. IF you wish to stop, double enter to quit.

    The only problem is that the text for the message field is not aligned ( to get a clear picture of what i m saying, please run this code in your complier. I use Turbo C 3.0)

    I want to align all the text that you enter for the next line start from the same location right under from the first line.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAXLINES 25
    
    main()
    {
    clrscr();
    char name[15];
    char subject[15];
    char msg[100];
    
    textcolor(7);gotoxy(3,5);
    cprintf("Name:");
    gets(name);
    textcolor(7);gotoxy(3,7);
    cprintf("Subject:");
    gets(subject);
    textcolor(7);gotoxy(3,9);
    cprintf("Message:");
    gets(msg);
    
    
    int get_lines(char *lines[]);
    
     char *lines[MAXLINES];
    
         int number_of_lines;
    
         /* Read in the lines from the keyboard. */
    
         number_of_lines = get_lines(lines);
    
         if ( number_of_lines < 0 )
         {
    	 puts(" Memory allocation error");
    	 exit(-1);
        }
    
         return(0);
     }
    
     int get_lines(char *lines[])
     {
         int n = 0;
         char buffer[80];  /* Temporary storage for each line. */
    
         gotoxy(3,9);cprintf("Message:");
    
         while ((n < MAXLINES) && (gets(buffer) != 0) &&
    	    (buffer[0] != '\0'))
         {
    	 if ((lines[n] = (char *)malloc(strlen(buffer)+1)) == NULL)
    	     return -1;
    	 strcpy( lines[n++], buffer );
         }
         return n;
    
    
    }
    i thought of using a for loop along with gotoxy();
    but not sure how?
    i would like a little help...
    thanks..
    amaR

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Just a tip: Help will come a lot faster around here if people don't have to execute your code to see what your problem is.

    Some of the functions you use may not be available in all compilers. So while it's good that you mention what compiler you're using, you limit the number of people that can answer your question.

    Try to give a short example of how the output is now and how you'd like it to be. (Code tags may help in preserving the formatting so people can see clearly what you mean).
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    12
    ok.. thx for tip.

    CURRENT OUTPUT
    Code:
    Name:*input name*
    
    Subject:*input subject*
    
    Message:*input message -- first line
    message --second line
    message -- third line*
    EXPECTED OUTPUT
    Code:
    Name:*input name*
    
    Subject:*input subject*
    
    Message:*input message -- first line
                     message --second line
                     message -- third line*
    do u notice the difference...
    the expected output has the message text left aligned.
    but the other does not.

    does this make things clear?
    thanks again!!

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Just figure out how many spaces you need to output to align the text and use a loop as you surmised. Or if you mean you want to indent the length of "Message: " simply output the correct number of spaces in another cprintf();

    And don't worry about
    Some of the functions you use may not be available in all compilers. So while it's good that you mention what compiler you're using, you limit the number of people that can answer your question.
    You certainly don't want to hear about ncurses when using borland, and hear that getch() is not portable. You DO get the best help when you mention the compiler.

    Sorry, Azuth but your statement seems to imply that mentioning the compiler is not a good thing. I don't think that's what you meant, but it is worded that way.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Rereading it I can see how it might be taken that way, and you're right, it's not what I meant, I'll be more careful. It was non-portable functions that may require a certain compiler or library that limited the number of potential helpers, not telling us what compiler is in use, that is indeed a very good thing.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    12
    hey WaltP, the suggestion u made is nice, but can't it only be used if u read it off the file??

    But i m not doing that, this program is what a user enters by
    gets(msg); the thing i was to point is that when u press [ENTER] to go to the second line the the cursor starts from the same place.
    And was wondering if i could control the postion

    (maybe by a combination of a for loop and gotoxy(); function)


    maybe i didn't explain in a clear way ....

    this is u help u know what i mean.....

    current output

    expected output
    Last edited by M_amaR; 10-23-2003 at 04:54 AM.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    I've done something similar to this before, but using curses on Unix, so I can't help specifically with the syntax. However, what I suggest is to get the user input one character at a time, instead of one line at a time (getch instead of gets?), and then when they input a newline character, swap it for a space in your buffer string, and use gotoxy to get the cursor in the right place (you know how far in, and you could just use a int variable to control how far down...). You'd also need something to check for two newline characters in a row, which would indicate the need to end the subroutine...
    There is no such thing as a humble opinion

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by M_amaR
    hey WaltP, the suggestion u made is nice, but can't it only be used if u read it off the file??

    But i m not doing that, this program is what a user enters by
    gets(msg); the thing i was to point is that when u press [ENTER] to go to the second line the the cursor starts from the same place.
    And was wondering if i could control the postion

    (maybe by a combination of a for loop and gotoxy(); function)


    maybe i didn't explain in a clear way ....

    this is u help u know what i mean.....

    current output

    expected output
    No, it doesn't require you reading from a file. You are doing screen manipulation, you just have to position the cursor where you need it. These questions should help you come up with the answer (I hope).

    Question 1: You want the 2nd line someone types to start at the same column as the input started for the first line?
    In other words:
    Code:
        Message: user input line 1
                 user inputs line 2, starts where this line starts
    This is what I interpret your problem is. I believe you're answer is "yes". If I'm mistaken, stop reading now and restate the problem.

    Question 2: What is it that makes the starting point not the left edge of the screen? Isn't it the word "Message"?
    Again I will assume the answer is yes.

    Question 3: What can you possibly do to move the cursor from the left edge of the screen to the position you desire? This you can figure out easily by thinking about it. I can think of 2 very easy answers -- pick one or come up with your own.

    Hope that helps... Awaiting your next post

    Oh, and by the way, gets() is a dangerous function to use. Convert each to fgets() for safety. This will not change your logic in any way, just helps the program not explode when you demo it for the instructor.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    12
    hey i start to get u're point and yes u r right bout what i want,

    my friend told me bout gets() being not safe, but i still use it coz i m not that experienced in C.

    i'll try fgets()....

    but still dun know bout the cursor postioning apart for gotoxy()
    maybe this code is beyond my understand at this moment??

    but thx anyways !!

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by M_amaR

    but still dun know bout the cursor postioning apart for gotoxy()
    maybe this code is beyond my understand at this moment??

    but thx anyways !!
    I don't think it's beyond you. You got this far without help, you just haven't gone the last little bit.

    What happens when you execute this code
    Code:
    gotoxy(3,9);  
    cprintf("Message:");
    Where is the cursor after the gotoxy()? Where is the cursor after cprintf()?

    Now, what happens when you output, say, a space? Where is the cursor before and after you execute a cprint(" ");

    NOW think about the cprint() above....

    -----------------------------

    Are you trying to input all the message lines using one gets()? If so, think about doing one line at a time and I'll bet the light will come on....
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    12
    opps... i noticed that the code i posted first was a bit wrong ...

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAXLINES 25
    
    main()
    {
    clrscr();
    char name[15];
    char subject[15];
    char msg[100];
    
    textcolor(7);gotoxy(3,5);
    cprintf("Name:");
    gets(name);
    textcolor(7);gotoxy(3,7);
    cprintf("Subject:");
    gets(subject);
    
    
    int get_lines(char *lines[]);
    
     char *lines[MAXLINES];
    
         int number_of_lines;
    
         /* Read in the lines from the keyboard. */
    
         number_of_lines = get_lines(lines);
    
         if ( number_of_lines < 0 )
         {
    	 puts(" Memory allocation error");
    	 exit(-1);
        }
    
         return(0);
     }
    
     int get_lines(char *lines[])
     {
         int n = 0;
         char buffer[80];  /* Temporary storage for each line. */
    
         gotoxy(3,9);cprintf("Message:");
    
         while ((n < MAXLINES) && (gets(buffer) != 0) &&
    	    (buffer[0] != '\0'))
         {
    	 if ((lines[n] = (char *)malloc(strlen(buffer)+1)) == NULL)
    	     return -1;
    	 strcpy( lines[n++], buffer );
         }
         return n;
    
    
    }
    this part was not intented for the code
    Code:
     
    textcolor(7);gotoxy(3,9);
    cprintf("Message:");
    gets(msg);
    Are you trying to input all the message lines using one gets()? If so, think about doing one line at a time and I'll bet the light will come on....
    hm... one line at a time ?? seems like a way out..
    let me see if i get this clear....

    i use one gets() per line, but when the user double enters the message is done !!

    hm.... seem easy !! but m i on the right track ??
    thanks for the explaination WaltP....

    i'll work on it and display what i come out with in a bit...

  12. #12
    Registered User
    Join Date
    Oct 2003
    Posts
    12
    wait ...

    for more than one gets()

    won't i need to declare more the one variable ?

    if so, is that effective..

  13. #13
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by M_amaR
    wait ...

    for more than one gets()

    won't i need to declare more the one variable ?

    if so, is that effective..
    Not necessarily. If you are done with the buffer from the first fgets(), you can use the same buffer for the next.

    (it's in blue to remind you to consider switching from the dangerous and evil gets() to the safe and nice fgets())
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. Input a Hex number and output hex number to a text field
    By zoobaby in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2009, 11:26 AM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  5. Replies: 1
    Last Post: 07-13-2002, 05:45 PM