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