Alice Community

Alice Community (http://www.alice.org/community/index.php)
-   Questions and Comments (http://www.alice.org/community/forumdisplay.php?f=9)
-   -   Moving on (http://www.alice.org/community/showthread.php?t=7037)

belso 08-02-2011 04:02 PM

Moving on
 
Hello i have become proficient in alice over the years and i wish to learn c++. anyone know where i can download c++? Many thanks

legolizard 08-02-2011 05:01 PM

It depends there are many different IDE's and compilers, out there and it is a matter of preference as well as what OS you are using. For example if you are running a Mac then you need to find a Mac specific IDE. Personally I use Dev-C++,it is free,simple, runs on Windows, and the compiler is mostly standard, but again it is a matter of preference.

Anyways good luck in learning C++,I think you will find it relatively easy to get to understand, but if you need any help you can always ask me.:)

Dj Tech42 08-02-2011 05:11 PM

Xcode for Mac

belso 08-02-2011 10:21 PM

[QUOTE=legolizard;40167]It depends there are many different IDE's and compilers, out there and it is a matter of preference as well as what OS you are using. For example if you are running a Mac then you need to find a Mac specific IDE. Personally I use Dev-C++,it is free,simple, runs on Windows, and the compiler is mostly standard, but again it is a matter of preference.

Anyways good luck in learning C++,I think you will find it relatively easy to get to understand, but if you need any help you can always ask me.:)[/QUOTE]

Hey i use windows and i would like to use dev-c++ can you pass me a link? Thanks

legolizard 08-02-2011 10:39 PM

Here you go.[URL="http://www.bloodshed.net/devcpp.html"]http://www.bloodshed.net/devcpp.html[/URL]

belso 08-03-2011 04:32 AM

Thanks

Dj Tech42 08-03-2011 01:11 PM

I'm interested in learning C++ too. Can you post some tips later on?

We're stuck with C# at school. It's basically Microsoft-proprietary Java. ugh

legolizard 08-03-2011 01:50 PM

[QUOTE=Dj Tech42;40220]
We're stuck with C# at school. It's basically Microsoft-proprietary Java. ugh[/QUOTE]

Yeah, I don't really like C# either.

belso 08-07-2011 02:10 PM

!
 
Hey i have a c++ book and the starting is different in the book we still use the iostream and stuff but it has voidmain() which dosent work in this c++ so what do i use for this c++? Thank you

legolizard 08-07-2011 03:12 PM

This is because the C++ standard main() is of integer type not of [B]void[/B] type. Your book uses Microsoft's version of the main(), which is of course not standard and will [I]only work[/I] in compilers made by Microsoft say Microsoft Visual Studio, which I believe you must buy after a trial period. You can still use your book however replace:

[CODE]void main()
{
//Some code.....
}

[/CODE]

with:

[CODE]int main()
{
//Some code....
return 0;
}[/CODE]

belso 08-07-2011 05:37 PM

What does the return 0; do?

legolizard 08-07-2011 07:14 PM

You will problably read up on this later in your book, but here is my expanation if what it is.

Since the main() is of integer type meaning that it has the [B]int[/B] keyword it must return a value. See there two primary types of functions ,functions that return values and functions that return nothing.Any function with the keyword [B]void[/B] at the beggining does not return anything. Every other function must return something. For example:

[CODE][B]int[/B] foo()
{
//Some code
}[/CODE]

returns an integer.

while

[CODE]string boo()
{
//Some code
}[/CODE]

returns a string.

Now every function that returns something [B][I][U]must[/U][/I][/B] have a [B]return[/B] statment and then a value or variable that you want to return.

So you would get:

[CODE][B]int[/B] foo()
{
[B]return[/B] 5;
}[/CODE]

So this means that when foo is finsihed executing it will return the value 5.

Now what this return statment does is basically makes the value of foo eqaul to 5.

So to summarize return 0; basically returns the integer 0 back to the main().

Does that make sense? If it does not I can make a example program for you.:)

belso 08-07-2011 09:01 PM

!?!?!?!?
 
Hey i understand that completely but what i dont understand is this code i made adjusted to waht you said from the book. It dosent work at all i cant even run it because it wont compile it for some reason


#include <iostream.h>


int main()
{
cout << "Hello! "
"Congratulations on your first"
"C++ program"





return 0;

}

legolizard 08-07-2011 09:28 PM

Now Microsoft and that book are getting on my nerves. First they use void main() and anyone who uses that should be shot. Now they are using a C library and not even using the [B][I][U]STANDARD NAMESPACE[/U][/I][/B].:mad::mad: They even forgot the@#$&*^!!! semicolon at the end of the cout statement!!!! RAWR!!!!! *thorws something from desk*

Sorry one of the few things that annoys me is when experts have no clue on what they are doing.

Okay well firstly instead of

[CODE]#include <iostream.h>[/CODE]

use

[CODE]#include <iostream>[/CODE]

iostream.h is for C not C++.

Secondly cout and endl and a few other things are part of the library iostream within the standard namespace. Now there are two ways to address this you can

A)[CODE]#include <iostream>


[B]using[/B][B] namespace [/B]std;
[B]int[/B] main()
{

cout << "Hello! "<<endl;
cout<<"Congrats on making your first C++ program!"<<endl;

return 0;

} [/CODE]

or

B)[CODE]#include <iostream>


[B]int[/B] main()
{

std::cout << "Hello! "<<std::endl;
std::cout<<"Congrats on making your first C++ program!"<<std::endl;

return 0;

} [/CODE]

Matter of personal preferance. Also one other thing to note. Both programs above will compile fine but you will notice that your prompt will open and then close immediately. To counter act this you can do a variety of things such as:

[CODE]#include <iostream>


[B]using[/B][B] namespace [/B]std;
[B]int[/B] main()
{

cout << "Hello! "<<endl;
cout<<"Congrats on making your first C++ program!"<<endl;

cin.get();//What I generally use; just press the enter key.

return 0;

} [/CODE]

or


[CODE]#include <iostream>
#include <conio.h>//Technically a C header file but it is not as bad as iostream.h


[B]using[/B][B] namespace [/B]std;
[B]int[/B] main()
{

cout << "Hello! "<<endl;
cout<<"Congrats on making your first C++ program!"<<endl;

getch();

return 0;

} [/CODE]

or


[CODE]#include <iostream>


[B]using[/B][B] namespace [/B]std;
[B]int[/B] main()
{

cout << "Hello! "<<endl;
cout<<"Congrats on making your first C++ program!"<<endl;

system("PAUSE");//This method however is very resourse costly do not use for big programs as it is bad programming pracitce.

return 0;

} [/CODE]

One final thing endl = end line or enter.

belso 08-07-2011 09:50 PM

Thanks
 
Hey thanks but i decided to use cin.get(); and i would like to know what that means. Im sorry but i dont like to use something and less im certain on the uses so it can be used in as many ways as i can

legolizard 08-07-2011 10:06 PM

Very good state of mind. cin.get(); also comes from the standard namspace within the iostream header file. It is derived from the normal cin>>.

What it does is wait for the user to enter something it then treats it as a char meaining you could type "Blalalalaalalalalala" and then cin.get() would only recieve 'B' where as cin>> whould take the entire string. When you press enter cin.get() takes in whatever you typed in executes and then the main() goes to to the last line return 0; thereby terminating the program. cin.get() can also be used for user input because of this and is not just for pausing. You could use it like this for example:

[CODE]#include <iostream>

using namespace std;

int main()
{
[B]char[/B] myChar;

cin.get(myChar);

cout<<myChar<<endl;

system("PAUSE");//I used system("PAUSE"); to make it less confusing.

return 0;

}
[/CODE]

belso 08-07-2011 11:20 PM

Ok i sorta get that but i am also confused so you used cin.get() but how did you make it so he had to hit enter?

legolizard 08-08-2011 09:55 AM

Okay then lets back track a little. As you know cout tells the computer to output something such as a string or varaiable and we have already seen it used in our "Hello World " program like so:

[CODE]#include <iostream>

using namespace std;

int main()
{
cout<<"Hello World! "<<endl;

cin.get();
return 0;
}[/CODE]

cout stands for console output, but does console[I] input [/I]exist? Yes this is called a cin statment and like cout it is contained within the iostream header file. In order to use the cin statment in our program we go like this:

[CODE]#include <iostream>

using namespace std;

int main()
{
int userAge;//Here we declare a integer variable named userAge.
cout<<"Hello World!"<<endl;
cout<<"How old are you?"<<endl;
cin>>userAge;//Here we use a cin statment for user input. Notice how we use two greter than signs rather than two less than signs. You will learn about this later.

cin.get();
return 0;
}[/CODE]

Now here is the problem with using cin.get() that I forgot to mention. When you press enter for your cin statment you then tell the computer to save whatever you typed into the integer userAge. However when you press "enter" it actually stays in the keyboard buffer rendering cin.get() useless for pausing. So what you do is add another dirivative of cin known as cin.ignore(); which will ignore the last key you typed(enter) thus pausing the program.

In the end we should have this:

[CODE]#include <iostream>

using namespace std;

int main()
{
int userAge;//Here we declare a integer variable named userAge.
cout<<"Hello World!"<<endl;
cout<<"How old are you?"<<endl;
cin>>userAge;//Here we use a cin statment for user input. Notice how we use two greter than signs rather than two less than signs. You will learn about this later.

cout<<"Wow you are "<< userAge <<" year(s) old."<<endl;

cin.ignore();//Ignores the last key typed being enter.
cin.get();
return 0;
}[/CODE]

Now in order to tell the user to press the enter key again all you have to do is add a cout statment below cin.ignore saying "Press enter to continue."

Now cin is very interesting. If you enter a number it will treat it s one, if you enter a string it will treat it as a string. Yet what if we want only one single character. This is when we use cin.get() such as in the program in my last post. The difference between cin.get() and cin is basically when you enter somthing it will treat what you typed as an appropiate data type while cin.get() will treat everything as a char value and will even cut off what you typed into a single char.

Does that make more sense? Again I can give you an example.

Edit: I just noticed but my posts are really long. Sorry about that.

belso 08-08-2011 02:45 PM

Yes i think i get it much better now thank you and your post are long but you cant attempt to try and make them short they are full of valuable info that needs to be there thank you

belso 08-08-2011 03:03 PM

?
 
Hello i am trying to make a program to explain all the functions of c++ and i have made a starting but something is wrong with it can you help explain what?



#include <iostream>

using namespace std;

int main()
{
int userinp;

cout<< "This program was made by Belso. And the knowledge is from Legolizard."



cout<<"Hello we are about to learn about the functions" end1;
cout<< " of c++ take a seat and enjoy the ride" end1;
cout<< " Next to each word if there is a letter" end1;
cout<< "Then that is the letter you have to hit to learn about it" end1;
cout<< "cout(a)"end1;
cout<< "cin(b)" end1;
cout<< "int(c)" end1;
cout<< "cin.get() (d)" end1;
cin>>userinp;
}
for some reason it wont compile maybe i am missing something thank you for looking at it

legolizard 08-08-2011 04:37 PM

Only syntax errors. Firstly it is endl with an 'l' not end1 with a '1.' Secondly you forgot to use the insertion operators for ever endl;

For example you did:

[CODE]cout<<"Hello we are about to learn about the functions" end1;[/CODE]

But it should be:

[CODE]cout << "Hello we are about to learn about the functions" << endl;[/CODE]

Nighthawk0973 08-08-2011 05:00 PM

it seems like the book your using isn't using the same compiler as you are. I started with dev-C++ and I have deinstalled it. I prefer Code::Blocks ([url]www.codeblocks.org[/url]) but I have MSVC++ for when I learn more about GUIs and want to use them more easily. MSVC++ allows you to drag and drop buttons allowing me to create a web browser before I even knew what 'cout' ment :D

I recommend 'Buckys C++ Programming Tutorials' it's a series on youtube. I'm still learning it today. It utilizes the same compiler Code::Blocks has. There's also a tutorial series that hte same guy made called 'C++ Programming Tutorials' and it utilizes Dev C++ but isn't as good. If you want a book on Dev-C++ get C++ For Dummies, all in one desk reference. It has 7 C++ books in one ans will teach you what you will need ot know. Example: Networking!!!!

I like Code::Blocks best because it uses an Eclipse style of programming. So when you make Quotation mark 1, it automatically makes Quotation mark 2 and your cursor gets put inbetween, than you can type the contents of the quote, and lastly you just press the -> key (left arrow) to move outside of the quotation marks and continue codeing.

I think Code::Blocks has the '::' in it because of this: (code made by me just know in Code::Blocks)

Quick Note: This works just ran it! Codeblocks automatically returns 0 if your using the 'int main()' function
[CODE]
#ifndef CODE_H
#define CODE_H

class Code{

public:
Code(); //Code classes constructor
void Blocks(); //void method Blocks
};

#endif
[/CODE]
That was a header file called 'Code.h'
[CODE]
#include <iostream>
#include "Code.h"

using namespace std;

Code::Code(){


}

void Code::Blocks(){ //NOTICE THE PART AFTER VOID: Code::Blocks

cout << "Get it? Code::Blocks!" << endl; //THIS LINE IS PROBABLY GOING
//MAKE THE MOST SENSE TO YOU :D
}
[/CODE]
This is Code.cpp, the reason I did the big comments is so you would see
[CODE]
#include <iostream>
#include "Code.h"

using namespace std;

int main(){

Code Code; //allows us to execute next line without errors
Code.Blocks(); //Calls on the method Blocks from code.cpp
}
[/CODE]
And the result is:

[CODE]
Get it? Code::Blocks!

Process Returned 0 <0x0> execution time : 0.063
Press Any Key To Continue
[/CODE]
But the bottom 2 lines are just codeblocks stuff that dissapear when you use more advanced programming I'm sure. It's a fully runnable .exe ready for full distribution now! :D

legolizard 08-08-2011 08:32 PM

Wow. Just wow. Code blocks is actually really cool. I remeber that my teacher once said to me to use it but I don't listen too often.:o The good part though is that it uses a standard compiler for C++ which means that all that I have showed you Belso isn't wholefully useless.:) So yeah download code blocks it is 1000000x better than Dev, only wish I stated using it before.:( Thanks NightHawk, but uh don't you think it is a little early to show him OOP?

belso 08-08-2011 08:58 PM

Ok i will download code blocks and i have realized how different my book is and i will go use a different book

belso 08-09-2011 03:49 PM

Ok i do not get codeblocks at all i am completely stumped.

legolizard 08-09-2011 06:31 PM

Okay what do you mean exactly? Do you mean which version to download ,or how to make a program or what?

Nighthawk0973 08-09-2011 07:29 PM

I hope your not attempting the code that I wrote... that was not ment to be your level at all. I was just saying that I'm pretty sure that's why there's the double colons in the middle of Code and Blocks. (Get it? Code::Blocks! :D)

Anyways here's how you start codeblocks. File->New->Project

inside your project there's a file called 'main.cpp' inside your main.cpp type the following:

[CODE]
#include <iostream>

using namespace std;

int main(){

cout << "Hello World, but in Code::Blocks!" << endl;
}
[/CODE]

This will print out a Code::Blocks hello world. Don't add the return 0; because it already does that with codeblocks.

@legolizard
Yeah. I always strive to find the best IDE. Best Java IDE is Eclipse. Best C++ IDE is Code::Blocks, best VB IDE... I have no clue :D
(Best HTML IDE that I know of is JCreator)

belso 08-09-2011 08:38 PM

?
 
Ok i installed it but when i go into where i make a project i dont see the main thing that you were talking about so i cant code anything. Maybe i did install it wrong

legolizard 08-09-2011 09:28 PM

If you look at the left you will see a tab called "projects" make sure you have that tab open and you see something that says "workspace." Make sure you click it and two other things will pop down. Now you should see a + butt0n next to "Source." Click the plus sign and and another file will popup called main.cpp.. Click it and TA DA!

belso 08-10-2011 12:40 PM

Alright i found it and i can do code now but when i made the code the nighthawk did i go to build and run and it dosent do anything

legolizard 08-10-2011 02:14 PM

Don't worry about what Nighthawk did he was just explaining why Code :: Blocks uses the two :: (if I remember correcty it is called the unary scope resolution...... operator......thing could be wrong though. I [I]think[/I] it is used in other things but I would have to test it out I generally don't use it.) The main point though is that Nighthawks code is "oriented" differently than what you are used to, but don't worry all will be revealed in no time.:)

belso 08-10-2011 04:15 PM

I used his hello world code

legolizard 08-10-2011 05:03 PM

Ah okay. Well I think I know why this is but I need to see what it says at the bottom in the "build log" to be sure.

belso 08-10-2011 09:23 PM

The build log is blank

legolizard 08-10-2011 09:36 PM

Hmmmm. Well when you downloaded Code :: Blocks did you get the first one for Windows or the second one?

Nighthawk0973 08-10-2011 10:46 PM

I'm confused on how you would have so much troubles with this... Most of this is pretty self explanitory really... maybe I forgot something in my Hello World code... Also, codeblocks is the same as C++ it's just using a different compiler than your used to. Most of it works great though.

Acually I take that back. Project setup might be a bit confusing for the absolute beginner so let's take this step by step...

Step 1 - Open up Code::Blocks

Step 2 - File->New->Project

Step 3 - Choose Console Application and Hit 'Go'

Step 4 - Click Next

Step 5 - Make sure the language 'C++' (not C) is chosen and click next.

Step 6 - Make the project name 'Programming' or something else that you would like and than Click Next. It prompted, Overwrite the project.

Step 7 - Make sure the Compiler is: GNU GCC Compiler, and leave everything else at the default. I believe the 'GNU GCC Compiler' is a standard C++ compiler. You could run other compilers once your more experienced. The coding will be different though.

Step 8 - Click Finish and overwrite again if it prompts you.

Step 9 - Expand the Sources folder and double click main.cpp

Step 10 - Copy the source code I wrote down (the Hello World source) and paste it into the main.cpp file

Step 11 - Press F9 and watch the magic happen!

Please note my Code::Blocks version is 10.05 if that changes anything which I doubt...

@legolizard
According to [url]www.cplusplus.com[/url] '::' is called the Scope Operator, not the Scope Insertion Operator. That's what I would've thought it was if I hadn't googled it just to be sure.

belso 08-11-2011 03:04 AM

Ok i got to the point where i expand the sources file but where is the sources file?

Nighthawk0973 08-11-2011 01:41 PM

Did you follow all the steps exactly as I said? It's in the project explorer (or something equivilant to that) on the righthand side, just like legolizard said you click the plus and the sources folder will expand and reveal main.cpp.

belso 08-11-2011 08:55 PM

Ok i did everything you said and the build log said this "Hello - Debug" uses an invalid compiler. Probably the toolchain path within the compiler options is not setup correctly?! Skipping...
Nothing to be done.

legolizard 08-11-2011 09:09 PM

When you first downloaded C::B did you get the first one or the second one? First one bad second one good.:)


All times are GMT -5. The time now is 07:05 AM.

Copyright ©2020, Carnegie Mellon University
Alice 2.x 1999-2012, Alice 3.x 2008-2012, Carnegie Mellon University. All rights reserved.