So I just picked up a book on C++ probably an hour ago, and I seem to be having problems already. So this is my code, I was wondering if anybody could tell me what I can do to fix the error in the line of code where it says (float)? Thanks in advance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Listing 2.2 using std::cout
#include <iostream>
int main()
{
std::cout << "Hello there.\n";
std::cout << "Here is 5: " << 5 << "\n";
std::cout << "The manipulator";
std::cout << "writes a new line to the screen.";
std::cout << std::endl;
std::cout << "Here is a very big number:\t" << 7000;
std::cout << std::endl;
std::cout << "Here is the sum of 8 and 5:\t";
std::cout << (8+5) << std::endl;
std::cout << "Here's a fraction:\t\t"
std::cout << float (5/8) << std::endl;
std::cout << "Ghosty1212 is a C++ Programmer!\n";
return 0;
}
You need to have 5 or 8 to be float, the way you are doing it is like this: float( int(5) / int(8) )
so 5/8 returns an integer and the result should be 0
if you change it in float( 5 ) / 8 the result will have float as type as this is the bigger type in the expression.
To make it shorter you could type 5.0f / 8 'f' means 'float' if you write only 5.0 it will have as type double
PS: If you want to see a very big number try -1ull
All right, thanks. Now it presents this weird error code that I can't seem to fix... I understand what it's asking me to fix, but I try to edit it and nothing happens... Here's the error code:
15 C:\Documents and Settings\Owner\My Documents\C++ Projects\usingcout.cpp expected `;' before "std"
Well now there is another problem... lol.... When I compile the code and then run it the Command Box screen just flashes.... And that's all. Is anything wrong with my compiler type? Or something like that?
Thank you both, I used the system ("PAUSE") command and it worked out well... Thank you very much! But I used to be able to do it without it, and now it does that... But oh well, I'm going to use this code from now on. Thank you again!
// Listing 2.2 using std::cout
#include <iostream>
int main()
{
std::cout << "Hello there.\n";
std::cout << "Here is 5: " << 5 << "\n";
std::cout << "The manipulator";
std::cout << "writes a new line to the screen.";
std::cout << std::endl;
std::cout << "Here is a very big number:\t" << 7000;
std::cout << std::endl;
std::cout << "Here is the sum of 8 and 5:\t";
std::cout << (8+5) << std::endl;
std::cout << "Here's a fraction:\t\t";
std::cout << float (5/8) << std::endl;
std::cout << "Ghosty1212 is a C++ Programmer!\n";
return 0;
}