Quick Question about Strings

This is a discussion on Quick Question about Strings within the C Programming forums, part of the General Programming Boards category; My program needs to have at least 15instructions with at most 70 string length. It should be declared inside a ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    12

    Quick Question about Strings

    My program needs to have at least 15instructions with at most 70 string length. It should be declared inside a structure,

    Code:
    typedef char string70[71];
    
    typedef struct{
    string70 instruct[15];
    }Recipe;
    ....
    int main(){
    Recipe x;
    .....
    
    fgets(x.instruct[1],sizeof(string70),stdin); /*Takes an inputed instruction*/
    x.instruct[1][strlen(x.instruct[1])-1]= '\0'; /*Removes the extra enter key */
    Is that the right code or.. Thank you )
    Last edited by givmefive5; 10-16-2011 at 07:24 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your typedef doesn't have a type. You need to define like so:
    Code:
    typedef typeofthing newnameofthing;
    Your 'string70' doesn't have a type.


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

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    12
    Edited that part, thanks

    Is my program logically correct, the accessing of the last part to be null byte. thanks. )

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, but if you put it with an if statement, you avoid any problem with a long line that gets the newline truncated.
    Code:
    len = strlen(array[row]);
    if(array[len-1]=='\n')
       array[len-1]='\0';

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    Yes, but if you put it with an if statement, you avoid any problem with a long line that gets the newline truncated.
    Code:
    len = strlen(array[row]);
    if(array[len-1]=='\n')
       array[len-1]='\0';
    Not to nit pick too badly but shouldn't that be ... array[row][len-1] = 0 ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    Quote Originally Posted by givmefive5
    My program needs to have at least 15instructions with at most 70 string length.
    Code:
    typedef char string70[71];
    You fixed the syntax error that quzah pointed out, but there's a little more that you can do: give your type a better name. For example:
    Code:
    typedef char instruction_type[71];
    
    /* ... */
    
    instruction_type instructions[15];
    This way, if your requirements change such that an instruction can have a max length of 80 instead, you would not end up either having to do some renaming, or having to live with a type named string70 that really means string80.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    12
    edited everything you guys said to my program, thank you )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick sort arrays of strings
    By nik2 in forum C Programming
    Replies: 5
    Last Post: 04-25-2010, 02:01 PM
  2. Quick check on reading in + using strings
    By advancedk in forum C Programming
    Replies: 2
    Last Post: 12-08-2008, 09:12 PM
  3. Quick question on malloc and strings
    By officedog in forum C Programming
    Replies: 20
    Last Post: 11-06-2008, 04:14 PM
  4. Quick question: Ifstream and Strings
    By Junior89 in forum C++ Programming
    Replies: 11
    Last Post: 12-22-2004, 02:22 AM
  5. Quick question about strings
    By Blak_Lightnin in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2003, 06:47 PM

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