Advertisement
Guest User

Untitled

a guest
Jul 29th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4.  
  5. void die(const char *message)
  6. {
  7.   if(errno) {
  8.     perror(message);
  9.   } else {
  10.     printf("ERROR: %s\n", message);
  11.   }
  12.  
  13.   exit(1);
  14. }
  15.  
  16. int main(int argc, char *argv[]) {
  17.   const double yins = 0.067;
  18.   const double siny = 14.91;
  19.   if(argc < 2) die("USAGE: <currency option> <value to convert>");
  20.   char curr = argv[1][0];
  21.   char *ammount_str = argv[2];
  22.   double ammount;
  23.   sscanf(ammount_str, "%lf", &ammount);
  24.   double converted;
  25.  
  26.   if(curr == 'y') {
  27.     converted = ammount * siny;
  28.     printf("%f SEK is equal to %f yen.\n", ammount, converted);
  29.   } else if(curr == 's') {
  30.     converted = ammount * yins;
  31.     printf("%f yen is equal to %f SEK.\n", ammount, converted);
  32.   } else die("Invalid currency option.");
  33.  
  34.   return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement