Thread: chalanging programing to solve

  1. #1
    Registered User j4k50n's Avatar
    Join Date
    Apr 2007
    Posts
    14

    chalanging programing to solve

    ok i wanted to enter a surname that can only fits 20 character...
    but if some one enters more then 20 character, it would just read the first 20 character that is written, when it is printed out...but the character after the 20th one, will not be written anywhere and as a matter of fact, i don't want it to affect the rest of the program...

    this is what i have so far...
    Code:
    #include <stdio.h>
    
    int main(int argc, char ** argv)
    {
    // Mainline Variable Declarations
    FILE * output = stdout;
    FILE * input = stdin;
    
    char * LName[20];
    
    			{
    			//Enter last name
    			fprintf(output,"--------------------------\n"); fflush(output);
    			fprintf(output,"Enter Your last name(maximum of 20 letters): "); fflush(output);
    
    			//input last name
    			fscanf(input,"%20s%*c",&LName);fflush(output);
    			}
    //output
    
    	fprintf(output," %s",LName);

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    "LName" is an array of pointers to characters, not an array of characters. So that's all wrong. Other than that, you'll need 21 characters in your array, so that you can truncate the string with the null character. Finally, you'll have to decide what it is you want to do to handle extra input. Probably you can get away with just reading and discarding everything past the 20th character, until you hit a newline character.

    Is there any particular reason you're aliasing stdin and stdout?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User j4k50n's Avatar
    Join Date
    Apr 2007
    Posts
    14
    Quote Originally Posted by quzah View Post
    "
    Is there any particular reason you're aliasing stdin and stdout?
    Quzah.
    yeah there is a reason, its part of the thing i have to do...
    so yeahh...
    but i don't really understand what you mean..can you explain in more detail...
    thank you

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    You might want to look at C String tutorial first, then you'll know the difference between "char var[]", "char *var" and "char *var[]".

    There's also an example that uses fgets() to read a string of maximal size 256, you can easily modify it to read only 20 characters.

  5. #5
    Registered User j4k50n's Avatar
    Join Date
    Apr 2007
    Posts
    14
    Quote Originally Posted by KONI View Post
    You might want to look at C String tutorial first, then you'll know the difference between "char var[]", "char *var" and "char *var[]".

    There's also an example that uses fgets() to read a string of maximal size 256, you can easily modify it to read only 20 characters.
    i know the difference between "char var[]" "char *var" and "char *var[]"
    and have tried fgets, but it stills deosn't work, and plus i m not suppose to use that...

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by j4k50n View Post
    and have tried fgets, but it stills deosn't work, and plus i m not suppose to use that...
    How are we supposed to help you if you make us play 20 questions?

    fgets() would be the perfect way of doing it otherwise.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > i know the difference between "char var[]" "char *var" and "char *var[]"
    Doesn't look like it from where I'm sitting.

    > fscanf(input,"&#37;20s%*c",&LName);
    To make this work, you would need

    char LName[21];
    fscanf(input,"%20s%*c",LName);

    Which would read
    - upto (and including 20 character)
    - upto (and excluding) the first white-space character

    The %*c reads (and throws away) the next character (only).

    Now what happens next depends entirely on the format of your text file. Any random white space for example in the text file can seriously affect how robust your use of fscanf is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User j4k50n's Avatar
    Join Date
    Apr 2007
    Posts
    14
    Quote Originally Posted by Salem View Post
    >
    The %*c reads (and throws away) the next character (only).
    yeah i put that in because it also reads out the enter button when i press enter, so its suppose to read enter button and erase it..

  9. #9
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by j4k50n View Post
    ... its suppose to read enter button and erase it..
    get away from my keyboard !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help to solve My easy C programing questions!
    By hotwebs in forum C Programming
    Replies: 13
    Last Post: 12-25-2009, 03:55 AM
  2. C programing for DNA...
    By S16 in forum C Programming
    Replies: 9
    Last Post: 04-29-2009, 09:25 AM
  3. Human brain and programing
    By avgprogamerjoe in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 08-27-2007, 04:48 PM
  4. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  5. Help to solve this problem
    By Romashka in forum C++ Programming
    Replies: 3
    Last Post: 04-16-2002, 09:32 AM