Wowo what was I thinking. For some reason I didn't see your #include iostream. Sorry about that.
But that is weird. By including the line usingnamespace std; it shouldn't complain. This isn't a solution to your problem, but something of note if you didn't know previously: The other way to include those is by simply referencing them at the top with
1 2 3
using std::cin;
using std::cout;
using std::endl;
That way you're only including what is necessary from the std namespace, and not the entire thing :) And you can just use "cin" or "endl" by itself, just like "using namespace std" allows you to do.
*EDIT: Oh, and I just made a new project space and compiled your code. Besides the fact that I needed to add a return 0; at the end of your main function for it to build w/o errors, it should be fine. After fixing that mine compiled error-free. What compiler are you using?
LOL.
can i share this story with you:
once upon a time, i wrote a code, compiled it and recieved an error.
the error was that i didn't include a particular header, so i included it.
i compiled the code, and it compiled correctly.
then i deleted the include directive i just added, recompiled the code:
it still work.
this thing never happened with me again, and up until this moment, i can't find a sensible answer for this phenomenon.
I've just started programming as an extra task around the office while on down-time or for extra hours after the typical work-day, but doing QA, I know how bad programming CAN be from the user stand-point with broken builds etc.
Also, just a board question. If I throw in the [ code ] [ /code ] can I toss any ole C++ in there and expect it to code properly?
If I understand you correctly, then the answer is yes. Here's what you could do instead of the "using namespace std" (I just adjusted the code you posted to demonstrate):
#include <iostream>
#include <string>
using std::cin; // added
using std::cout; // added
using std::endl; // added
int main()
{
char letter = 'A';
int integer = '0';
float dec = 0.0f;
cout << "Enter a letter ";
cin >> letter;
cout << "Enter an integer ";
cin >> integer;
cout << "Enter a float number ";
cin >> dec;
cout << endl;
cout << "Letter: " << letter << endl;
cout << "Integer: " << integer << endl;
cout << "Float: " << dec << endl;
return 0;
}
Whenever the compiler now sees cin, cout, or endl it knows that want to use it from the std namespace.
These are called "using declarations" where a "using declaration" allows an individual name to be used (ex. cin). The statements that follow the form "using namespace <some_namespace>" is a "using directive" and allows use of any and all names in that namespace (ex. cin, cout, endl, and everything else in std).
Compiler problem maybe? I tried it on Code::Blocks and it works perfectly. Also, why did you declare the values of 'integer' 'letter' and 'float'? I don't think that they should be pre assigned when you ask for a user-input.
Compiler problem maybe? I tried it on Code::Blocks and it works perfectly. Also, why did you declare the values of 'integer' 'letter' and 'float'? I don't think that they should be pre assigned when you ask for a user-input.
It's generally good practice to initialise your variables. Yes, you may be writing some code to set the value of your variables to something before they're ever used, but who knows what changes you're going to make in the future? Uninitialised variables are one of those ways in which bugs creep into your code as you modify and maintain it.
If nothing else, it's just a good habit to get into as you're learning.
Although, as Fredbill points out, int integer = '0'; isn't doing what you probably intended it to do.
i don't know what did he mean to do, but this expression is perfectly valid, and i use such expression in some of my projects.
int integer = '0'; means: define variable integer as int and initialize it with this value:
( the ASCII code witch corresponds to this letter ('0') ).
this expression assign the value (0x30) to integer.
you might consider using such expression in a string parser, where you have ascii digits and want to transform them to binary values, this expression gives an idea on what is your point.
edit:
example: i = ch - '0';