Tuesday, July 28, 2009

Basic C++ help using Microsoft Visual C++ 2005 Express?

i am encountering 2 errors upon building when using the following code





#include%26lt;iostream%26gt;


#include "stdafx.h"


int main()


{


cout%26lt;%26lt;"*"%26lt;%26lt;endl;


cout%26lt;%26lt;"**"%26lt;%26lt;endl;


cout%26lt;%26lt;"***"%26lt;%26lt;endl;


cout%26lt;%26lt;"****"%26lt;%26lt;endl;


cout%26lt;%26lt;"*****"%26lt;%26lt;endl;


cout%26lt;%26lt;"****"%26lt;%26lt;endl;


cout%26lt;%26lt;"***"%26lt;%26lt;endl;


cout%26lt;%26lt;"**"%26lt;%26lt;endl;


cout%26lt;%26lt;"*"%26lt;%26lt;endl;


return 0;


}








any ideas what i am doing wrong? i am getting these errors


error C2065: 'endl' : undeclared identifier and


error C2065: 'cout' : undeclared identifier








is there a special thing i need to "#include" ? apparently, using namespace std; is useless with C++Express 2005. i dunno?

Basic C++ help using Microsoft Visual C++ 2005 Express?
one of two things:





1 change the line #include%26lt;iostream%26gt; to


#include%26lt;iostream.h%26gt; or #include "iostream.h"





2 use #include "stdio.h" or #include %26lt;stdio.h%26gt;
Reply:i think you forgot to put a namespace declaration.


using namespace std;





also, you might have to include %26lt;iomanip%26gt;, some manipulators for iostreams need it.
Reply:does it say what line the error is on?





what are you trying to do? output all those stars? thats easy.





but idk abotu stdafx.h ive never used that portion of your code.





and personally i prefer not to use the "%26lt;%26lt;end" way of breaking a new line so heres what i think u should do





#include %26lt;iostream%26gt;


using namespace std;


int main(){





cout%26lt;%26lt;"*\n";


cout%26lt;%26lt;"**\n";


cout%26lt;%26lt;"***\n";


cout%26lt;%26lt;"****\n";


cout%26lt;%26lt;"*****\n";


cout%26lt;%26lt;"****\n";


cout%26lt;%26lt;"***\n";


cout%26lt;%26lt;"**\n";


cout%26lt;%26lt;"*\n";


return 0;


}





i dont think your copying and pasting your code correctly because if it says cout isnt a declared identifier your either missing a ; at the end of one of your lines, or your missing a "" or you may have accidently typed cout%26gt;%26gt; instead of cout%26lt;%26lt; or you may have only 1 %26lt; instead of 2.





that should cover all your mistakes.
Reply:Looks good to me. Just a guess... there doesn't appear to be a space between the #include and the %26lt;iostream%26gt;
Reply:declare the var?





ie: endl = 0 ?
Reply:an indeclared identifier is an undeclared variable you have no variable declarations


in regular C++ code this would be written as follows


#using


using namespace System;


int main()


{


//declare variables


CODE GOES HERE FOR VARIABLES example below


double exNum1=0.0;


double exNum2=0.0;


double exTotal =0.0;





// get input


Code Goes Here again example below


Console::Write("Get num 1: ");


exNum1= Convert::ToDouble(Console::ReadLine());


etc.





//do some sort of processing


CODE GOES HERE example below


exTotal = exNum1 + exNum2;





// display result


Code goes here example below





Console::Write("The Total is {0}: exTotal.ToString());





//end main


return 0;


}
Reply:#include "stdafx.h"





should be





#include %26lt;stdafx.h%26gt;





and you need to include the windows.h or win.h, somthing like that.


No comments:

Post a Comment