please help me im dieing

This is a discussion on please help me im dieing within the C Programming forums, part of the General Programming Boards category; working on a program that works out complex curtits and im stuck im am useing pella c to wright the ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    Post please help me im dieing

    working on a program that works out complex curtits and im stuck im am useing pella c to wright the program. the weird thing is that it run fine to put in the voltage the how many resisters then crashes.here it is.

    Code:
    #include <stdio.h> 
    
    
    
    void instructions (void); //print intructions
    struct resist{char relate; double value;}add; //structure for additional resistors 
    void draw_v (double v); //function for voltage line
    void draw_lines(struct resist add, int n); //lines for additional resistors
    void initial(double r1);
    //calc functions
    double totr (struct resist add, int n, double r1);
    double toti (double r, double v);
    double power (double i, double v);
    void display (double r, double i, double p);//display funtion
    
    int 
    main (void)
    {
    	int n; //number of resistors
    		
    	char decide; //check printed curcuit
    	double v; //voltage
    	double r1; //initial resistor
    
    	instructions ();
    	
    	printf ("What is your source voltage?    \n");
    	scanf ("%lf.3", &v);
    	while (v <1){
    		printf ("Try again...    \n");
    		scanf ("%lf.3", &v);
    	}
    	
    	
    	printf ("How many resistors are in your circuit?     \n");
    	scanf ("%d", &n);
    	while (n < 1) {
    		printf ("Entry invalid, please try again...   \n");
    		scanf ("%d", &n);
    	}
    
    
    	printf ("What is the value of the initial resistor?  \n");
    	scanf ("%lf", &r1); 
    	while ( r1 < 0) {
    		printf ("What is the value of the initial resistor, now?  \n");
    		scanf ("%lf", &r1);
    }
    	double more[n-1]; //array variable in struct
    	char and [n-1]; //array variable in struct
    	int i; //initiator variable
    
    	do {
    	printf ("What is the value of your next resistor?    \n");
    	scanf("%lf", more[i]);
    		add.value = more[i];
    	printf ("And what is this resistor's relationship to the previous resistor?    %.3f\n");
    	scanf ("%c", and[i]);
    		add.relate = and[n-1];}
    	 while  (i = n -1, i < n && i > 0, i -- );
    	draw_v (v);
    	draw_lines(add, n);
    	initial (r1);
    	printf ("\n Is this the circuit you would like solved?    %c \n", decide);
    	scanf ("%c", &decide);
    	while (decide != 'y' || 'n'){
    			printf ("Please answer y for yes or n for no...   %c  \n", decide);
    			scanf ("%c", &decide);
    		}
    	if (decide == 'n') 
    			printf ("Sorry, please reload program.");
    	else
    			printf ("Your circuit calculations are....\n");
    	
    	
    	totr(add, n, r1);
    	//values not passed from main
    	toti;
    	power;
    	display;	
    
    
    	return (0); //main/////////////////////////////////////////////////////////////////////
    }
    //////////////////////////////////////////////////////////////////////////////////////
    void instructions (void)
    {
    	printf ("This program will only work if you can make one continuous line through your circuit. \n");
    	printf ("This program will only work if your circuit has only one voltage source. \n");
    	printf ("This program requires lower case letters for verifiers. \n");
    	printf ("Enter the value of source voltage. \n");
    	printf ("Name the number of resistors. Then, starting with the outer most resistor, give the values of each resistor. \n"); 
    	printf ("For every additional resistor also give it's relation, series - (s) or parallel - (p), to the previous resistor. \n"); 
    	printf ("Next the program will print a simplyfied image of the circuit based on the resistor values that were entered. \n");
    	printf ("If the circuit is not to your liking type 'n' and start over. \n");
    	printf ("If the circuit meets your approval type 'y' to recieve your calculations. \n");
    	printf ("The program will calculate your total resistance, total current,  and total power.\n");
    	}
    //////////////////////////////////////////////////////////////////////////////////////
    
    
    
    
    	void draw_v ( double v)
    {
    printf  ("*");
    printf  ("*");
    printf  ("*");
    printf  ("*");
    printf("%lf ",&v);
    printf  ("*"); 
    printf  ("*");
    printf  ("*");
    printf  ("*");
    }
    ////////////////////////////////////////////////////////////////////////////
    void draw_lines(struct resist add, int n)
    {
     	
    	if (add.relate == 's' )
    		printf("***********%lf ***********",add .value );
    
    	else if (add.relate == 'p')
    printf("**************************");
    printf("*                                              ");
    printf("*                                              ");
    printf("*                                              ");
    printf("%lf                                          ",add. value);
    printf("*                                              "); 
    printf("*                                              ");
    printf("*                                              ");
    printf("*                                              ");
    
    }
    void initial(double r1)
    
    {
    printf  ("*");
    printf  ("*");
    printf  ("*");
    printf  ("*");
    printf("%lf", &r1);
    printf  ("*"); 
    printf  ("*");
    printf  ("*");
    printf  ("*");
    }
    
    
    double totr (struct resist add, int n, double r1)
    {
    	double r;
    	int x;
    
    	if (add.relate == 'p')
    		r = (add.value*r1)/(add.value+r1);
    	else
    		r = add.value+r1;
    
    	if (add.relate == 'p') {
    	do ( r=(add.value*r)/(add.value+r));
    
    	while (x = n-2, x > 0, --x);
    	}
    	else {
    		do (r=add.value+r);
    			while (x = n-2, x > 0, --x);
     }
    		return (r);
    	}
    
    
    
    double toti (double v,double r)
    {
    	double i;
    	i = v/r;
    	return (i);
    			}
    double power (double i,double v)
    {
    	double p;
    	p = i*v;
    	return (p);
    	}
    
    void display (double r,double i,double p)
    
    {
    	printf("The total resistance is .3%lf ohms", r);
    	printf("The total current is .3%lf amperes", i);
    	printf("The total power is .3%lf watts", p);
    }

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    and no this is not for school

    i just wanted a way to look at a circuit and not have to do the math every time.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Look at your compiler warnings, there should be tons of them. Fix all of those warnings to ensure (slightly) more reliable code. But in particular to your crash, its probably because of these lines
    Code:
    	scanf("%lf", more[i]);
    		add.value = more[i];
    	printf ("And what is this resistor's relationship to the previous resistor?    %.3f\n");
    	scanf ("%c", and[i]);
    Where the "scanf" requires an address, like "&myVariable" not "myVariable". If you read the compiler warnings you'd see this warning, though.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Location
    Germany, Hannover
    Posts
    15
    just a few hints:
    Code:
    double more[n-1]; //array variable in struct
    doesn't work, because at compile time n doesn't contain a valid value, better use
    Code:
    double more[10]; //array variable in struct
    Code:
    scanf("%lf", more[i]);
    should be
    Code:
    scanf("%lf", &more[i]);
    Code:
    	scanf ("%c", and[i]);
    should be
    Code:
    	scanf (" %c", &and[i]);
    ... mind the blank before %c...
    Code:
    	while ((decide != 'y') &&(decide != 'n')){
    does work
    ur while loops are complicated, better keep it simple like
    Code:
    	 while  (--i >0 );
    (first loop )

    develop ur code step by step, verifying the results after each step by using debug/printf statements.
    its not useful to write the whole program and then start debugging , because u copy ur mistakes a lot of times and then nothing works as it should do

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by kermitaner View Post
    just a few hints:
    Code:
    double more[n-1]; //array variable in struct
    doesn't work, because at compile time n doesn't contain a valid value
    I sometimes forget this, too, but variable length arrays were added in the C99 standard, which we can assume he/she is using, otherwise it wouldn't compile (so he/she wouldn't know of a runtime error).

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    no warnings

    the compiler build it with on erros or warnings

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I actually can't continue to believe anything you say, considering the lack of proper grammar and coherency. Just because its not giving any warnings right now is probably because the warning level isn't strict enough. To make it more strict, look at the documentation for whatever compiler (or IDE and compiler) you're using.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Location
    Germany, Hannover
    Posts
    15
    Quote Originally Posted by nadroj View Post
    I sometimes forget this, too, but variable length arrays were added in the C99 standard
    ah, ok, I didn't know that ...

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    im sorry my grammer does not stand up to your stander but i have been up for 18 hr working on the project so im a little sleepy. i still do not get any warnings
    when the program builds i dont know what it is but its driving me mad.i know its just a dumb mistake on my behalf but it compiles and runs untill it hits the third question so i dont know. it does give me a -1billon number when it crashes ????????

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Did you implement the things that have been suggested to you by us to change? After you do that and if you are still having problems, then post your updated and complete code. Be sure to describe the problem also.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my program keeps dieing on me please help
    By sayword in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2003, 11:06 AM
  2. Further developing C for the web
    By bjdea1 in forum C Programming
    Replies: 24
    Last Post: 12-25-2002, 12:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21