Thread: Cannot figure out the rest of the program

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    24

    Cannot figure out the rest of the program

    I can only use <stdio.h> for the problem.
    I wrote most of it out but cannot figure out the special cases.
    Code:
    #include <stdio.h>
    #define VARIABLE_NAME 'Y'
    int main()
    {
      double a, b, c, d;
      char x;
      printf("Enter a linear equation:    ");
      do {
        scanf("%lf %c + %lf = %lf", &a, &x, &b, &c);
      } while (getchar() != '\n');
    
      d = (c - b) / a;
    
      if (a == 0) {
        printf("Invalid equation!\n");
        return 1;
      }
    
      else if ( /* condition */ ) {
        /* code */
    
      } else {
        printf("Y = %.3lf\n", d);
      }
    
      return 0;
    }
    
    #include<stdio.h>
    #defineVARIABLE_NAME'Y'
    int main()
    {
      double a, b, c, d;
      char x;
      printf("Enter a linear equation:    ");
      do {
        scanf("%lf %c + %lf = %lf", &a, &x, &b, &c);
      } while (getchar() != '\n');
    
      d = (c - b) / a;
    
      if (a == 0) {
        printf("Invalid equation!\n");
        return 1;
      }
    
      else if ( /* condition */ ) {
        /* code */
    
      } else {
        printf("Y = %.3lf\n", d);
      }
    
      return 0;
    }
    Cannot figure out the rest of the program-screenshot-30-jpgCannot figure out the rest of the program-screenshot-31-jpg
    I cannot ask the user to input values of a,b and c.
    Last edited by Salem; 11-12-2020 at 08:47 AM. Reason: Removed crayola

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    799
    scanf() returns the number of convertions made.

    You should test if a is zero before trying to use it as a divisor.

    %lf in printf() has a different meaning than on scanf().

    You don't need that do..while loop using getchar().

    Since you did a good effort to solve this, here's a possible implementation:
    Code:
    #include <stdio.h>
    
    #define VARIABLE_NAME 'Y'
    
    // FIX: This is C, so void is needed!
    int main ( void )
    {
      double a, b, c;
      char y;
    
      printf ( "Enter a linear equation:  " );
      fflush ( stdout );
    
      // NOTE: No spaces!
      if ( scanf ( "%lf%c+%lf=%lf", &a, &y, &b, &c ) != 4 )
      {
      invalid_input:
        puts ( "Invalid input!" );
        return 1;
      }
    
      // NOTE: It is OK to use goto!
      if ( y != VARIABLE_NAME )
        goto invalid_input;
    
      // Check if a isn't zero first.
      if ( a == 0.0 )
      {
        puts ( "Invalid equation!" );
        return 1;
      }
    
      // Use %g to format the result properly (otherwise, small results
      // could appear as 0.000000.
      // Also, print the "variable name".
      printf ( "%c = %g\n", y, ( c - b ) / a );
    
      return 0;
    }
    Last edited by flp1969; 11-12-2020 at 07:55 AM.

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    24
    There can be spaces between the other characters just not between a and Y. Also if the equation is valid like you can see in the second run where bla bla is inputted it will still give out the answer. Those are the parts I cannot seem to figure out. Sorry for any inconvenience.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,068
    %lf allows spaces before it, but you do need to add some extra spaces to flp's code:
    Code:
    scanf("%lf%c +%lf =%lf", &a, &y, &b, &c)
    Or, equivalently, but perhaps more readable:
    Code:
    scanf("%lf%c + %lf = %lf", &a, &y, &b, &c)
    And of course, don't ever use goto!
    And you don't need the fflush(stdout) since the scanf will automatically do that (i.e., stdin is "tied" to stdout).
    Philosophy is a battle against the bewitchment of our intelligence by means of language. - Wittgenstein

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    799
    Quote Originally Posted by john.c View Post
    And of course, don't ever use goto!
    And you don't need the fflush(stdout) since the scanf will automatically do that (i.e., stdin is "tied" to stdout).
    Why not? Just because Dijkstra told you so?

    And fflush(stdout) is there not because scanf(), but because stdout is line buffered. Not all c libraries garantees the flushing of the buffer just before printf() returns if the string does not contain a '\n' char.

    stdin is not 'tied' to stdout... I've got this kind of problem with old compilers when you print something just before using scanf() or some function using stdin and NOTHING is printed just as expected. So, fflush(stdout) garantees the right behavior.

    glibc garantees the flushing just before printf returns (due to threading issues), but this is not always true.
    Last edited by flp1969; 11-12-2020 at 09:56 AM.

  6. #6
    Registered User
    Join Date
    Nov 2020
    Posts
    24
    so goto is always bad?

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,068
    Quote Originally Posted by Timmy10 View Post
    so goto is always bad?
    For beginners, yes.
    flp simply doesn't know what he's talking about.
    Philosophy is a battle against the bewitchment of our intelligence by means of language. - Wittgenstein

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    799
    Quote Originally Posted by Timmy10 View Post
    so goto is always bad?
    As John says, for beginners, yes... goto should be avoided, even for experienced programmers, but it isn't always 'bad'.

    And. yes... I do know what I am talking about.

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    1,068
    Just ignore flp.
    He's proven again and again that he doesn't know what he's talking about.
    He's apparently 51 years old but acts like a baby.
    It's pathetic.
    Philosophy is a battle against the bewitchment of our intelligence by means of language. - Wittgenstein

  10. #10
    Registered User
    Join Date
    Feb 2019
    Posts
    799
    In my example code here's some ways to avoid using goto in this fragment:
    Code:
      if ( scanf ( "%lf%c + %lf = %lf", &a, &y, &b, &c ) != 4 )
      {
      invalid_input:
        puts ( "Invalid input!" );
        return 1;
      }
      
      // NOTE: It is OK to use goto (in this context)!
      if ( y != VARIABLE_NAME )
        goto invalid_input;
    1- Replicate the same block in the second if:
    Code:
      if ( scanf ( "%lf%c + %lf = %lf", &a, &y, &b, &c ) != 4 )
      {
        puts ( "Invalid input!" );
        return 1;
      }
      
      if ( y != VARIABLE_NAME )
      {
        puts ( "Invalid input!" );
        return 1;
      }
    This, in an unoptimized compilation, will make the program a little bigger (2 calls to puts).

    2- Use setjmp/longjmp:
    Code:
      jmp_buf jb;
    
      if ( setjmp( jb ) )
      {
        puts( "Invalid input!" );
        return 1;
      }
    
      ...
    
      if ( scanf ( "%lf%c + %lf = %lf", &a, &y, &b, &c ) != 4 )
        longjmp( jb, 1 );
    
      if ( y != VARIABLE_NAME )
        longjmp( jb, 1 );
    This will create a more complex code (slower and bigger).

    3- Use a helper function which never returns
    Code:
    static void invalid_func( void )
    {
      puts( "Invalid input!" );
      exit(1);
    }
    ...
    int main( void )
    {
    ...
      if ( scanf ( "%lf%c + %lf = %lf", &a, &y, &b, &c ) != 4 )
        invalid_func();
      
      if ( y != VARIABLE_NAME )
        invalid_func();
    Solution 2 and 3 breaks the conditions of the exercise (only to use stdio.h!) and create a bigger code.
    If you want to avoid goto, use solution #1.

    With solution 3 the compiler can decide to make the function inline, which is more or less equivalent to solution 1, to avoid inlining you'll need to use a compiler extenstion. For GCC:
    Code:
    __attribute__((noinline))
    static void invalid_func( void ) { ... }
    But, since this is not standard, It can be unacceptable to solve the problem at hand.
    Last edited by flp1969; 11-12-2020 at 10:31 AM.

  11. #11
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    764
    flp is right. The C standards make no guarantee that stdout will be flushed when the program reads from stdin, even if both point to the same terminal (and some implementations don't automatically flush stdout before reading from stdin). If you care about portability and not relying on the behavior of some implementations, it's best to fflush(stdout) before reading from stdin.

    See Question 12.4.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-22-2012, 12:05 PM
  2. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-01-2008, 11:56 PM
  3. Can't figure out why this program won't work
    By Northstar in forum C Programming
    Replies: 6
    Last Post: 11-26-2007, 11:31 AM
  4. Cant figure out how to end my program
    By Summonerur in forum C Programming
    Replies: 9
    Last Post: 10-11-2004, 10:53 PM
  5. Help with the rest of this program
    By kroogs in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2003, 01:02 PM

Tags for this Thread