Thread: Very strange problem!

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    11

    Very strange problem!

    This reads a string and prints each character in a new line.

    Code:
    float rt, gt;
    int a;
          
    printf ("Input value rt:\n");
    scanf ("%f", &rt);
    printf ("Input value gt:\n");
    scanf ("%f",&gt);
    
    int i;
    char str1[50];
    printf ("Input string\n");
    gets(str1);
    i = 0;
    while (str1[i]!= '\0')
        {
              if (str1[i] == ' '){ i++;  continue;}
              
              if (str1[i]!= '+' || str1[i]!= '-' || str1[i]!= '*' || str1[i]!= '/')
              {
                   printf("\n%c", str1[i]);
               }
              else 
              {
                   printf("\n%c", str1[i]);
              } 
         
         i++;       
        }
    
    
    scanf("%d", &a);
    But like it is written above it doesn't work. I don't have a clue of what the problem is but the thing is that, without this part...

    Code:
    printf ("Input value rt:\n");
    scanf ("%f", &rt);
    printf ("Input value gt:\n");
    scanf ("%f",&gt);
    ...it runs great. Add this and it stops working.

    Any help, please?

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    You should describe how it "stops working"
    Does it give the wrong result? Does it crash?

    Also you really ought to consider rewriting your loop.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. You shouldn't be using gets at all.
    2. Even once you change gets to fgets, scanf and fgets don't play nice together. If you insist on mixing them, you will have to take care of the newline character that scanf leaves behind yourself. (There's an FAQ on this.)

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    This part does not make sense:
    Code:
    if (str1[i]!= '+' || str1[i]!= '-' || str1[i]!= '*' || str1[i]!= '/')
    The corollary is

    Code:
    if ( ! (str1[i] == '+' && str1[i] == '-' && str1[i] == '*' && str1[i] == '/'))
    ... which is clearly impossible to be met. In other words, your logic will always yield a TRUE result.
    Last edited by nonoob; 06-26-2009 at 11:28 AM.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    Quote Originally Posted by NeonBlack View Post
    You should describe how it "stops working"
    Does it give the wrong result? Does it crash?

    Also you really ought to consider rewriting your loop.

    It doesn't print the string. After I input the string and press enter the program abruptly ends. If scanf isn't present in the code, it works great and prints the string.

    1. You shouldn't be using gets at all.
    2. Even once you change gets to fgets, scanf and fgets don't play nice together. If you insist on mixing them, you will have to take care of the newline character that scanf leaves behind yourself. (There's an FAQ on this.)
    I have used fgets and it does exactly the same thing. What should I use to make it work?

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    You could try to use the idea about using fgets right here

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    Quote Originally Posted by nonoob View Post
    This part does not make sense:
    Code:
    if (str1[i]!= '+' || str1[i]!= '-' || str1[i]!= '*' || str1[i]!= '/')
    The corollary is

    Code:
    if ( ! (str1[i] == '+' && str1[i] == '-' && str1[i] == '*' && str1[i] == '/'))
    ... which is clearly impossible to be met.
    In fact, that part of the code works fine, although I'm going to rewrite it later. The thing between the scanf and fgets or gets is the problem.

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    Quote Originally Posted by $l4xklynx View Post
    You could try to use the idea about using fgets right here
    Tried it that way, unfortunately it doesn't work. Does the same thing, doesn't print the string.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Or you could just use isalpha() to capture all alpha characters from the input and disregard those non-alpha characters.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by no_one_knows View Post
    Tried it that way, unfortunately it doesn't work. Does the same thing, doesn't print the string.
    Were you able to enter a string using gets()? if not, then the string will not be printed. Prior to input string using gets you have 2 scanf()'s, I presume, that you won't be able to input any value to gets() because of the excess buffer from scanf(). So it would be good to use fgets for all user inputs and convert those chars to float using atof().

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by no_one_knows View Post
    In fact, that part of the code works fine, although I'm going to rewrite it later. The thing between the scanf and fgets or gets is the problem.
    No it doesn't. Likely it's not what's causing your immediate problem but I'm sure it will not give you the correct result when you resolved your first problem.

  12. #12
    Registered User
    Join Date
    Jun 2008
    Posts
    11
    Quote Originally Posted by $l4xklynx View Post
    Or you could just use isalpha() to capture all alpha characters from the input and disregard those non-alpha characters.
    That is a very nice function, I've replaced the second condition of the while for that.

    It doesn't solve the problem, though. It seems that the while loop isn't even run if the scanf is used anywhere in the function.

    The current code is this:

    Code:
    float rt, gt;
    int a;
          
    printf ("Input value rt:\n");
    scanf ("%f", &rt);
    printf ("Input value gt:\n");
    scanf ("%f",&gt);
    
    int i;
    char str1[50];
    printf ("Input string\n");
    fgets(str1, 50, stdin);
    i = 0;
    while (str1[i]!= '\0')
        {
              if (str1[i] == ' '){ i++;  continue;}
              
              if (isalpha(str1[i]))
              {
                   printf("\n%c", str1[i]);
               }
              else 
              {
                   printf("\n%c", str1[i]);
              } 
         
         i++;       
        }
    
    
    scanf("%d", &a);
    And, unfortunately, it still doesn't work.
    Last edited by no_one_knows; 06-26-2009 at 11:51 AM.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by tabstop View Post
    2. Even once you change gets to fgets, scanf and fgets don't play nice together. If you insist on mixing them, you will have to take care of the newline character that scanf leaves behind yourself. (There's an FAQ on this.)
    I will repeat tabstop's comment here, as it provides you the references to understanding your problem.

  14. #14
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Are you using a microsoft compiler?

    If so, check PRB: "Floating-point Support Not Loaded" Error with scanf()
    Try initializing rt and gt before calling scanf.
    Its crazy, I know.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    And, unfortunately, it still doesn't work.
    I know that it will not work according to the code that you posted.

    I don't see the relation of the 2 scanf() input to the goal of your program which is to print chars to a new line by line. Anyway the colored red statement in the code is causing an excess buffer.
    Code:
    float rt, gt;
    int a;
          
    printf ("Input value rt:\n");
    scanf ("%f", &rt);  //You can input but with excess
    printf ("Input value gt:\n");
    scanf ("%f",&gt);  //You can input but with excess
    
    
    int i;
    char str1[50];
    printf ("Input string\n");
    fgets(str1, 50, stdin); //Excess go here, you cannot input
    If you cannot input any value for fgets() then there will be no printed string. Use fgets for each input to avoid excess buffer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem with GETLINE
    By wco5002 in forum C++ Programming
    Replies: 13
    Last Post: 07-07-2008, 09:57 AM
  2. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 02:07 PM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  4. Strange problem
    By ~Kyo~ in forum Game Programming
    Replies: 0
    Last Post: 02-14-2006, 10:35 PM