### Help with formatting my output

I need help formatting my output to "look nice". Also, my first volts output is giving an odd output with random letters and numbers in it, but every other one is fine.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include #include #include using namespace std; double calc_volts(double[], double[], double[]); int main() { int j=0; double current[10] = {10.62,14.89,13.21,16.55,19.62,9.47,6.58,18.32,12.15,3.98}; double volts[10]; double resistance[10] = {4,8.5,6,7.35,9,15.3,3,5.4,2.9,4.8}; volts[j] = calc_volts(current, volts, resistance); cout<<"This program will output the voltage from a given current and resistance"<
Last edited on
I added some spaces and an end line:

Is this the "nice" you were thinking?

#include <iomanip>
#include <iostream>
#include <cmath>

using namespace std;
double calc_volts(double[], double[], double[]);

int main()

{

int j=0;

double current[10] = {10.62,14.89,13.21,16.55,19.62,9.47,6.58,18.32,12.15,3.98};

double volts[10];

double resistance[10] = {4.0,8.5,6.0,7.35,9.0,15.3,3.0,5.4,2.9,4.8};

volts[j] = calc_volts(current, volts, resistance);

cout<<"This program will output the voltage from a given current and resistance."<<endl<<endl;

for (j = 0; j<10; j++)
{
cout<<setw(6)<<fixed<<setprecision(3)<<"Current"<<" "<<j<<": "<<current[j]<<endl;
cout<<setw(6)<<fixed<<setprecision(3)<<"Resistance"<<" " <<j<<": "<<resistance[j]<<endl;
cout<<setw(5)<<fixed<<setprecision(3)<<"Volts"<<" "<<j<<": "<<volts[j]<<endl<<endl;
}

//system("pause");
return 0;

}
double calc_volts(double current[],double volts[], double resistance[])
{int j;

for (j = 0; j<10; j++)
{volts[j] = (current[j])*(resistance[j]);
}
return volts[j];

}
double printvolts (double outputvoltage[])
{for(int v=0;v<10;v++)
cout<<setw(6)<<fixed<<setprecision(3)<<" "<<outputvoltage[v]<<endl;
return 0;
}
yes, thank you. now the problem is that in the output of volts[0] is coming out at random numbers. (i.e. 954654.00045000000). what is happening here?
You are setting volts[0] to be equal to volts[10] in your calc_volts function. What you may want to do instead is to make your calc_volts function of type `void`, not return anything, and replace your line 22 with the following:

 ``123`` ``````// volts[j] = calc_volts(current, volts, resistance); // This function now returns void, and edits directly into "volts": calc_volts(current, volts, resistance);``````
Last edited on
Topic archived. No new replies allowed.