Thursday, July 9, 2009

C++ Errors?

Please debug this. I'm a beginner in C++ so I'm having problems. My compiler tells my I have 9 errors but it won't be specific enough to tell me whats wrong!!! Here is the source code:





























//


// Beginning template for perimeter


#include %26lt;cstdio%26gt;


#include %26lt;cstdlib%26gt;


#include %26lt;iostream%26gt;


using namespace std;





int main( int nNumberofArgs , char* pszArgs[])


{


// Adjacent-to-variable assignment





cout %26lt;%26lt; "Length of Adjacent?"


int adjacent;


cin %26gt;%26gt; adjacent;





// Opposite-to-variable assignment





cout "Length of Opposite?"


int opposite;


cin %26gt;%26gt; opposite;





// Hypotenuse-to-variable assignment





cout "Lenght of Hypotenuse?"


int hypotenuse;


cin %26gt;%26gt; hypotenuse;





// Calculation of perimeter





int perimeter;


perimeter = adjacent + opposite + hypotenuse;





// Display of value





cout %26lt;%26lt; "Perimeter value is:";


cout %26lt;%26lt; perimeter %26lt;%26lt; end1;





//Wait for user to copy answer


system( "Pause");


return 0;


}

C++ Errors?
It would help people if you wrote out the error messages too.





Anyway, I agree with the previous poster that you're missing semicolons. You're missing 3 of them, but the compiler reads and translates your program line by line, so you will end up with more than 3 errors.


Also, your 'end1' near the end of the program should be 'endl'.





I just want to add a couple things that you can do.





1) Don't allocate variables before you use them. This is bad form, and depending on how intelligent the compiler is, it might even bloat the machine code that is produced. For example, right after the '{' after the line with main(), you should declare your variables there. Like this:





int main( int nNumberofArgs , char* pszArgs[])


{


int adjacent;


int opposite;


etc.





This is also done for any separate procedures that you will make in the future.


Even back in the 80's when home computer users programmed with the built-in BASIC language, some people used to grouse about declaring your variables ahead of time.





2) One other thing that is possible (although not necessary) is to concatanate your cout messages and your cin statements. Here's an example:





cout %26lt;%26lt; "Input Adjacent, Opposite, and Hypotenuse\n";


cin %26gt;%26gt; adjacent, opposite, hypotenuse;





Since cin is given 3 integer input parameters, it will expect the user to input 3 lines of numbers. The cin line above is equivalent to:





cin %26gt;%26gt; adjacent;


cin %26gt;%26gt; opposite;


cin %26gt;%26gt; hypotenuse;
Reply:Make sure to add a semicolon (;) after each of the statements.


If you're using turbo c++ then debugging via F7 is a strong %26amp; easy way. Try it from the very next time. You'll be helpful.
Reply:Add a semicolon after each cout statement.





e.g. Change: cout %26lt;%26lt; "Length of Adjacent?"





TO:





cout %26lt;%26lt; "Length of Adjacent?";





Also, you forgot the %26lt;%26lt; in the following cout statements:





cout "Length of Opposite?"





cout "Lenght of Hypotenuse?"
Reply:end1 should be endl
Reply:hey sorry this isnt the answer but what Dev C++ should I download, there are alot, is it free, please goto my account and answer!!!


No comments:

Post a Comment