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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#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,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"<<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(6)<<fixed<<setprecision(3)<<"Volts"<<j<<":	"<<volts[j]<<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;
}
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:

1
2
3
// 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.