NO

Author Topic: A new Code  (Read 5394 times)

aagajanian

  • Guest
A new Code
« on: October 08, 2014, 01:38:31 AM »
Dear Helpers,
I am not able to build and compile the following code. Would you please help me find the mistake.
Thanks,
Aram

#include <stdio.h>
/*paul wilson*/
/*et2560*/
/*UNIT 3 LAB A*/
/*this program is to calculate voltge from current and resistance using ohms law*/
int main (void)
{
   double voltage, current, resistance;
   printf("enter the current in amps: ");
   scanf("%1f", &current);
   printf("enter the resistance in ohms: ");
   scanf("%1f", &resistance);
   voltage=resistance*current;
   printf("\nThe voltage is %.3f for a circuit with %.3f current and %.3f
resistance\n",voltage, current, resistance);
   return(0);
}

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: A new Code
« Reply #1 on: October 08, 2014, 01:52:39 AM »
Take a very close look at the format argument of your scanf() statements...  ;)

Ralf

PS: It really helps to
a) put your code in code tags (the [ # ] button above the message entry window)
b) be a bit more specific about the error/problem you perceive

tpekar

  • Guest
Re: A new Code
« Reply #2 on: October 09, 2014, 10:53:30 PM »
Your scanf  '%1f' only allows for a one position number between 0 and 9.  Your printf '%.3f' is printing three decimals and omitting the whole portion of the number.  Using %f without a number in between eliminates this restriction.  If you want to format your output you could use something like '%7.2f' in your printf statement which would give you 4 places to the left of the decimal point and 2 places to the right of it.

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: A new Code
« Reply #3 on: October 09, 2014, 11:48:48 PM »
Your scanf  '%1f' only allows for a one position number between 0 and 9.  Your printf '%.3f' is printing three decimals and omitting the whole portion of the number.  Using %f without a number in between eliminates this restriction.  If you want to format your output you could use something like '%7.2f' in your printf statement which would give you 4 places to the left of the decimal point and 2 places to the right of it.
Well, a bit beside the point, I guess.

(S)He simply missed that he typed "%1f" (with the number 1 (one) instead of the likely intended "%lf" (with the lower case letter "l" ("L") as required for the data type double as which (s)he declared the variables to be read in by the scanf() function.

It simply comes down to the fact that someone really needs to pay attention to what is being written in the code. Computers (and compilers before that) can't not guess what someone intends to do, they will do exactly as being told. Or as in this case, the compiler will tell you that there is something wrong and that format string and variable type don't match...

Ralf

defrancis7

  • Guest
Re: A new Code
« Reply #4 on: October 10, 2014, 05:57:07 PM »
In the days of the typewriter, the lower case "l' was used for the numeral "1".  Perhaps the original poster has had some experience using typewriters?  Just saying...

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: A new Code
« Reply #5 on: October 11, 2014, 12:35:42 AM »
In the days of the typewriter, the lower case "l' was used for the numeral "1".  Perhaps the original poster has had some experience using typewriters?  Just saying...
That may quite well be the case, but still, the compiler will in this case only accept the lower case "l" ("L") as the scanf() parameter is a double. And hence it gives an error message to that fact...

Ralf

blackbee045

  • Guest
Re: A new Code
« Reply #6 on: October 21, 2014, 08:07:21 AM »
It simply comes down to the fact that someone really needs to pay attention to what is being written in the code. Computers (and compilers before that) can't not guess what someone intends to do, they will do exactly as being told. Or as in this case, the compiler will tell you that there is something wrong and that format string and variable type don't match...