Home > Back-end >  Beginners ask directions
Beginners ask directions

Time:12-15

Beginner C language, a great god, please help me see this programming results why not?

CodePudding user response:

Control yourself
 
#include
#include
#include

Int main ()
{
Double m;
int c;
Printf (" input a real number: \ n ");
Lf the scanf (" % ", & amp; M);

While (1)
{
Printf (" * * * * * * * * * * * * * * * * * * * * \ n ");
Printf (" 1. Output opposite \ n ");
Printf (" output square number 2. \ n ");
Printf (" 3. Output square root \ n ");
Printf (" exit 4. \ n ");
Printf (" * * * * * * * * * * * * * * * * * * * * \ n ");
Printf (" input options: ");
The scanf (" % d ", & amp; c);
The switch (c)
{
Case 1:
Printf (" % 2 f opposite is: % 2 f \ n ", m, -m);
break;
Case 2:
Printf (" % 2 f square number is:. % 2 f \ n ", m, m * m);
break;
Case 3:
Printf (" %. What's the square root of 2 f: % 2 f \ n ", m, SQRT (m));
break;
Case 4:
Printf (" exit the program! \n");
exit(0);
break;
Default:
Printf (" please enter a number between 1-4! \n");
break;
}
}

system("pause");
return 0;
}

CodePudding user response:

Line 9, the scanf function, wrong
 
#include
#include

Int main (void)
{
int i;
Float m;
Int c=5;
Printf (" input a real number: \ n ");
The scanf (" % f ", & amp; M);
Printf (" * * * * * * * * * * * * * * * * * * * * * \ n ");
Printf (" 1. Input the opposite \ n ");
Printf (" output square number 2. \ n ");
Printf (" 3. Output square root \ n ");
Printf (" exit 4. \ n ");
Printf (" * * * * * * * * * * * * * * * * * * * * * \ n ");
While (I & lt; {
=4)The scanf (" % d ", & amp; c);
The switch (c) {
Case 1:
Printf (" output opposite: % 2 f \ n ", -m);
break;
Case 2:
Printf (" output square number: % 2 f \ n ", m * m);
break;
Case 3:
Printf (" output square root: % 2 f \ n ", SQRT (m));
break;
Case 4:
Printf (" exit the program! \n");
break;
Default:
Printf (" please enter a number between 1 ~ 4! \n");
}
If (c==4) {
break;
}
}
return 0;
}

CodePudding user response:

Reference
printf (" %. What's the square root of 2 f: % 2 f \ n ", m, SQRT (m));

The perfect again, let it 0 when meet with negative to prevent error
 printf (" %. What's the square root of 2 f: % 2 f \ n ", m, (m<0? 0: SQRT (m))); 
  • Related