Thread: looping problem?

  1. #1
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35

    looping problem?

    hi, everyone!

    please take a look at my program:
    Code:
    int main()
    {
       while(1)
       {
         printf("\n>> ");
         gets(cmd_str);
    
         split_cmd();
    
         if( strcmp(cmd_wrds[0],"MAKE") == 0 ) make();
    	 
       }
        return 0;
    }
    
    int make(void)
    {
        char matrix_name;
        matrix_name=cmd_wrds[1][0];
        if (matrix_name == 'A') v=0;
        else if (matrix_name == 'B') v=1;
        else if (matrix_name == 'C') v=2;
    
            printf("Enter number of rows: ");
            scanf("%d",&row);
            printf("Enter number of columns: ");
            scanf("%d",&col);
    
    	for (i=0;i<row;i++){
          for (j=0;j<col;j++){
               printf("%c[%d][%d] = ",matrix_name,i,j);
               scanf("%d",&m[v][i][j]);      }}  
        return 0;    
    }
    it does fine on first try but after the first looping of the code it does something unnecessary and i don't know why it does that.
    it's like this:
    Code:
    >>MAKE A
    Enter number of rows: 2
    Enter number of columns: 2
    A[0][0] = 1
    A[0][1] = 2
    A[1][0] = 3
    A[1][1] = 4
    
    >> //this is what i'm talking about, it should only show once before asking for another command
    >>MAKE B
    what seems to be the problem?
    by the way, the program still works fine after the first loop, i think, it's just that ">>" that's bothering me. where did it came from?
    thanks to those who'll help!
    Last edited by nyekknyakk; 08-20-2010 at 05:15 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    a) you're using gets() - very bad, read the FAQ
    b) you're mixing scanf() with other input methods - not so good
    c) lots of global variables - try using parameters.

    > that's bothering me. where did it came from?
    It's the \n left behind by the previous call to scanf

    Using fgets() to read ALL input into a buffer, then using your parser (or sscanf or whatever) when it's in memory is a lot saner in the long run.
    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.

  3. #3
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35
    Quote Originally Posted by Salem View Post
    > that's bothering me. where did it came from?
    It's the \n left behind by the previous call to scanf
    what do you mean by that? which part did it went wrong?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    you call make()
    make calls scanf()
    You type in

    1\n
    2\n
    3\n
    4
    \n

    scanf happily skips newlines by itself, but when you switch back to some other input method then surprises appear.
    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.

  5. #5
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35
    Quote Originally Posted by Salem View Post
    you call make()
    make calls scanf()
    You type in

    1\n
    2\n
    3\n
    4
    \n

    scanf happily skips newlines by itself, but when you switch back to some other input method then surprises appear.
    oh, that's where it came from. but how can i fix it?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    Use fgets() + sscanf() for ALL input.
    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.

  7. #7
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35
    Quote Originally Posted by Salem View Post
    Use fgets() + sscanf() for ALL input.
    How does it differ from gets and scanf?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    gets is dangerous, read SourceForge.net: Gets - cpwiki.
    scanf, as mentioned, will read your integral data and leave the newline. See SourceForge.net: Scanf woes - cpwiki.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35
    but does it still works the same?
    and has the same syntax?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For gets, works the same, yes, same syntax, no. Look up fgets in your favorite documentation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looping problem
    By main() in forum C Programming
    Replies: 4
    Last Post: 05-27-2010, 02:28 AM
  2. endless looping problem
    By SoulMagician in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2009, 01:21 AM
  3. Getting illegal case error
    By scmurphy64 in forum C Programming
    Replies: 2
    Last Post: 09-17-2009, 10:35 AM
  4. looping problem
    By chris285 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 11:03 AM
  5. Looping problem
    By sketchit in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 02:19 PM