Reputation: 21
Ok, so my assignment is to build on an existing code from the book. I have to add the 3rd box and then calculate the total for all 3 boxes. Here is what I've written so far, but it will not compile. Please help me find the problem. Thanks.
The program I'm using is MS Visual C++ and the complile error I get is
error C2447: '{' : missing function header (old-style formal list?)
referring to the { after my int Total_Volume line
// Structures_and_classes.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
class CBox // Class definition at global scope
{
public:
double m_Length; // Length of a box in inches
double m_Width; // Width of a box in inches
double m_Height; // Height of a box in inches
};
int main();
int Total_Volume;
{
CBox box1;
CBox box2;
CBox box3;
double boxVolume = 0.0; // Stores the volume of a box
box1.m_Height = 18.0; // Define the values
box1.m_Length = 78.0; // of the members of
box1.m_Width = 24.0; // the object box1
box2.m_Height = box1.m_Height - 10; // Define box2 Box 2 H = 8
box2.m_Length = box1.m_Length/2.0; // members in Box 2 L = 39
box2.m_Width = 0.25*box1.m_Length; // terms of box1 Box 2 W = 6
box3.m_Height = box1.m_Height + 2; // Define box3 Box 3 H = 20
box3.m_Length = box1.m_Length - 18; //members in Box 3 L = 50
box3.m_Width = box1.m_Width + 1; //terms of box1 Box 3 W = 25
// Box1
boxVolume = box1.m_Height*box1.m_Length*box1.m_Width;cout << endl;
<< "Volume of box1 = " << boxVolume;
cout << endl;
// Box 2
boxVolume = box2.m_Height*box2.m_Length*box2.m_Width;cout << endl;
<< "Volume of box2 = " << boxVolume;
cout << endl;
// Box 3
boxVolume = box3.m_Height*box3.m_Length*box3.m_Width;cout << endl;
<< "Volume of box3 = " << boxVolume;
cout << endl;
//Calculate Total Volume
Total_Volume = (box1.m_Height*box1.m_Length*box1.m_Width)+
(box2.m_Height*box2.m_Length*box2.m_Width)+
(box3.m_Height*box3.m_Length*box3.m_Width);
return 0;
}
Upvotes: 1
Views: 660
Reputation: 882756
Change:
int main();
int Total_Volume;
{
to:
int main()
{
int Total_Volume;
That will fix your immediate problem, although I suspect you'll have a few more questions today :-)
The actual problem with your current code is that it defines a prototype for main, followed by a file-level variable, followed by a naked brace, which is why it's complaining about a missing function header.
You may also want to consider changing your main
function to one of:
int main (int argc, char *argv[])
int main (void)
(probably the second in your case) as these are the two forms required to be supported by the ISO C standard. Implementations are free to accept others if they wish but I usually prefer my code to be as standard as possible. The reason I say "may" is because it's not necessarily required to get your code working, more of a style thing.
Upvotes: 8
Reputation: 40871
It looks like you mixed up a couple lines:
int main();
int Total_Volume;
{
CBox box1;
CBox box2;
CBox box3;
looks like it should be:
int main() {
int Total_Volume;
CBox box1;
CBox box2;
CBox box3;
Upvotes: 1
Reputation: 755094
You probably intended to write:
int main()
{
int TotalVolume;
You can't start a block where you did.
You have declared that there will be a function main()
, and that there's a global variable TotalVolume, but the anonymous block following these is not allowed.
Upvotes: 0
Reputation: 6297
Goodness me. Please check the preview and at least make sure your question looks like prose plus some code. Edit okay, let's assume that was a glitch that's been sorted out. The rest of my answer still holds.
A little bit of digging suggests the first problem is that you don't have a properly defined main() function. This is the first thing one learns about C++ typically, so in the first instance get that right.
Good luck.
Upvotes: 0
Reputation: 24395
int main();
int Total_Volume;
{
should be
int main()
{
int Total_Volume;
Upvotes: 3