-
void, void function
I don't know if it's me or what but I can't understand the tutorials for the life of me on this site. Seems to me they are focused not on the newbie but for a higher level programmer...but I regress. I am trying to add a void function into my program. Just a couple of cout statements that send some lines of text to the screeen. No calcualtions or what not.
1. Do i need a special header file to use void functions?
2. What is the format for functions (outside of the main program)?
3. what is the proper syntax for function calls?
Thanks in advance, I just don't understand the tutorials they may as well be in Chinese. :mad:
-
Re: void, void function
1. No
2.
Code:
void functionName()
{
//statements
}
3. functionName();
-
Still having problems...getting all kinds of funky errors....any ideas?
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const double RED_RUG = 4.99;
const double GREEN_RUG = 5.99;
const double BLUE_RUG = 6.99;
int main(void)
{
char fileName[15];
string discount;
char customerName[25];
char rugColor;
char paymentMethod;
int roomLength;
int roomWidth; //variables
string rugDisplay;
string discountPercent;
int roomArea;
double endDiscount;
double paidDiscount;
double rugCost;
double carpetCost;
double trueCost;
cout<<"Enter file name for billing: ";
cin.getline(fileName,15);
cout<<setw(20)<<"Welcome to..."<<endl;
cout<<endl;
cout<<setw(40)<<">>>Phils Floors<<<<"<<endl; //business header-output to screen
cout<<endl;
cout<<setw(36)<<"Our Rugs Rock"<<endl;
cout<<endl;
cout<<"If you enter the customers name, type of rug purchased,"<<endl;
cout<<"room dimensions, and type of payment, this program"<<endl; //program explaination
cout<<"will calculate and print a bill to a file, Billing.dat"<<endl;
cout<<endl;
cout<<"Please enter the customers name: ";
cin.getline(customerName,25); //input customers name
cout<<endl;
cout<<"Please enter rug color (R)ed (B)lue (G)reen: ";
cin>>rugColor; //input rug color
cout<<endl;
if (rugColor == 'R' || rugColor == 'r')
{
rugDisplay = "RED";
rugCost = RED_RUG;
}
else
if (rugColor == 'B' || rugColor == 'b')
{
rugDisplay = "BLUE"; //set rugdisplay variable and cost of rug
rugCost = BLUE_RUG;
}
else
if(rugColor == 'G' || rugColor == 'g')
{
rugDisplay = "GREEN";
rugCost = GREEN_RUG;
}
else
{
rugDisplay = "Default";
rugCost = 6.99;
}
cout<<"Please enter the method of payment"<<endl;
cout<<"(C)ash (Z)ippy or (O)ther: ";
cin>>paymentMethod; //input payment method
cout<<endl;
if (paymentMethod == 'C' || paymentMethod == 'c')
{
paidDiscount = .10;
}
else if (paymentMethod == 'Z' || paymentMethod == 'z') //set paidDiscount variable to use in calculation
{
paidDiscount = .05;
}
else
{
paidDiscount = 1;
}
if (paymentMethod == 'C' || paymentMethod == 'c')
{
discount = "Cash and Carry";
discountPercent = "10% -- Cash and Carry";
}
else if (paymentMethod == 'Z' || paymentMethod == 'z')
{
discount = "Zippy Discount"; //set discount variables (output messages)
discountPercent = "5% -- Zippy Discount";
}
else if (paymentMethod == 'O' || paymentMethod == 'o')
{
discount = "No Discount";
discountPercent = "No Discount";
}
else
{
discount = "No payment type specified";
discountPercent = "No Discount";
}
cout<<"Enter the Length of your room: "; //input room length
cin>>roomLength;
cout<<endl;
cout<<"Enter the Width of your room: ";
cin>>roomWidth; //input room width
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
roomArea = roomWidth * roomLength; //calculate room area
carpetCost = roomArea * rugCost; //calculate rug cost
endDiscount = roomArea * rugCost * paidDiscount; //calculate discount
trueCost = carpetCost - endDiscount; //calculate carpet cost minus the discount (truecost)
if (paidDiscount == 1)
{
trueCost = carpetCost; //set "no discount" variables
endDiscount = 0;
}
cout<<setw(40)<<">>>Phil's Floors<<<<"<<endl;
cout<<endl; //output header
cout<<setw(36)<<"Our Rugs Rock"<<endl;
cout<<endl;
ofstream fout;
void text();
fout<<setiosflags(ios::fixed);
fout.open(fileName);
fout<<"Customer Name:"<<setw(27)<<customerName<<endl; //output customer name
fout<<endl;
fout<<"Rug Color:"<<setw(31)<<rugDisplay<<endl; //output rugcolor
fout<<endl;
fout<<"Payment Method:"<<setw(26)<<discount<<endl; //output payment method
fout<<endl;
fout<<setw(30)<<discountPercent<<endl; //output discount percent message
fout<<endl;
fout<<setprecision(2)<<"Discount: "<<setw(26)<<"$"<<endDiscount<<endl; //output discount
fout<<endl;
fout<<setprecision(2)<<"Rug Cost: "<<setw(25)<<"$"<<carpetCost<<endl; //output carpet cost (before discount)
fout<<endl;
fout<<setprecision(2)<<"Final Cost:"<<setw(24)<<"$"<<trueCost<<endl; //output carpet cost(final cost-after discount)
fout<<endl;
fout.close();
return 0;
}
void text()
using namespace std;
{
fout<<setw(40)<<">>>Phil's Floors<<<<"<<endl;
fout<<endl; //function output header
fout<<setw(36)<<"Our Rugs Rock"<<endl;
fout<<endl;
}
-
Here is an example of a function that happens to return nothing (void). I'd suggest learning each concept with a very simple program before applying it to your larger, more complicated program.
Code:
#include <iostream>
// Function prototype goes here above main().
void Display(int numToDisplay);
int main()
{
int myNum = 0;
// Pass any data that is needed by the function.
Display(myNum); // Pass a local variable.
Display(myNum+1); // Pass the result of an expression.
Display(2); // Pass a constant value.
Display(-1); // Pass a constant value.
myNum = 3; // Change the local variable.
Display(myNum); // Pass the modified variable.
return 0;
}
// Function definition. Note it is the same as it was prototyped earlier.
void Display(int numToDisplay)
{
// We now have a copy of the passed in data to use in the function.
if (numToDisplay < 0)
{
std::cout << "Error, " << numToDisplay << " is invalid input." << std::endl;
}
else
{
std::cout << "Number is " << numToDisplay << std::endl;
// Modifying numToDisplay does not change main()'s version.
numToDisplay = numToDisplay * numToDisplay;
std::cout << "Number squared is " << numToDisplay << std::endl;
}
}
Notice how the function is declared (see the prototype) before main(). Then it is called inside main(). And finally it is defined after main(). Make sure you are declaring and calling your text function correctly in addition to defining it.
Also notice how you have to pass the data to the function so that the function can use it. The function cannot use a variable that is declared inside a different function. For example, Display cannot use myNum because myNum was declared in main(). Instead, a copy of myNum's value must be passed to the Display function so that it can be used. In your case, your function cannot use fout, since fout was created inside of main(). You would either have to create fout inside of text(), or pass it in as a parameter (I recommend creating it inside and doing all your output in there, then passing in any data as function arguments).
Does that help?
-
You rock! that helped out a bunch :D