Thread: My frist program Omhs law

  1. #1
    Mac_thevamp
    Guest

    Talking My frist program Omhs law

    Here is the the source code for my Omhs Law program, I can't figure how to attach it.Fell free to use this progam , modifi it as lone reconise me A.Mac Hugh as the author.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Omhs Law program
    Author :A.Mac Hugh
    Date:-30-09-2002
    Version:-*/
    void voltage (); /* function prototype*/
    void current (); /* function prototype*/
    void reistance (); /* function prototype*/
    
    void main(void)
    {    int x;
         void menu (void); /* function prototype*/
         printf("\n\tWelcome to Machsofts Omhs Lawn. \n\tThis program will culate thr ristance , current or voltage.");
    	 printf("\n\tThis is copyright Machsoft inc 2002");
    	 printf("\n\tVersion 1\n\n\n");/* intoduction*/
    
    	 
    	 
    	 menu (); /*fuction beging run*/
         printf( "please hit any x and enter to continue\n\t");
         scanf("%d",&x) ;
    }
    
    void menu (){
    int nums;
    
    printf("To calulate the voltage  press\n\t 1\n\n\t\n");/* ask user for therir option*/
    printf("To calulate current press \n\n\t2\n\n\t\n");
    printf("To caulate reistance press \n\n\t3\n\n\t\n");
    
    scanf("%d",& nums);/*get option*/
    
    if (nums == 1)
    voltage () ;   /*fuction beging run*/
    
    else if(nums == 2)
    current ();     /*fuction beging run*/
    
    else if(nums==3)
    reistance ();
    
    }
    
    void voltage (){      /* Voltagre fuction varibles v i r*/
    
    int v;
    int i;
    int r;
        
        printf(" Please enter in the values for the current (i)\n" );/*ask user enter in values*/
    	printf("and the reistance (r)\n");
        scanf("%d",&i);/* get values*/
        scanf("%d",&r); 
        v= i*r;
    
        printf("\nThe voltage is %d volts\n\t",v);/* prints out voltage*/
    
    
    
    }
    void current (){      /* Current fuction varibles v i r*/
    int v;
    int i;
    int r;
    
    printf(" Please enter in the values for the voltage (v) and the reistance (r)\n\t");/*ask user enter in values*/
    scanf("%d%d",&v,&r);;/* get values*/
    
        i= v/r;
    
        printf("\nThe curremt  is %d amps\n\t",i);/* prints out current*/
    
    
    
    }
    void reistance (){    /* Reistance fuction varibles v i r*/
    int v;
    int i;
    int r;
    
    printf(" Please enter in the values for the current (i) and the voltage (v)\n\t");/*ask user enter in values*/
    scanf("%d%d",&v,&i);;/* get values*/
    
        r= v/i;
    
        printf("\nThe reistance is %d omhs\n\t",r);/* prints out resistance*/
    
    
    
    }
    [EDIT]Code tags added by Hammer. Please read this thread.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>void main(void)
    Oh dear, oh dear.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You have the same spelling errors all over the place, did you use a lot Cut-n-Paste? .
    You even have it in your function (Resistance). LoL!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Mac_thevamp
    Guest

    Angry SOORY

    Look i am only learning c a moment,in collage and i know i don't have to put void main(void) i just do it to make sure that everthing is right.Also I'm sorry that not as good of a speller than the rest of you.Anyway i hope to have version 2 ready soon,with all the mistakes fixed.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    No offense meant, sorry!
    And what Hammer meant was that the correct return value of main() is an int, not void.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    You might consider changing the if else structure to a switch statement, or even consider calculating simple power or adding voltage and current division for series or parallel circuits.

    Code:
    ...
    scanf("%d", &choice)
    switch(choice)
    {
       case 1: voltage();
               break;
       case 2: current();
               break;
       case 3: resistance();
               break;
       default: // invalid entry
               break;
    };
    AC power is a little more involved
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you wanted comments on your code, why didn't you ask?!

    Right, here are some:

    - Function prototypes
    If a function takes no parameters, its prototype should really be declared with void, like this:
    >void voltage (void);

    - void main
    Yep, as already stated the correct return from main is an int. So make it
    >int main(void)
    and don't forget to return a value at the end.

    - Spelling
    As already stated, your spelling is terrible. If your doing a programming course, your teacher may well knock marks off for such things.

    - scanf("%d",& nums)
    Always check the return code from scanf() to ensure it did what you want. If you don't understand, try entering a letter instead of a number and see what happens.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Mac_thevamp
    Guest

    Thanks

    Thanks Hammer and all the others for the postive and negitive feed back,I'm sorry I sounded like a bit of a fool the other day . I am doing a collage physcis course and that's why i am learing C\C++
    I think I'll invest in a pocket dictionary, and i will try the switich statment and may be a loop.So what this space foe version 2.Also at collaeg I will be working on an einstine caculatetor and when it's finish i'll post it up.
    Mac

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. genocide law is history
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 07-17-2003, 09:46 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM