C++ question

<p>hey guys its me again...the neophyte programmer...well i picked up c++ and have been reading up on it and have found that it can be one of the most frustrating things ever! neways can someone tell me why this program wont run its basic as hell....it says there is sumthing wrong with line 10...the first cout....</p>

<p>// program that converts F to C</p>

<h1>include <iostream></iostream></h1>

<h1>include <string></string></h1>

<p>float degreesF;
float degreesC;
main()
{
cout << "Enter degrees in Farenheit: ";
cin >> degreesF;
float degreesC = (degreesF * (5.0 / 9.0) - 32.0;
cout << "the degrees in Celsius is: ";
<< degreesC;
return (0);
}</p>

<p>how about adding this underneath #include <string></string></p>

<p>using namespace std;</p>

<p>also, I think you are misunderstanding the concept of a ;(semicolon). You don't put it at the end of a line, but rather then end of a statement. So you need to put cout before << degreesC.</p>

<p>g2g sleep, but post back if you have other questions.</p>

<p>PS: it should be int main() not main() - which was old school C.</p>

<p>If I'm not mistaken, you need to add "using namespace std;" (without the quotes) right after the two #include statements. However, I usually program in Java and have only looked into C++, so I might be wrong.</p>

<p>Edit: Argh, you beat me to it, Sagar.</p>

<h1>include <iostream></iostream></h1>

<h1>include <string></string></h1>

<p>using namespace std; // I forgot why you need this, but in order to use
// #include <xxx> without the ".h":
// #include <xxx.h>, you'll need this statement</xxx.h></xxx></p>

<p>int main() // main() is a function, so it has to have a return type.
// notice you had a "return 0;" at the end, it's actually
//returning the integer value "0" to indicate success!
{
// declaring global variables is generally unhealthy
float degreesF;
float degreesC;</p>

<p>cout << "Enter degrees in Farenheit: ";
cin >> degreesF;
degreesC = (degreesF - 32.0) *(5.0/9.0); // don't need to say "float degreesC", that's redundant. and your formula was wrong.
cout << "the degrees in Celsius is: " << degreesC;
return 0;
}</p>

<p>use put</p>

<p>using namespace std;</p>

<p>because all of those headers are under std. Otherwise, you would do</p>

<p>cout << ...</p>

<p>as</p>

<p>std::iostreams::cout <<</p>

<p>i think</p>

<p>ahhh i see...okay well vector i cut and pasted your version and it worked fine so i see i must have needed the <em>using namespace std</em>...i forgot to add this because i had been looking through an older c++ book which was copyrighted in 1995 and i guess they didnt use that back then...it used however in my newer book <em>c++ for dummies</em>...my question is though to sagar about this statement....</p>

<p><strong><em>also, I think you are misunderstanding the concept of a ;(semicolon). You don't put it at the end of a line, but rather then end of a statement. So you need to put cout before << degreesC.</em></strong></p>

<p>what does that mean???? what do you mean by you need to put cout before << degrees C??? i think i did? i just don't see where your coming from...thanks!!</p>

<p>**** one final thing!!! i know how to get the program to display results till the user strikes a key it is ....system("PAUSE") i believe....however vectors program doesn't have this and it still waits for the user to strike a key to continue...why does it do that? for my program if i don't put the system("PAUSE") the program flashes for a split second and then disapears and you can't see anything</p>

<p>oh yeah also..for vector or anyone else...how come you didnt use the '
' character or the endl statement after the cout...i always thought that it was neccesarry for whatever reason...if someone wouldn't mind explaining why it is or isn't would be very helpful!!!</p>

<p>"<strong><em>also, I think you are misunderstanding the concept of a ;(semicolon). You don't put it at the end of a line, but rather then end of a statement. So you need to put cout before << degreesC.</em></strong></p>

<p>what does that mean???? what do you mean by you need to put cout before << degrees C??? i think i did? i just don't see where your coming from...thanks!!"</p>

<p>I'm talking about what you originally posted:</p>

<p>cout << "the degrees in Celsius is: ";
<< degreesC;</p>

<p>you don't need the first semi colon.</p>

<p>you should have it as either:
cout << "the degrees in Celsius is: " << degreesC;</p>

<p>or </p>

<p>cout << "the degrees in Celsius is" ";
cout << degreesC;</p>

<p>also, when possible, use std::endl over "
".</p>

<p>Also, I tried vectors code, and it doesn't pause. Unless you are executing directly from command prompt, this shouldn't happen.</p>

<p>An alternative to system("PAUSE") is to do</p>

<p>cin >> <insert some="" variable="" you="" don't="" need="" anymore="">;</insert></p>

<p>this will require that the user input some kind of number or character, etc, and then the program will exit. This is more platform independent, because what your system("PAUSE") is doing is entering PAUSE onto the console, which is windows specific. Therefore, you won't get the same results on linux.</p>

<p>ahhh i see! hmmm i wonder why system("PAUSE") was never mentioned in my earlier book?! that is pretty dumb because it wasn't until got this newbook and read about system("PAUSE") that i could start viewing my programs!!! but thanx alot!!!!</p>

<p>
[quote]
Also, I tried vectors code, and it doesn't pause. Unless you are executing directly from command prompt, this shouldn't happen.

[/quote]

Sagar, that depends on the compiler (and compiler options) you use. Some compilers, microsoft included, have a commandline switch which adds a pause at the end. Also, my technique for other compilers is to use getch(); at the very end.</p>

<p>yeah just tell us what compiler u are using.. if u are using bloodshed, then :( but if its visual 6+ then fix those things the people said and u should be fine. (btw i dont goto MIT)</p>

<p>wats so bad about bloodshed?</p>

<p>also another dumb question thats been bothering me for sometime...i'm having difficulty comprehending the RETURN and DO functions....i know return is supposed to store the results of the function? but for what? can someone write a basic program that uses it so i can better understand it...same with the the DO function...thanks!</p>

<p>(Note: I haven't coded for a year or two, so I may be wrong. Feel free to correct me. I was never much of a programmer to begin with (compilers hate me). ^_^)</p>

<p>When a function returns a value, that's usually either the end result or an error code. The type of value it returns is declared at the beginning of the function (for example, "int main()" will return an integer).</p>

<p>Example:</p>

<p>float multiply(factor0, factor1)
{
float product = factor0 * factor1;
return product;
}</p>

<p>Yeah, it's a useless function, but it hopefully gets the point across. If I called it later with the following:</p>

<p>float product62 = multiply(6, 2);</p>

<p>Then product62 would have the value 12. You'll often see main() return 0 because 0 is the error code saying that everything went normally.</p>

<p>As for do: I always think of it as one part of a do...while loop (basically a loop whose contents always execute at least once). For example:</p>

<p>do
{
counter++;
} while(counter <= 42);</p>

<p>would increment the counter and then check to see if it was <= 42. If it was, then it'd increment again and check, etc.</p>

<p>You basically just have to memorize what Loops do but the dude above me pretty much said what DO does...
I dont like bloodshed cause you have to add some stuff to your code and it gets annoying. I prefer Borland/Visual/GCC</p>

<p>hey MineIsDifferent, know any Java?</p>

<p>If you are working on Windows, use the Microsoft Compilers, as they are arguably the most standards compliant compilers out there. And price is not argument, since you can get Visual C++ Express Beta 2, Visual Studio 2005 beta 2, or Visual C++ Professional Optimizing Compiler Toolkit for free, which is awesome.</p>

<p>PS: Does anyone know why free food taste better than if you had to pay for it? I mean, its still the same food, its possibly a psychological thing?</p>

<p>nope i dont know java but im thinking im gonna leave c++ alone and try to pick it up...it was good to get a grasp on C++ to learn how the computer languages work...but im thinking java would be more fun and useful?? as in regards to a someone who is just doing this for fun and get a better grasp on computer technology/programming...any opinons?</p>

<p>sagar_indurkhya: does the free one come with IDE?
...your name sounds indian</p>

<p>Mine is different in answer to your original program, you should put # include <iostream.h>.Dont forget the .h because it indiactes that youre including a header file,and it also contains the cout fnc.</iostream.h></p>

<p>2)You put a ; at the end of a statement not at the end of a line.</p>

<p>3) Try using void main() its a lot simpler than returning some value at the end. Also write getch(); if you use void main(), so that you can view the O/P.</p>

<p>4)You're right this is a really simple program...:)</p>

<p>Hey Gandhiji. Yeah I'm from India. Why did you use Gandhiji as your name? Are you from India. </p>

<p>No it doesn't come with the IDE. No such thing as a free lunch right?</p>

<p>I would advise going on ebay, buying an academic license to Visual C++ 2003, and voila, first class IDE+Compiler at around $50.</p>

<p>Havaldaar: <xxx.h> was declared not as proper as <xxx> by the C++ Standards Committee, although both are legal. </xxx></xxx.h></p>

<p>How is </p>

<p>void main()
{
}</p>

<p>easier than </p>

<p>int main()
{
return 0;
}</p>

<p>half the compilers won't compile that, as it isn't legal C++.</p>

<p>void main(void)
{
}</p>

<p>is perfectly legal IF you put at least ONE executable statement in there.. you can't compile nothing.. though the OTHER half of the compilers do produce perfect exes out of this "nothing" :p</p>