c++ - scanf() causing strange results -


I have a code that presents an interesting question (in my opinion).

  / * Power picks number for integer powers / / #include & lt; Stdio.h & gt; Double power (double n, int p); Int main (zero) {double x, expo; / * Is the original number and the result of expo * / int f; / * XP is exponance that is being extended to x * / printf ("Enter a number and positive integer that \ n the first number to be raised. \ N Enter q to leave \ n"); While (scanf ("% lf% d", & amp; x, & amp; x) == 2) {xpow = power (x, exp); Printf ("% 3g power% d in% .5g \ n", x, exp, xpow); Printf ("Enter the next pair of numbers or q to leave. \ N"); } Printf ("Hope you enjoyed your electric journey - bye! \ N"); Return 0; } Double Power (Double N, Int P) {Double PA = 1; Int i; For (i = 1; i & lt; = p; i ++) {P * n; } Return Pau; }  

If you see the numbers to be entered in order of numbers then floating point number and then decimal number (base number and then exponent). But when I enter input with an integer base and floating point exponent, it produces a strange result.

  [Mike @ Mike / Code / Powercoda] Enter $ ./power number and positive integer number, the first number will increase. To leave q enter 1 2.3 1 to power 2 in 1 Enter the next pair of numbers or q 2 Enter the next pair of numbers or q to answer 2 to 3.4.  

This floating point starts pushing the second number of exponent back to the next input. I was hoping someone could understand what was happening behind the curtain. I know that this is scanf (), its array is not checking the boundaries, but if someone can give me some deeper understanding then I would appreciate it. Thanks Stack Overflow - M.I.

Edit Just wanted to thank everyone for your input. Any other answer is more welcome Thanks again, SEO.

Before reading "2.3" scanf "." It shows that this is no longer a valid integer and prevents ".3" in the buffer, is omitted, then you type "2 3.4" then scanf parses it when ".3 \ n2 3.4" is in buffer That is, it shows your example like ".3" and "2"


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -