Learning C++ from scratch

I would like to start learning c++ from scratch, where would I start.

Page 1 of 1

9 Replies - 2657 Views - Last Post: 16 May 2010 - 09:36 AM Rate Topic: -----

#1 alakazamm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-July 08

Learning C++ from scratch

Posted 17 July 2008 - 07:31 AM

Hey people,

I am looking to learn C++, I have no prior knowledge of any other language. I've tried using the 'Learn C++ in 21 Days' book which is on the internet but it does not really explain anything. I have made the 'Hello World' program but I cannot compile it because I always get errors.

Here is the code I use:
#include <iostream>

int main();
int main()
{
cout << "Hello World!\n";
return 0;
}



The guide doesn't specify any particular software or compiler it just says I need a raw text editor (notepad) and some sort of compiler. I downloaded 'Cygwin' and use it to compile but I always get the following messages:

hello.cpp: In function 'int main()':
hello.cpp:6: error: 'cout' undeclared (first use of this function)
hello.cpp:6: error: (Each undeclared identifier is reported only once for each function it appears in.)

The code I used is copy and pasted straight from the guide but it still doesn't work. If anyone could explain what is going wrong I would really appreciate it. I am more annoyed at the fact that following the guide word for word doesn't work and I would be even more happy if someone could point me in the right direction or show me a working guide which can help me get started on learning this code.

Is This A Good Question/Topic? 0
  • +

Replies To: Learning C++ from scratch

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Learning C++ from scratch

Posted 17 July 2008 - 07:39 AM

If you copied that from a specific book, I would suggest you get another book. this code will not compile on any compiler. It should read:
#include <iostream>
using namespace std;

int main()
{
cout << "Hello World!\n";
return 0;
}



http://www.cplusplus.com/doc/tutorial/
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3121
  • View blog
  • Posts: 19,167
  • Joined: 14-September 07

Re: Learning C++ from scratch

Posted 17 July 2008 - 08:14 AM

I don't know what version you have of that book; I really enjoy the C++ 21 days one that I own. Sidenote, you don't need to declare main()'s prototype like other functions.
Was This Post Helpful? 0
  • +
  • -

#4 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Learning C++ from scratch

Posted 17 July 2008 - 08:25 AM

I'm just in the process of learning C++ for the first time too. I have a little bit of prior knowledge on BASIC but that's about it. The book that i'm finding the best for learning is "Starting out with C++ from control structures through objects" sixth edition by Tony Gaddis.... It's 1400 pages but if you can get through it it's really helpful. Hope this helps
Was This Post Helpful? 0
  • +
  • -

#5 Guest_rsk*


Reputation:

Re: Learning C++ from scratch

Posted 16 May 2010 - 12:03 AM

this is the code for hello world


#include<stdio.h>
main()
{
printf("hello world\n");
return 0;}
Was This Post Helpful? -2

#6 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,362
  • Joined: 28-March 09

Re: Learning C++ from scratch

Posted 16 May 2010 - 12:16 AM

If you happen to have the "Learn C++ in 21 days" thats around 250-300 pages, its junk. I have it and it explains nothing at all. I was hoping i could use it as a quick reference, but I don't even think it would be good for fire kindling.

Try looking up Murachs C++ 2008 on amazon. Im sure you could pick it up for around 3 bucks or something used. The C# version is $2.08 used, so I can't imagine the C++ version is much more.
Was This Post Helpful? 0
  • +
  • -

#7 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Learning C++ from scratch

Posted 16 May 2010 - 12:29 AM

View Postrsk, on 16 May 2010 - 01:03 AM, said:

this is the code for hello world


#include<stdio.h>
main()
{
printf("hello world\n");
return 0;}


might have been better rendered as ...

this is the C code for hello world

#include <stdio.h>

int main()
{
   printf( "Hello world\n" );

   printf( "\nPress 'enter' to continue ... " );
   getchar();
   return 0;
}



Edit:
Hey ... the OP was back on "17 July 2008"
Oh well ... it's a recurring theme.

This post has been edited by David W: 16 May 2010 - 12:46 AM

Was This Post Helpful? 0
  • +
  • -

#8 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Learning C++ from scratch

Posted 16 May 2010 - 04:46 AM

View Postrsk, on 15 May 2010 - 11:03 PM, said:

this is the code for hello world


#include<stdio.h>
main()
{
printf("hello world\n");
return 0;}


This isn't a good example to learn from to be honest.
Was This Post Helpful? 1
  • +
  • -

#9 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Learning C++ from scratch

Posted 16 May 2010 - 07:54 AM

View PostAphex19, on 16 May 2010 - 05:46 AM, said:

View Postrsk, on 15 May 2010 - 11:03 PM, said:

this is the code for hello world


#include<stdio.h>
main()
{
printf("hello world\n");
return 0;}


This isn't a good example to learn from to be honest.



You are right ... so let's learn from it ...

what is wrong:

the line
main()
needs to be replaced with the line
int main()
so that the program will compile ok on C compilers


what needs to be improved:

(readibility) by using:
1. conventional indentation
2. spaces & extra blank lines
3. comments

(concept veracity) by stating:
this is code that will compile with
-> C compilers
also
-> C++ compilers

/* C 'io' printf and getchar need this header file included */
#include <stdio.h>

int main()
{
   printf( "Hello world\n" );

   printf( "\nPress 'enter' to continue ... " );

   /* now wait for 'enter' to be pressed ... to keep the window open */
   getchar();
   return 0;
}


This post has been edited by David W: 16 May 2010 - 08:02 AM

Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3121
  • View blog
  • Posts: 19,167
  • Joined: 14-September 07

Re: Learning C++ from scratch

Posted 16 May 2010 - 09:36 AM

Necro thread is closed
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1