Staredit Network > Forums > Technology & Computers > Topic: C++ noob help
C++ noob help
Sep 8 2007, 5:15 am
By: l3lack-l3ahamut  

Sep 8 2007, 5:15 am l3lack-l3ahamut Post #1



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.

Sep 8 2007, 5:27 am l3lack-l3ahamut Post #2



nevermind I was browsing around and figured out that I was missing "using" in front of "namespace std;"



None.

Sep 8 2007, 5:30 am Falkoner Post #3



Don't you need to add something to the iostream, like <iostream.h>, or is that just to keep a program smaller?



None.

Sep 8 2007, 5:35 am l3lack-l3ahamut Post #4



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.

Sep 8 2007, 5:36 am Forsaken Archer Post #5



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.

Sep 8 2007, 6:10 am ShadowFlare Post #6



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.

Sep 9 2007, 6:29 am AntiSleep Post #7



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.

Sep 9 2007, 9:40 am ShadowFlare Post #8



Yeah, that is indeed what it is. :bye1: 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.

Sep 10 2007, 4:36 pm Kellimus Post #9



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



None.

Sep 11 2007, 5:16 pm l3lack-l3ahamut Post #10



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.

Sep 11 2007, 7:20 pm Clokr_ Post #11



The cin statement is just to retrieve data. You don't need the >> '\n'.



?????

Sep 11 2007, 9:31 pm l3lack-l3ahamut Post #12



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.

Sep 11 2007, 9:36 pm Clokr_ Post #13



Quote from Vhaeraun
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.



?????

Sep 12 2007, 1:11 am Matt Burch Post #14



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 :P.

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.

Sep 12 2007, 4:44 am ShadowFlare Post #15



Quote from Kellimus
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.

Sep 12 2007, 10:02 am AntiSleep Post #16



Quote from Clokr_
Quote from Vhaeraun
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.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[01:39 am]
Ultraviolet -- no u elky skeleton guy, I'll use em better
[10:50 pm]
Vrael -- Ultraviolet
Ultraviolet shouted: How about you all send me your minerals instead of washing them into the gambling void? I'm saving up for a new name color and/or glow
hey cut it out I'm getting all the minerals
[10:11 pm]
Ultraviolet -- :P
[10:11 pm]
Ultraviolet -- How about you all send me your minerals instead of washing them into the gambling void? I'm saving up for a new name color and/or glow
[2024-4-17. : 11:50 pm]
O)FaRTy1billion[MM] -- nice, now i have more than enough
[2024-4-17. : 11:49 pm]
O)FaRTy1billion[MM] -- if i don't gamble them away first
[2024-4-17. : 11:49 pm]
O)FaRTy1billion[MM] -- o, due to a donation i now have enough minerals to send you minerals
[2024-4-17. : 3:26 am]
O)FaRTy1billion[MM] -- i have to ask for minerals first tho cuz i don't have enough to send
[2024-4-17. : 1:53 am]
Vrael -- bet u'll ask for my minerals first and then just send me some lousy vespene gas instead
[2024-4-17. : 1:52 am]
Vrael -- hah do you think I was born yesterday?
Please log in to shout.


Members Online: IlyaSnopchenko