Input/Output

Hey guys, trying input and output stuff here and I can't quite find what the issue is. The errors I'm getting are:

in line 12 Undeclared use of function 'cout'
in 13 Undeclared use of 'cin'
in 21 undeclared use of "endl" - Wat is issue and hao iz stop?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

using namespace std;

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;
    
}
Last edited on
Try this??
 
#include <iostream> 


http://www.cplusplus.com/reference/ostream/endl/
Last edited on
The iostream is already there in the original code.

Also, that part about endl; wasn't really about that function, it was more about what is the undeclared issue and how do I get it to bugger off.
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 using namespace 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?
Last edited on
i don't know what IDE you're using, however i copied your code and pasted it into msv10:
it worked like magic.
there's no problem in this code.
Yeah, oddly enough I recopied it from here after seeing these comments and tried it again and it's working? Dafuq. Oh well, that solves that.

Enigma, you were saying that if I use "using" and inputs etc, that I can then code it out the same way as I would if I was using namespace?
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.
That is actually completely hilarious.

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?
apparently, yes.
it just doesn't indent any code you insert, so better write your code in an IDE, then copy/paste it inside code tags.
@FrankFoxJaeger:

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):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#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).
Last edited on
closed account (N36fSL3A)
int integer = '0';
What the hell?

Don't you mean:
int integer = 0;
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.
try this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    char letter;
    int integer;
    float dec;
    
    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;

    cout << endl;
    system("PAUSE");
	return 0;
} 


i'm also new at this so... that's the best i can do... :)
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.
@Fredbill30:
int integer = '0';
What the hell?

Don't you mean:
int integer = 0;


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';
Last edited on
Topic archived. No new replies allowed.