Thread: Hi need some help

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    7

    Hi need some help

    Hi first time posting. Very new to C coding, currently doing the basics for college. The program is a decimal to rad/degrees to Fahrenheit converter. We had to add to the code to get resistors in series to add together. Just having trouble getting the last bit to work. Any help would be great. Its the last part of the code option 3 im having trouble with.
    Temp and Degrees To Rads.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
      int Option, ResCount , Resistors, ResValue, ResTotal;
      ResCount = 0;
      printf("\t\t_____________________\t\t");
      printf("\n\n\tl. Temperature conversion\n");
      printf("\t2. Angle conversion\n");
      printf("\t3. Series Resistance");
      printf("\n\t\nPlease select one of the above options: ");
      scanf("%i", & Option);
    
      if (Option == 1) {
        float c, f;
        do {
        printf("Enter Temp between -40 and 60: ");
        scanf("%f", & c);
    
        if (c < -40 || c > 60) {
        printf("\n\n\t Wrong Value");
    
          }
        } while (c < 0 || c > 100);
        f = c * 9.0 / 5.0 + 32.0;
        printf("Fahrenheit is %.2f", f);
    
      } else if (Option == 2) {
        float d, r;
        printf("Enter Degrees: ");
        scanf("%f", & d);
        r = d * 3.14 / 180.0;
        printf("Radians is %.2f\n\n", r);
      }
    
        else if (Option == 3) {
        printf("\n\t\tSeries Resistance\n");
        printf("\t\t___________________\n");
    
        printf ("\n\tHow many resisitors do you have?(2-9):\t");
        scanf  ("%i", &Resistors);
        if (Resistors < 2 || Resistors > 9){
        printf ("\tEnter a value between 2 and 9\n");
        }
        while (Resistors <2 || Resistors >9);
        for (ResCount=0; ResCount < Resistors; ResCount++){
        printf ("\tPlease enter R%i value (Ohms): ", ResCount+1);
        scanf("%i", ResValue);
        ResTotal = ResTotal + ResValue;
    }
        printf ("\n\tTotal Resistance is %i Ohms", ResTotal);
    }
    
    
    
    
      return 0;
    }
    Last edited by Salem; 02-24-2021 at 11:52 AM. Reason: Inlined the code for convenience

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,627
    1, indentation really helps to see what's going on.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      int Option, ResCount, Resistors, ResValue, ResTotal;
      ResCount = 0;
      printf("\t\t_____________________\t\t");
      printf("\n\n\tl. Temperature conversion\n");
      printf("\t2. Angle conversion\n");
      printf("\t3. Series Resistance");
      printf("\n\t\nPlease select one of the above options: ");
      scanf("%i", &Option);
    
      if (Option == 1) {
        float c, f;
        do {
          printf("Enter Temp between -40 and 60: ");
          scanf("%f", &c);
          if (c < -40 || c > 60) {
            printf("\n\n\t Wrong Value");
          }
        } while (c < 0 || c > 100);
        f = c * 9.0 / 5.0 + 32.0;
        printf("Fahrenheit is %.2f", f);
      } else if (Option == 2) {
        float d, r;
        printf("Enter Degrees: ");
        scanf("%f", &d);
        r = d * 3.14 / 180.0;
        printf("Radians is %.2f\n\n", r);
      }
      else if (Option == 3) {
        printf("\n\t\tSeries Resistance\n");
        printf("\t\t___________________\n");
        printf("\n\tHow many resisitors do you have?(2-9):\t");
        scanf("%i", &Resistors);
    
        if (Resistors < 2 || Resistors > 9) {
          printf("\tEnter a value between 2 and 9\n");
        }
        while (Resistors < 2 || Resistors > 9);
    
        for (ResCount = 0; ResCount < Resistors; ResCount++) {
          printf("\tPlease enter R%i value (Ohms): ", ResCount + 1);
          scanf("%i", ResValue);
          ResTotal = ResTotal + ResValue;
        }
        printf("\n\tTotal Resistance is %i Ohms", ResTotal);
      }
      return 0;
    }
    2, compile with lots of warnings enabled.
    Code:
    $ gcc -Wall -Wextra foo.c
    foo.c: In function ‘main’:
    foo.c:46:13: warning: format ‘%i’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
           scanf("%i", ResValue);
                 ^
    foo.c:46:7: warning: ‘ResValue’ may be used uninitialized in this function [-Wmaybe-uninitialized]
           scanf("%i", ResValue);
           ^
    In this case, you forgot an & on one of your scanf's.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2021
    Posts
    7
    Thanks for the reply. How do i set the compiler to have more warnings?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,627
    Well which compiler / IDE are you using?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2021
    Posts
    7
    Im using GNU compiler

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,627
    So am I.
    So you've already seen how I use gcc.
    Now you can do the same.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Tags for this Thread