• Advertisement

Archived

This topic is now archived and is closed to further replies.

im so dumb! need help with hello world c++ project!

This topic is 5034 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

edit - In this post, I am simply trying to get help learning all about c++. I ask questions and people help me! thanks to everyone who has so far. No need to read everything, if you have time, please go down to the last question from wilhil, and answer... Thanks everyone for your help. [edited by - wilhil on May 12, 2004 5:20:36 PM]

Share this post


Link to post
Share on other sites
Advertisement
Telling us what the actual problem is would help.....

what Error messages / Warnings are you getting?

Share this post


Link to post
Share on other sites
h:\my projects\console 2\console 2.cpp(8): error C2065: ''cout'' : undeclared identifier


sorry! i forgot!

Share this post


Link to post
Share on other sites
try

using namespace std;

after #include "stdafx.h"

that will you let you use all the std you''ve included without going

using std::cout
using std::cin

etc.. for each command you want to use.

Share this post


Link to post
Share on other sites
Firstly you''re not including the "using namespace std" directive and secondly you talked about how you created your console application and C++ file but not how you created and included your header file. Did you create your header file in your project?

Share this post


Link to post
Share on other sites
It created everything automatically, and I just changed the .cpp file to add what I wanted.

Share this post


Link to post
Share on other sites
ok

std:: before works fine

thanks, but please can you explain what it does, what it means e.t.c.

I am currently working from http://www.cpp-home.com/tutorial.php?21_1
but i guess it is out of date then.

does anyone else know a free online tutorial that explains from the very beggining?

Share this post


Link to post
Share on other sites
You are sending the constant character string "test" to the standard output stream (represented by std::cout in C++ iostream library).

If you know nothing about programming, I would recommend buying a beginner''s book on C/C++ and spending a lot of time in the first couple chapters.

Regards,
Jeff


[ CodeDread ]

Share this post


Link to post
Share on other sites
I have been programming in visual basic for years now. I want to move to c++, In the tutorial I found, it didnt have the std:: bit, so I didnt understand it.

thanks

Share this post


Link to post
Share on other sites
Namespaces were introduced to avoid name clashing in large projects. By encompassing functions/methods in a namespace it provides a way of distuinguishing between two methods which look the same to the compiler:

//everything after this is enclosed in namespace Foo
namespace Foo
{
void SomeFunc(int SomeParm);
}

void SomeFunc(int SomeParm);

Now if you was to call SomeFunc like this:
SomeFunc(2358975);

You would be calling the non-Foo namespace method because the namespace declaration acts as an extra scope barrier.

To get to data declared inside a namespace you have a few options: you can either place the namespace name followed by the scope-resoluition operator ( :: ) directly before the method name to say you want to call the method inside that namespace like so:
Foo::SomeFunc(33565);

This can be a bit of a pain so alternatively you can add using namespace Foo; at the top of the file and everything declared inside the Foo namespace will be brought into scope.

Regarding your question everything in the C++ standard library is placed in the "std" namespace so to get access to cout/cin etc you must either use std:: before the identifier names or add using namespace std; to stdafx.h as has already been mentioned.

EDIT: removed forum smilies

[edited by - Spudder on May 10, 2004 12:08:03 PM]

Share this post


Link to post
Share on other sites
The Sams line of books are good (learn blah blah blah in 21 days) It''s what I used to learn C++

Share this post


Link to post
Share on other sites
// console 2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int blabla;
int blabla2;

int main()
{
std::cout<<"test"<<" 2 ";
std::cin>>blabla;
std::cin>>blabla2;
return 0;
}



it seems to wait for the first one, then it skips straight on.. what am i doing wrong here? also, how do i do a code thing in the post?

Share this post


Link to post
Share on other sites
The problem is that you''re not doing anything at the end of the program. The line:

return 0;

exits the program, and that''s the reason it closes.

If you run the program in a DOS windows, you''ll find that the window stays open after execution.

Share this post


Link to post
Share on other sites
Regarding the forums, put your code in between square-bracket tags. To see what I mean, hit "Quote" on my reply and you'll see the tags.


Example code here...(for smaller code sections, preserves
formatting...



More code here...(for bigger code sections)
// allows syntax high-lighting and a scroll bar




[ CodeDread ]

[edited by - rypyr on May 10, 2004 1:44:00 PM]

Share this post


Link to post
Share on other sites
shouldn't he be using #include? EDIT i'ma dumbass who didn't read the whole post.



Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|



[edited by - ssjcory on May 10, 2004 1:45:55 PM]

Share this post


Link to post
Share on other sites
well I just cant get this to work :


// console 2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int numberentered;
int numberentered2;

int main()
{
std::cout<<"please type your number";
std::cin>>numberentered;
std::cout<<"your number was" numberentered;
return 0;
}


I dont know how I should format the way to return the number. I get the error

h:\my projects\console 2\console 2.cpp(13): error C2146: syntax error : missing '';'' before identifier ''numberentered''


and before, I understand the window closing, I am not that much of a noob! I tried simply making another input happen, so you had to press enter before the window closed, but I couldnt get it working

Share this post


Link to post
Share on other sites
std::cout << "your number was" numberentered; 


try using

std::cout << "your number was" << numberentered; 


and it shoud work.

Also check www.cplusplus.com for a very good tutorial myself am using for migrating from VB.

[edited by - VBBR on May 10, 2004 2:19:59 PM]

Share this post


Link to post
Share on other sites
If you don''t understand classes and the C++ I/O system yet, I''d suggest you use printf() first instead.

Share this post


Link to post
Share on other sites
looking at site.

and also, how can I make a pause at the end of the program so that someone has to press enter, or something before it goes away?

Share this post


Link to post
Share on other sites
just use:

system("PAUSE");

just before the "return 0;" line.
One more thing, I suggest you to start always with a completely empty project and create the cpp files yourself rather than using the AppWizard or whatever.

[edited by - VBBR on May 10, 2004 2:48:18 PM]

Share this post


Link to post
Share on other sites
add

system("pause");


to pause your program. If that doesn''t work you could use

std::cin.get()

but that only works if there''s nothing in your input queue (you could change your

std::cin>>blabla

to

(std::cin>>blabla).get()

to get rid of the newline in the input queue.

Share this post


Link to post
Share on other sites
quote:
looking at site.

and also, how can I make a pause at the end of the program so that someone has to press enter, or something before it goes away?


There are a number of ways, most non-standard.


#include <stdlib.h>
int main(void) {
system("pause");
return 0;
}


This calls the system command line with the 'pause' command, and is very slow. Should work on alot of systems, though, because most widely used systems have a 'pause' command.


#include <stdio.h>
int main(void) {
fflush(stdin);
getchar();
}


This one flushes the keyboard buffer, and then calls 'getchar()'. 'getchar()' retrieves a keyboard from that buffer, and if there's nothing, it waits for something. Since 'fflush()' clears it, 'getchar()' waits until you press a key.

I've compiled this with MSVC++ 6.0, and it works. I compiled a console game I made as an example for a friend with Dev-C++, and it crashed. I haven't found the error, but I think it's with using 'fflush()' on 'stdin', because 'fflush()' is for output, not input.


#include <stdio.h>
int main(void) {
char var;
scanf("%c",&var);
return 0;
}


This is probably the most portable way, but I think it leaves the 'enter' key in the keyboard buffer, so your next input read will read the 'enter' press.


As for the original problem, you included all of the files correctly, but the compiler couldn't 'use' the functions inside, because they were 'locked' in a namespace. The namespace requires that you use the scope operator ( :: ) to access the stuff inside, or issue a 'using' directive:


namespace std {
cout
cin
}

int main(void) {
cout << "hello" << endl;
//Will give an error. Cout is defined in the std namespace, and cannot be accessed with just the name
std::cout << "hello" << std::cout;//Okay
using std::cout;
cout << "hello" << endl;//Will give an error. We are using cout, but not endl.
using namespace std;
cout << "hello" << endl;//Okay. We said to use the entire namespace, including 'cout' and 'endl'


[Edit: Because horizontal page scrolling is evil.]

[edited by - Oluseyi on May 12, 2004 3:51:06 PM]

Share this post


Link to post
Share on other sites
well thanks alot for that big message, although I just got a little confusde1 but it is all good!



system pauser works great! thanks.... almost the same as visual basi cthere! system instead of shell though!

again, thanks for the help.

Share this post


Link to post
Share on other sites
now the next thing.... if statements!

Thank you for your help so far, I was wondering if someone could tell me how to do the most simplest of if statements.

in visual basic, the moment i learnt how to do this, I could do almost anything! I know there are loads more that I have to learn in c++, but so far I have learnt more today than I have in ages!

I love learning by examples as I do not mind books, but it takes me so much longer... I am dyslexic and I find it so hard to concentrate on a book.

basically in visual basic

if item = this then
do something
end if

I am looking for the c++ version of this!

Thanks

Share this post


Link to post
Share on other sites
One point to note when testing for equality is to remember to use == and not =, the first is the equality operator and returns a bool value (true/false) whereas the second is the assignment operator which assigns the value on the right hand side of the = to the variable on the left hand side.

Here''s a quick example of how to test for equality using integers but it also applies to the other data types (floats are a little more tricky)


int g_InputVar = 0;

cout << "Enter A Number:" << endl;
cin >> g_InputVar; //for examples sake the user entered 1546

//check to see if the entered values is 1546
if(g_InputVar == 1546)
{
cout << "You entered 1546!" << endl;
else
{
cout << "You didn''t enter 1546" << endl;
}


That''s just a quick code snippet which shows how you can test for equality between integers. If you want to test individual characters (chars) then remember to use single quotes around the character:

if(g_SomeChar == ''a'')
//do something

If you are simply testing for a non-zero value (ie a boolean true value) you can simply enter the expression between the if parentheses like this:

int X = 50;
int Y = 100;

if(X > Y)
//do something

Alternativly if you are checking the value of a bool variable you don''t need the == true part of the expression, you can simple do this:

bool g_SomeBool = true;

//check to see if g_SomeBool is true
if(g_SomeBool)
//so something

Some people do add the == true part just to make the code clearer for others reading it.

Share this post


Link to post
Share on other sites

  • Advertisement