void function()


I am attempting to write a program that starts with a void function (no parameters) I cannot get the "printmessage" function to display. If I remove the "int main" function the "printmessage" works fine. Part of my assignment is to display the printmessage function then proceed with more arithmetic and additional output statements. I am as newb as they come, please pardon any confusing terminology.
At one point I did get both functions to display but they were out of order.
I can take rough criticism so dont hold back.






#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;

const double area = 1.2;
const double dist = .04;
const double diacon = 30;
const double elecon = 8.8542e-012;
double capa;

void printmessage()
{
cout << setw(44) << "Computation of Capacitance" << endl;
cout << setw(32) << "by" << endl;
cout << setw(38) << "James S. Bates" << endl;

}

int main()
{
capa = 3 * 3;
cout << "capa = " << capa << endl;

system("PAUSE");
}
@jesse1221

add printmessage(); right after
1
2
int main()
{
, and that should do the trick.
You've misunderstood how C++ programs work. They start execution at main, and every other function only gets executed if it gets called. In your code, you defined the function printmessage(), but defining a function doesn't execute it. You still have to call it.
Immediately after I posted I figured it out, thanks for the reply. I do have another issue now, I cannot get my math to display anything except 0.00. "Capa" should display a number similar to "1.5583e-008"

This is my program complete except for the math:

#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;

double area = 1.20;
double dist = .04;
double diacon = 30;
const double elecon = 8.8542e-012;
double capa;

void printmessage()
{
cout << setw(44) << "Computation of Capacitance" << endl;
cout << setw(32) << "by" << endl;
cout << setw(38) << "James S. Bates" << endl;

}

int main()
{
double area;
double dist;
double diacon;
double elecon;
double capa;
printmessage();
cout << endl << endl;

cout << fixed << setprecision(2);
cout << "Enter the area of the plates (in meters squared):";
cin >> area;
cout << endl << endl;

cout << "Enter distance between the plates (in meters):";
cin >> dist;
cout << endl << endl;

cout << "Enter dielectric constant of the separating material:";
cin >> diacon;
cout << endl << endl;

cout << "Results:" << endl;
cout << setw(21) << "Area of the plates:" << setw(31) << "1.20 meters^2" << endl;
cout << setw(24) << "Distance between them:" << setw(26) << "0.04 meters" << endl;
cout << setw(22) << "Dialectric constant:" << setw(21) << "30.00" << endl << endl;
cout << setw(20) << "Electric constant:" << setw(36) << "8.8542e-012 farads/meter" << endl << endl;


capa = (1.20 / 0.04) * 30.00 * 8.8542e-012;

cout << "Capacitance:" << setw(29) << capa << endl;

system("PAUSE");
}
http://www.cplusplus.com/reference/iostream/manipulators/scientific/
http://www.cplusplus.com/reference/iostream/manipulators/setprecision/

The precision is probably too low to display anything nice.

Try:
cout << "Capacitance:" << setw(29) << scientific << capa << endl;
or
cout << "Capacitance:" << setw(29) << setprecision(12) << capa << endl;
That did it, I should have known that too. We discussed "scientific" last week. It's a great feeling seeing a program work, especially when its only my fifth one. Thanks for the help. I should be able to complete my next three assignments on my own.

Here's the working version:

#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;

double area = 1.20;
double dist = .04;
double diacon = 30;
const double elecon = 8.8542e-012;
double capa;

void printmessage()
{
cout << setw(44) << "Computation of Capacitance" << endl;
cout << setw(32) << "by" << endl;
cout << setw(38) << "James S. Bates" << endl;

}

int main()
{
double area = 1.20;
double dist = 0.04;
double diacon = 30.00;
double elecon = 8.8542e-012;
double capa;

printmessage();
cout << endl << endl;

cout << fixed << setprecision(2);
cout << "Enter the area of the plates (in meters squared):";
cin >> area;
cout << endl << endl;

cout << "Enter distance between the plates (in meters):";
cin >> dist;
cout << endl << endl;

cout << "Enter dielectric constant of the separating material:";
cin >> diacon;
cout << endl << endl;

cout << "Results:" << endl;
cout << setw(21) << "Area of the plates:" << setw(31) << "1.20 meters^2" << endl;
cout << setw(24) << "Distance between them:" << setw(26) << "0.04 meters" << endl;
cout << setw(22) << "Dialectric constant:" << setw(21) << "30.00" << endl << endl;
cout << setw(20) << "Electric constant:" << setw(36) << "8.8542e-012 farads/meter" << endl << endl;


capa = (area / dist) * diacon * elecon;
cout << fixed << setprecision(5);
cout << setw(14) << "Capacitance:" << setw(30) << scientific << capa << setw(7) << "farads" << endl;

system("PAUSE");
}
You're welcome!
Topic archived. No new replies allowed.