Advertisement
Guest User

sandy's homework she can't solve

a guest
Oct 16th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. bool isValidInput(const int numArgsNeeded, const int numArgsRead, const bool isLastElementOnLine) {
  6.   char lastCharacterOnLine;
  7.   bool correctFormat = (numArgsRead == numArgsNeeded);
  8.   if(isLastElementOnLine) {
  9.     scanf("%c", &lastCharacterOnLine);
  10.     correctFormat = correctFormat && lastCharacterOnLine == '\n';
  11.   }
  12.   return correctFormat;
  13. }
  14.  
  15. char getValidChar(const bool isLastElementOnLine) {
  16.   const int numArgsNeeded = 1;
  17.   int numArgsRead;
  18.   char theCharacter;
  19.   numArgsRead = scanf(" %c", &theCharacter);
  20.   if(isValidInput(numArgsNeeded, numArgsRead, isLastElementOnLine)) {
  21.     return theCharacter;
  22.   }
  23.   else {
  24.     printf("Invalid formatting. Ending program.\n");
  25.     exit(0);
  26.   }
  27. }
  28.  
  29. double getValid(const bool isLastElementOnLine) {
  30.   const int numArgsNeeded = 2;
  31.   int numArgsRead;
  32.   double tempValue;
  33.   char tempUnit;
  34.   numArgsRead = scanf("%lf %c", &tempValue, &tempUnit);
  35.   if(isValidInput(numArgsNeeded, numArgsRead, isLastElementOnLine)) {
  36.     return tempValue && tempUnit;
  37.   }
  38.   else {
  39.     printf("Invalid formatting. Ending program.\n");
  40.     exit(0);
  41.   }
  42. }
  43.  
  44. char getNewUnit(const bool isLastElementOnLine) {
  45.   const int numArgsNeeded = 1;
  46.   int numArgsRead;
  47.   char newUnit;
  48.   numArgsRead = scanf(" %c", &newUnit);
  49.   if(isValidInput(numArgsNeeded, numArgsRead, isLastElementOnLine)) {
  50.     return newUnit;
  51.   }
  52.   else {
  53.     printf("Invalid formatting. Ending program.");
  54.     exit(0);
  55.   }
  56. }
  57.  
  58. int main() {
  59.  
  60.   char conversionChoice = 0;
  61.   char newUnit = 0;
  62.   double originalValue = 0;
  63.  
  64.   printf("Pick the type of conversion that you would like to do.\n");
  65.   printf("T or t for temperature\n");
  66.   printf("D or d for distance\n");
  67.  
  68.     printf("Enter your choice: ");
  69.     conversionChoice = getValidChar(true);
  70.   if (conversionChoice == 'T' || conversionChoice == 't') {
  71.     printf("Enter the temperature followed by its suffix (F, C, or K): ");
  72.     originalValue = getValid(true);
  73.     printf("Enter the new unit type (F, C, or K): ");
  74.     newUnit = getNewUnit(true);
  75.  
  76.   }
  77.   else if (conversionChoice == 'D' || conversionChoice == 'd') {
  78.     printf("Enter the distance followed by its suffix (I,F,Y,M): ");
  79.     originalValue = getValid(true);
  80.     printf("Enter the new unit type (I,F,Y,M): ");
  81.     newUnit = getNewUnit(true);
  82.   }
  83.   else {
  84.     printf("Unknown conversion type %c chosen. Ending program.", conversionChoice);
  85.     exit(0);
  86.   }
  87.  
  88.   return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement