# Thread: Cannot figure out the rest of the program

1. ## 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;
}```  I cannot ask the user to input values of a,b and c. 2. 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;
}``` 3. 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. %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). 5. Originally Posted by john.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).
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. 6. so goto is always bad? 7. Originally Posted by Timmy10 For beginners, yes.
flp simply doesn't know what he's talking about. 8. Originally Posted by Timmy10 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. 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. 10. 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. 11. 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 %.3lfn, %lf, &c;, printfy, return 