Thread: array i[n],v[n]..etc cant display from [0] to [50] only newly added input displayed

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    3

    array i[n],v[n]..etc cant display from [0] to [50] only newly added input displayed

    I am trying to display out all the input values of array i[n],v[n],p[n],r[n] in high_low(), but only last newly added input will be displayed. help pls?

    thanks!



    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #define SIZE 50
    
    
    int menu(void);
    void printline(void);
    void power_VI(void);
    //void power_VR(void);
    //void power_IR(void);
    void high_low(void);
    int check(float *);
    
    
    int choice,n=0;
    
    char input[SIZE];
    float vots[SIZE],amps[SIZE],res[SIZE],power[SIZE];
    
    float *i=amps,*v=vots,*r=res,*p=power;
    
    
    int main (void)
    {
    
    
    
    		while(1)
    		{
    
    			choice=menu();
    
    			switch(choice)
    			{
    				case '1':	power_VI();
    						
    							break;
    				case '2':
    						//	power_VR();
    							break;
    				case '3':
    					//		power_IR();
    							break;
    				case '4':
    							high_low();
    							break;
    
    				case '5':	
    							printf("\n\t\t");
    							break;
    				default :
    							printf("\n\t\tInvalid Key -- try again!\n");
    			}
    			if(choice == '5')
    			break;
    		
    			printf("\n\t\tPress any key to continue!");
    			getch();
    		}
    
    }
    
    
    void high_low(void)
    {
    		
    	int count=0;
    
    	for(count=0;count < n; count++)
    		{
    	system("cls");
    	printf("\n");
    	printf("\tThe calculated DC powers are:\n");
    	printline();
    
    	printf("\n\tNo.\tCurrent\t\tVoltage\t\tResistance\tPower");
    	printf("\n\t\t(amps)\t\t(volt)\t\t(ohms)\t\t(watts)\n");
    	printline();
    
        printf("\t%2d\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n",
    	count+1,i[count],v[count],r[count],p[count]);
    		
    	
    		}
    
    }	
    
    void power_VI (void)
    
    {	
    	system("cls");
    	printline();
    	printf("\t\tDC Power calculation using Voltage and Current\n");
    	printline();
    
    	do{
    	
    	printf("\n\t\tEnter the value of voltage <vots> : ");
    	gets(input);
    	v[n]=(float)strtod(input, NULL);
    
    	}while(check(&v[n])==0);
    	
    	
    
    
    	do{
    
    	printf("\t\tEnter the value of current <amps> : ");
    	gets(input);
    	i[n]=(float)strtod(input, NULL);
    
    	}while(check(&i[n])==0);
    	
    
    	
    	printf("\n\t\tThe computed values are :\n");
    	r[n]   = (v[n])/(i[n]);
    	p[n]  = (v[n])*(i[n]);
    	
    	printf("\n\t\tResistance = %.2f ohms\tDC power = %.2f watts\n", r[n],p[n]);
    	
    	n++;
    
    
    }
    int check(float *x)
    {
    	int flag=1;
    	
    	if(*x<=0)
    	{
    		printf("\n\t\tInvalid data entry! Please enter again.\n");
    		flag=0;
    	}
    
    	return flag;
    }
    /*
    void power_VR (void)
    
    {
    	
    	system("cls");
    	printline();
    	printf("\t\tDC Power calculation using Voltage and Resistance\n");
    	printline();
    
    	do{
    
    	printf("\n\t\tEnter the value of voltage    <vots> : ");
    	gets(input);
    	v[index]=strtod(input, NULL);
    
    	}while(check(v)==0);
    
    	do{
    
    	printf("\t\tEnter the value of resistance <ohms> : ");
    	gets(input);
    	r[index]=strtod(input, NULL);
    	}while(check(r)==0);
    
    	printf("\n\t\tThe computed values are :\n");
    	i[index] = (v[index])/(r[index]);
    	p[index] = (i[index])*(i[index])*(r[index]);
    	
    	printf("\n\t\tCurrent = %.2f amps\tDC power = %.2f watts\n", i[index],p[index]);
    
    
    }
    void power_IR (void)
    
    {
    	
    	system("cls");
    	printline();
    	printf("\t\tDC Power calculation using Current and Resistance\n");
    	printline();
    
    	do{
    	printf("\n\t\tEnter the value of current    <amps> : ");
    	gets(input);
    	i[index]=strtod(input, NULL);
    	}while(check(i)==0);
    
    
    	do{
    
    	printf("\t\tEnter the value of resistance <ohms> : ");
    	gets(input);
    	r[index]=strtod(input, NULL);
    	
    	}while(check(r)==0);
    	
    	printf("\n\t\tThe computed values are :\n");
    	v[index] = (i[index])*(r[index]);
    	p[index] = (v[index])*(v[index])/(r[index]);
    	
    	printf("\n\t\tVoltage = %.2f ohms\tDC power = %.2f watts\n", v[index],p[index]);
    
    
    }
    
    
    */
    		
    int menu(void)
    
    {
    	system("cls");
    	printline();
    	printf("\t\tCALCULATION OF DC POWER\n");
    	printline();
    	printf("\t\t(1)\tUsing Voltage and Current\n");
    	printf("\t\t(2)\tUsing Voltage and Resistance\n");
    	printf("\t\t(3)\tUsing Current and Resistance\n");
    	printf("\t\t(4)\tDisplay Highest & Lowest Power\n");
    	printf("\t\t(5)\tExit\n");
    	printline();
    	printf("\n\t\tPlease enter your choice : ");
    	return getche();
    }
    
    
    void printline (void)
    {
    	int count;
    	printf("\t");
    	for(count=0; count<65; count++)
    	{
    		printf("=");
    	}
    		printf("\n");
    }
    Code Tags added by....guess who ?...Kermi3

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. for loop with sort issues
    By redmondtab in forum C Programming
    Replies: 10
    Last Post: 10-09-2006, 10:36 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM