Can anyone tell me what the heck is wrong with this basic code...
#include <iostream>
namespace std;
int main()
{
cout << "testing..." << endl;
return 0;
}
from what little i know this should work? but the compiler is telling me that cout and endl are undeclared identifiers which is what I thought the #include <iosteam> was supposed to do?
None.
nevermind I was browsing around and figured out that I was missing "using" in front of "namespace std;"
None.
Don't you need to add something to the iostream, like <iostream.h>, or is that just to keep a program smaller?
None.
I don't know, I was told it wasn't needed so I didn't bother adding the .h Although now I have another question lol.
when using:
cin <<
I've forgotten what to place to the right when I want to store whatever was entered to a variable?
None.
I was going to suggest adding "using", but I'm way too much of a noob and didn't want to make a fool out of my self. So this might be wrong, if not, oh wellz:
It's just cin >> your_variable;
None.
Basically it is that you point it which way it is going. If it is going from the variable to something else, you point it away from the variable, to the left. If it is going to store a value into the variable, point it toward the variable, to the right.
As for iostream.h, that is an old version of it, and it does not even exist on some newer compilers.
None.
it is called a header file, it basically adds the code in the header file to the front of your program, to give support for commonly used functions, so you do not have to rewrite those function every time you make a program.
None.
Yeah, that is indeed what it is.
Also, a header file doesn't need any certain file extension - it can even have none at all (added that sentence in case you were replying to my post).
None.
Its already been said: "Add
using in front of
namespace"
You don't need the < > around the .h file (That was old C++ code.. Newer versions of C++ don't require you to put that) but I do think you would still need to have the ".h" in there if you didn't put the < > around iostream...
A header file
IS a file extention... Its a .h (in which h stands for "header").. Just like .obj, .cpp, .c, .exe, .dll, ect.... are all file extentions...
You can even make your own header files, too... Like for example, if you had 20 classes that you didn't want to define within your main source code, you could simply make a "header file" and bring it in that way.. For example:
#include <iostream>
#include <string>
#include "twentyclasses.h"
using namespace std;
int main()
{
Username names;
char letter;
cout << "Please type ONE letter: ";
cin << letter;
names.dosomething();
cout << "Thanks!";
return 0;
}
Username is where I defined my class within "twentyclasses.h"
names is the name I give my
object.
names.dosomething(); is a function within the
Usernames class that I call upon in my source code (where
names is the
objects name, and
dosomething(); is the function that I created within my class
Usernames)
Since dosomething(); is declared within the Usernames class within my "twentyclasses.h" header file, it should work out fine...
I'm not that far into OOP, I'm more of a conditional programmer atm...
Talk to Clokr_ if you need any help
None.
Alright well at the moment I'm trying to make a little triangle area calculator just to screw around with...
#include <iostream>
using namespace std;
int main()
{
double base,
area,
height;
cout << "Enter base of triangle: " << '\n';
cin >> base >> '\n';
cout << "Enter height of triangle: " << '\n';
cin >> height >> '\n';
cout << "Base is " << base
<< "\n Height is " << height;
area = 0.5 * base * height;
cout << "Area of triangle is " << area << '\n' << endl;
return 0;
}
However when I try to compile it gives me this error...
error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'char' (or there is no acceptable conversion)
at both of my cin >> lines
None.
The cin statement is just to retrieve data. You don't need the >> '\n'.
?????
Cool, thanks and I have another question lulz. How do I write out a number to an explonent. I tried base ^ power and that didn't work.
None.
Cool, thanks and I have another question lulz. How do I write out a number to an explonent. I tried base ^ power and that didn't work.
There's no power operator, instead there's a power function (called pow) defined inside the math header. You'd have to include the math.h header to use it.
However, for base-2 powers bitshifts are a lot faster than the pow function. And for low integer exponents a loop would be faster too.
?????
I've taken a course for C++ as a correspondence course for school. On mac, it is a little different, in xCode, I get errors if I don't have <iomanip.h>, I don't know why it is like that though. I should think it would work both the same on windows, and on macs. I make an awesome program, then my friends can't use it because it doesn't use "void main()" and doesn't return 0; because it's better on mac
.
I keep pronouncing cin, and cout as "sin" and "kout", but its actually "c in" and "c out" as in the language "c" and input, and output.
cin>>
cout<<
I forgot how to do the pow() function, I think it's pow(#1, #2) where #1 is to the power of #2 or something.
I haven't played with C++ in a month.
None.
You don't need the < > around the .h file (That was old C++ code.. Newer versions of C++ don't require you to put that) but I do think you would still need to have the ".h" in there if you didn't put the < > around iostream...
A header file IS a file extention... Its a .h (in which h stands for "header").. Just like .obj, .cpp, .c, .exe, .dll, ect.... are all file extentions...
That is all wrong. There are two versions of the #include statement:
#include <filename>
#include "filename"
One or the other needs to be put around the file name and they have different meanings. The first one looks for the file from the include folders. The second one looks for the include file from the folder that the file including it is contained in.
As for file extensions, the compiler does not ever care what file extension you use for anything you reference with the #include directive. You can actually use anything you want to for the extension on a header file you make. All that matters is that you use the correct file name in the #include directive. You type in iostream and not iostream.h simply because iostream is the name of the file, not iostream.h. There is no other reason than that. iostream.h is an older version and is only packaged with some compilers for compatibility.
None.
Cool, thanks and I have another question lulz. How do I write out a number to an explonent. I tried base ^ power and that didn't work.
There's no power operator, instead there's a power function (called pow) defined inside the math header. You'd have to include the math.h header to use it.
However, for base-2 powers bitshifts are a lot faster than the pow function. And for low integer exponents a loop would be faster too.
a carat( ^ ) is an XOR operator(exclusive or)
None.