Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Need to fix 2 EASY problems in C++.


  • Please log in to reply

#1
magusbuckley

magusbuckley

    Member

  • Member
  • PipPipPip
  • 626 posts
Hello:

A guy at work has a son taking C++ in highschool. He asked me if I could look at the programs. In college, I took QBasic, Cobol, and RPG. I had hoped that if I looked at these two programs for him I might could just glance them over and find something out of the ordinary. Unfortunately, it looks as if I don't have a clude. These are two VERY SIMPLE problems so hopefully some one can look over them and let me know what you think.

Problem 1 - Rectangle 2

#include <iostream>
using namespace std;

int main();
{ int Width, Length, Area;
cout<<"Please insert the Width"<<endl;
cin>>Width;
cout<<"Please insert the Length"<<endl;
cin>>Length;
cout<<"The Area is:"<<Area<<endl;

system("PAUSE");
return 0;

}


**Error Report***
------ Rebuild All started: Project: Rectangle2, Configuration: Debug Win32 ------
Deleting intermediate and output files for project 'Rectangle2', configuration 'Debug|Win32'
Compiling...
Rectangle2.cpp
c:\documents and settings\compaq_owner\my documents\visual studio 2005\projects\rectangle2\rectangle2\rectangle2.cpp(5) : error C2447: '{' : missing function header (old-style formal list?)
Build log was saved at "file://c:\Documents and Settings\Compaq_Owner\My Documents\Visual Studio 2005\Projects\Rectangle2\Rectangle2\Debug\BuildLog.htm"
Rectangle2 - 1 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========


Problem 2 - Area of a trapazoid


//used to stream input and output
#include <iostream>
//used so that std:: isn't used repeatedly
using namespace std;


//starts flip void(unsure)
int flip (void);
//starts main application
int main()

{
   cout<<"Now in the flip function" << endl;
   return 1;


//Names all of the containment files that will be used
{double baseOne, baseTwo, baseSum, completebaseSum, Altitude, Area;

//Will print the following text on screen
cout<<"What is the length of the first base of the Trapezoid?"<<endl;
//stores data entered by user in baseOne containment file
cin>> baseOne;

//Will print the following text on screen
cout<<"What is the length of the second base of the Treapezoid?"<<endl;
//stores data entered by user in baseTwo containment file
cin>> baseTwo;

//the formula used to combine the two bases and divide it
baseSum = (baseOne + baseTwo);
completebaseSum = (baseSum / 2);

//Will print the following text on screen
cout<<"What is the altitude of the Trapezoid?"<<endl;
//stores data entered by user in Altitue containment file
cin>> Altitude;

//formula used to solve the area of the trapezoid
Area = (Altitude * completebaseSum);
//Will print the following text on screen
cout<<"The area of the Trapezoid is:"<< Area<< endl;



system("PAUSE");

//closes console
return 0;
}


I think his dad wrote the second program for him. I'm not sure why, but he didn't e-mail me any details about the second program. His dad mentioned at work, though, that one of the programs wouldn't compile because it was having trouble with the #include <iostream> line.

Any information you can provide will be greatly appreciated.

Thanks,

Magus
  • 0

Advertisements


#2
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP

Problem 1 - Rectangle 2

Many problems here. The student really needs to go back to the text.

1) The function main() is being defined here as well as declared, so there should be no semicolon after int main().
2) There is no assignment to the Area variable, it is never being computed.
3) system() I believe is defined in <cstdlib>, not <iostream>.

This code will work (note the bolding):

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
int Width, Length, Area;
cout<<"Please insert the Width"<<endl;
cin>>Width;
cout<<"Please insert the Length"<<endl;
cin>>Length;
Area = Length * Width;
cout<<"The Area is:"<<Area<<endl;

system("PAUSE");
return 0;

}


Of course, this only works on integers.

The second program is WAY overcommented. Bad style. If the line is self-explanatory, don't double the code length by commenting it. I know what #include <iostream> does. I know what using namespace std; does. Don't comment what printing something to cout will do: if the reader doesn't know that, the reader needs more than a comment line to help him/her.

Then there are syntax errors. The flip function is not defined, and is never called anyway. The main function is malformed. There's also an overuse of variables.

This is sufficient:

int main()
{

double baseOne, baseTwo, Altitude, Area;

cout<<"What is the length of the first base of the Trapezoid?"<<endl;
cin>> baseOne;

cout<<"What is the length of the second base of the Trapezoid?"<<endl;
cin>> baseTwo;

cout<<"What is the altitude of the Trapezoid?"<<endl;
cin>> Altitude;

Area = (Altitude * (baseOne + baseTwo) / 2.0);

cout<<"The area of the Trapezoid is:"<< Area<< endl;

system("PAUSE");
return 0;

}


Notice I used 2.0 instead of 2, which has the effect of casting the value of 2 to a double.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP