Friday, July 31, 2009

C++ Beginner! Just a tiny question!?

ok, you experienced programmers... dont laugh at me! my programing experence stopped when my commodore 64 broke! I have chose to take this last week before christmas and teach myself as much about c++ as possible. Today is my first day and I am stumped on a point in chapter 3 of my book dealing with loops.I understand the loops but i am just experimenting and trying to make a program different than the tutorial. All I want to do is output 2 variables on the same line.


cout %26lt;%26lt;first variable%26lt;%26lt; %26lt;%26lt;then the second variable%26lt;%26lt;;


get my point?


Also id like to know how to do this..


cout %26lt;%26lt;"some text"%26lt;%26lt;first variable%26lt;%26lt; %26lt;%26lt;"some text"%26lt;%26lt; %26lt;%26lt;then the second variable%26lt;%26lt;"some text";


here is my code


#include %26lt;iostream%26gt;





using namespace std;





int main()





{


int y=0;





for (int x=1;x%26lt;4097;x=x*2) {


y=y++;


cout%26lt;%26lt;y%26lt;%26lt; %26lt;%26lt;x%26lt;%26lt;endl; //%26lt;----- Problem Line!


}


cin.get();


return 1;


}





/* Output sould look like this


1 1


2 2


3 4


4 8


5 16


etc...*/





Any advice appreciated! Thanks

C++ Beginner! Just a tiny question!?
There should only be one %26lt;%26lt;. You put 2 that causes an error.


If you want to add text its the same as what you did above. Like this: cout %26lt;%26lt; "text" %26lt;%26lt; x %26lt;%26lt; endl;


Just enclose the text in double quotation marks and they will display as text.


Note: endl is end line or new line equivalent to \n in C.


use return 0 to notify the OS that the program is successfully executed. Usually the compiler will return 0 automatically when it reaches the closing bracket of main. Return 1 is used to return the number 1 to the operating system so it is not necessary and useless.
Reply:cout %26lt;%26lt; y %26lt;%26lt; x %26lt;%26lt; endl.





You got too many %26lt;%26lt;'s
Reply:int main()





you defined your main program to return an int


value, that is why your compiler is "complaining".. ^^





i don't know if it you change it to "void main()",


and delete the "return 1;" will work..


btw, im using code::blocks. it's free and more


compliant to standard C/C++, i guess.. ^_^v





btw, you should not be using "return 1;"..


it should be "return 0;", coz any number


returned by a console program to the os %26gt; 0


is interpreted(by the OS) as error value..





peace out.. ^_^v


No comments:

Post a Comment