Newbie problem

This is a discussion on Newbie problem within the C Programming forums, part of the General Programming Boards category; Hello, i''ve just started to learn C this is my code: Code: #define MAX 100 char **text ; int lines; ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    5

    Newbie problem

    Hello, i''ve just started to learn C

    this is my code:
    Code:
    #define MAX 100
    
    char **text ;
     
    int lines;
    
       
     
       printf("number of lines?\n");
       scanf("%d",&lines);
    
       text = (char**)malloc(linhas*sizeof(char*));
       int i=0;
    
       do{
           text[i] = (char*)malloc(MAX*sizeof(char));
           printf("%d) ",i+1);
            scanf("%(%d)[^\n]",MAX,text[i]);
           i++;
       }while(i<lines);
    Thats the problem:
    after the first loop iteration, it jumps to the end of loop. what am i doing wrong?

    i'm using:
    MinGW
    Code::Blocks
    Last edited by brunovl83; 10-29-2009 at 02:00 PM.

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Bad copy and paste here!


    Real quick..where are your ';' to terminate the commands? Also there is no need for &word, just use the variable name as this is a pointer.

    Some spelling problems seen as well.

    Check your return value for your malloc call.

    Do not forget to call free().
    Last edited by slingerland3g; 10-29-2009 at 01:52 PM.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    Quote Originally Posted by slingerland3g View Post
    Bad copy and paste here!


    Real quick..where are your ';' to terminate the commands? Also there is no need for &word, just use the variable name as this is a pointer.

    Some spelling problems seen as well.

    Check your return value for your malloc call.

    Do not forget to call free().
    sorry, i fixed ;P

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    This is puzzling. What is your intent here

    Code:
    scanf("%(%d)[^\n]",MAX,text[i]);
    Should you be using...
    Code:
    scanf("%s",text[i]);

    The reason I say, that is because MAX is a constant and can not be assigned to.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    i need to catch a string with spaces.

    i tried
    scanf("%100[\n]",texto[i]);
    scanf("%100[]",texto[i]);
    scanf("%100[^\n]",texto[i]);
    scanf("%s100[^\n]",texto[i]);

    with no success

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    scanf("%100[^\n]",texto[i]);

    will work, but to make it dynamic you'll have to form the string FIRST: sprintf(control, "%%%i[^\n]", MAX);
    Then you can scanf(control, texto[i]);

    Probably, however, you should use fgets().

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    the better succes until now is when i used:
    Code:
       do{
            texto[i] = (char*)malloc(MAX*sizeof(char));
            printf("%d) ",i+1);
            scanf("%*[^\n]",texto[i]);  // reads all caracter until '\n' appears.
            scanf("%*c");                     // reads '\n'
            i++;
        }while(i<linhas);
    it skips the first input, although all other lines recieve it's inputs,

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    That should do the trick. Please keep in mind the dangers of using scanf! If your array is not large enough, scanf will write past it with no problem or a care in the world.
    Code:
    scanf("%[^\n]",text[i]);

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    That should do the trick. Please keep in mind the dangers of using scanf! If your array is not large enough, scanf will write past it with no problem or a care in the world.
    Code:
    scanf("%[^\n]",text[i]);

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    I did it!
    Code:
        do{
            texto[i] = (char*)malloc(MAX*sizeof(char));
            printf("%d) ",i+1);
            scanf("%*c");                
            scanf("%100[^\n]",texto[i]);   
            scanf("%*c");                 
            i++;
        }while(i<linhas);
    thank u all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 11:47 PM
  2. Newbie problem with scanf function...
    By Mithal in forum C Programming
    Replies: 1
    Last Post: 11-13-2005, 10:28 PM
  3. Newbie ... looping problem
    By StupidIntel in forum C++ Programming
    Replies: 12
    Last Post: 05-13-2004, 07:45 PM
  4. Problem with code (newbie question)
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-31-2002, 02:39 AM
  5. newbie coding problem
    By rippascal in forum C++ Programming
    Replies: 10
    Last Post: 01-08-2002, 11:45 PM

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