PDA

View Full Version : help please, Newbie in C++




adrix7
Jun 10, 2008, 09:22 PM
hi, absolute newbie here,

I've just enrolled Programming fundamentals in C++ this summer

I've read that Objective C++ is a good developer to learn.. i have a macbook laptop (leopard 10.5.2)

now what do i need and where do i find it to get me started :-)


thank you for taking time guys more power to you all!


hope i'll enjoy and learn C++ basics thi summer it really looks hard:-(



yeroen
Jun 10, 2008, 09:36 PM
You mean the Objective C++ language? It's not a good first language to learn. In fact, it's not really a separate language. It's an idiom that mixes Objective C and C++, but the mixture is somewhat restricted, and to understand what's allowed and what isn't, you need to know both Objective C and C++ fairly well. As a language, it's really meant for carpetbagging C++ programmers moving over into Objective C.

I think you perhaps mean development environment? If this class you're taking has any taste, it'll all be in UNIX, writ and compiled in the terminal, but I'm betting it'll be Windows which means Visual Studio which means a heavyweight non-portable IDE that's almost as much effort to learn as the language itself.

adrix7
Jun 10, 2008, 09:52 PM
hi, yes its in visual studio... is there a version for mac? or what do you suggest i use for learning C++ in my class using macbook..

thanks :)

yeroen
Jun 10, 2008, 10:23 PM
If the class is in Visual Studio, then you'd need VMWare or Parallels running Windows. Visual Studio doesn't exist for Mac.

Apple's Xcode is the rough Visual Studio equivalent for OS X. It's a lovely piece of software, but like all IDEs, it's a complex, peculiar beast whose virtues aren't really apparent until your projects' complexity starts to scale (hundreds of files, multiple build versions, etc.) . Before that point is reached, it has a tendency to just get in the way.

I'd suggest, as a counterpoint and astringent to the MS way of doing things, pursuing a parallel learning path and get to know the UNIX layer of your Mac. Learn how to edit, compile, run, and debug programs from the command line, outside an everything-and-the-kitchen-sink IDE. Doing so you will have a much keener sense of what's going on behind the scenes, and you'll be a better programmer in the end.

adrix7
Jun 10, 2008, 11:20 PM
hi, what do you mean by exploring the unix layer of my macbook?
Im trying to download the xcode (leopard) from apple will this do in school?

sorry for my newbie questions:( i really need to get started otherwise i'll might change my this subject for another one, i have till tommorow to withdraw the course..(i hope i wont come to this)

i it will help.. this is the book we are using C++ How to Program", by Deitel and Deitel, 6th edition.

please direct me to a software where i can use in my macbook:confused:


thank you again:D

sonyc148
Jun 11, 2008, 12:26 AM
1) If you want to code for macosx, you can:
- use you Leopard install disc (if I remember correctly, XCode 3 can be installed from your Leopard install disc)
- or, if you have lost your Leopard install disc, download xcode 3 on the apple website (http://developer.apple.com/tools/download/)

Once you have downloaded XCode, you can follow the tutorial in the following to create your first macosx application:
http://developer.apple.com/documentation/Cocoa/Conceptual/ObjCTutorial/01Introduction/chapter_1_section_1.html

You can (and should) also read the book available at: http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/ObjC.pdf
It is the complete specification of the Objective C 2.0 language.

If you are taking a course about Visual C++, learning XCode/Objective-C 2.0 won't help you much. It is still an interesting exercise, as it will make you:
- aware of the alternatives
- more critical about Visual C++ (meaning that knowing other ways to do something can often lead you to having original ideas to solving problems)
- better at coding in general
But it is an exercise I would leave for later. I wouldn't recommend on learning XCode/Objectice-C 2.0 as your first language.

2) If you want to code for windows, or if you want to learn a little Visual C++ before taking your class, you will have to install windows xp on your macbook (no need to install vista). Use bootcamp for performance reasons (visual studio IDE is quite heavy in terms of ressources).

Once windows xp is installed on your macbook, you can download a "free for personnal use" version of Visual Studio C++. Use the following link to download the Visual Studio C++ Express Edition: http://www.microsoft.com/express/product/default.aspx

To learn about Visual Studio C++, you can use the following links (warning: if your browser is displaying an empty page when trying to follow those links, just try in another browser; I had problems navigating this site on firefox/safari, but not on internet explore 6):
http://www.videotutorialzone.com/development-tools/visual-c++/microsoft-visual-c++-lesson-0-installing-microsoft-visual-c++-2008-express-edition.html
http://www.videotutorialzone.com/development-tools/visual-c++/microsoft-visual-c++-lesson-1-creating-your-first-hello-world-program-in-the-console.html
http://www.videotutorialzone.com/development-tools/visual-c++/microsoft-visual-c++-lesson-2-building-on-the-hello-worl-application-with-input-and-outpudt.html
http://www.videotutorialzone.com/development-tools/visual-c++/microsoft-visual-c++-lesson-3-an-introduction-to-variables-and-constants.html
http://www.videotutorialzone.com/development-tools/visual-c++/microsoft-visual-c++-lesson-4-introduction-to-data-types.html
http://www.videotutorialzone.com/development-tools/visual-c++/microsoft-visual-c++-lesson-5-understanding-logical-operators.html
http://www.videotutorialzone.com/development-tools/visual-c++/microsoft-visual-c++-lesson-6-understanding-relational-operators.html
http://www.videotutorialzone.com/development-tools/visual-c++/microsoft-visual-c++-lesson-7-introduction-to-if-else-if-else.html
http://www.videotutorialzone.com/development-tools/visual-c++/microsoft-visual-c++-lesson-8-introduction-to-while-and-do-while-loops.html
http://www.videotutorialzone.com/development-tools/visual-c++/microsoft-visual-c++-lesson-9-tic-tac-toe.html

C++ is quite hard to learn if this is your first programming language, but it's not impossible. If you have the choice, I would recommend learning C#, as it is slightly easier.

You should also look for articles on the internet, to understand what OOP is (Object Oriented Programming, as opposed to Structural Programming).

If you don't know anything about programming, don't be afraid if the learning curve is quite slow at the beginning. The main things you need to keep doing are:
- experiment
- read
- ask
- (repeat the three tasks above)

The internet is full of tutorials/blogs about programming stuffs in general, so learn to master google, and do your own research ;)
When you are following a tutorial, don't give up if you don't understand everything. Follow the instruction, and once you have finished the tutorial, look back at what you did and see if you can make sense of what the tutorial was trying to demonstrate.

After following some tutorials, try to find something easy that you would like to code (something like a dvd collection application to keep track of your dvds, or an application where you can store/edit/display recipes, ...), and spend the time needed to code it. If you get stuck and don't know how to do something, google is your friend.

After the few hours/days/weeks, depending on your current knowledge, your will start learning new coding stuffs pretty fast, so the main advice you can get is: don't give up.

Good luck.

adrix7
Jun 11, 2008, 12:03 PM
hi, im not really trying to specifically code for mac or windows yet, just trying to learn the language first.. I've installed xcode, it really looks intimidating and its like for advance user only:o


can i just enter like few basic C++ examples from my book and get results like the classic..

#include <oistream>

int main()

{

std::cout <<"hello world"\n;

return 0;

}

// got so many errors on this by the way... what i did by the way is, i created a new project, choosed standard carbon c++.. then ecoded the above..

or did i do it wrong?


the book mentioned about GNU C++ went to their site but cant find the download link... can anyone help me with the link?


by the way i decided to not drop the subject.. im thinking this will really hurt my gpa:D

thanks again for taking time!!

lee1210
Jun 11, 2008, 12:12 PM
Forget about xCode completely.

Enter your code in a text editor (it's iostream not oistream, not sure if that's an issue at this point) and save it.

use g++ from the terminal. OS X already has the GNU compilation tools.

If your file was saved as helloworld.cpp, you'd run:
g++ -o helloworld helloworld.cpp
./helloworld

To compile and run your program.

Do not use an IDE like xCode yet. if you HAVE to use VC++ at school, so be it, but try to learn how to do things with the command line tools at the beginning.

-Lee

lazydog
Jun 11, 2008, 12:30 PM
Hi

Now i would recommend the opposite, ie use Xcode. Start a new project and select Command LineUtility, C++ tool. There's your hello world program ready for you to compile and run. You can take it from there. Once you get the basic understanding of how Xcode works (and it's not that difficult for simple stuff) you can concentrate on learning/experimenting with C++. If you decide you want to take learning C++ seriously, you can start digging into the command line, compiler options, linkers etc. But for simply trying out stuff from your book, I would say use Xcode.

b e n

adrix7
Jun 11, 2008, 01:09 PM
found the xcode with the hello world code,

pasted my code from the book, how do i view/run it
-i did clicked the build and go and run in the menu, it says it succeeded but i seem not to see the window/view of my "hello world":D


thank you so much guys!

lazydog
Jun 11, 2008, 01:15 PM
The output will be in the console view. Under the Run menu select Console, or hit shift+cmd+R.

b e n

adrix7
Jun 13, 2008, 02:13 PM
hi guys im already starting with C++ using xcode-- thanks for all your help!

yep i know my problems does not end here but actually the opposite, if you know what i mean:D

dont get me wrong im having fun!:)

you see im having this problem with the output..

i must come up with this result on screen, having to input 3 integers.

sum:
ave:
product:
smallest:
greatest:

- now my problem is i keep on having two results of smallest or greatest..
what did i do wrong?


thanks guys:D

adrix7
Jun 13, 2008, 02:13 PM
hi guys im already starting with C++ using xcode-- thanks for all your help!

yep i know my problems does not end here but actually the opposite, if you know what i mean:D

dont get me wrong im having fun!:)

you see im having this problem with the output..

i must come up with this result on screen, having to input 3 integers.

sum:
ave:
product:
smallest:
greatest:

- now my problem is i keep on having two results of smallest or greatest..
what did i do wrong?


thanks guys:D

aross99
Jun 13, 2008, 02:58 PM
I think you have an error in all of the IF statements with the "and" parameter - I am surprised you didn't get a syntax error.

instead of this:

if (number1 < number2 and number3)

I think you want something like this:

if (number1 < number2 && number1 < number3)

lee1210
Jun 13, 2008, 04:20 PM
I think you have an error in all of the IF statements with the "and" parameter - I am surprised you didn't get a syntax error.

instead of this:

if (number1 < number2 and number3)

I think you want something like this:

if (number1 < number2 && number1 < number3)

In C++ "and" is (can be?) a reserved word that can be used in place of a logical and (&&). As such the syntax was valid, but there was a logic error. The code amounted to, in English:
If number1 is less than number 2 and number3 is not equal to 0

I'm sure your assumption about the intention is correct, but wanted the OP to understand what he was expressing the way his code was.

-Lee

aross99
Jun 13, 2008, 04:30 PM
I figured it was something like that since it wasn't flagged as a syntax error. I'm afraid I am an old-time C programmer, and not up to speed on these "new" C++ conventions. :)

Good to know though...

adrix7
Jun 13, 2008, 10:28 PM
yep thats what i think thought so too.. there is something wrong with my grammar or logic...

its kind of funny how the smallest gives out single result and the greatest gives two..but sometimes 1...

any suggestions on how to make it work?

aross99
Jun 13, 2008, 10:37 PM
yep thats what i think thought so too.. there is something wrong with my grammar or logic...

its kind of funny how the smallest gives out single result and the greatest gives two..but sometimes 1...

any suggestions on how to make it work?

Try making the changes I suggested, or using "and" in place of the "&&" and I think you will be happy with the results...

adrix7
Jun 13, 2008, 11:03 PM
product = number1 * number2 * number3;
cout << "product is:" << product << endl;

//smallest = number1 < number2 and number3;
//cout << "smallest # is:" << smallest << endl;

if (number1 < number2 && number3)
cout << "smallest:" << number1 << endl;

if (number2 < number3 && number1)
cout << "smallest:" << number2 << endl;

if (number3 < number1 && number2)
cout << "smallest:" << number3 << endl;

//greatest = number1 > number2 or number3;


if (number1 > number2 && number3)
cout << "greatest:" << number1 << endl;

if (number2 > number3 && number1)
cout << "greatest:" << number2 << endl;

if (number3 > number1 && number2)
cout << "greatest:" << number3 << endl;

return 0;

}

// hi, this is what i did..

//still got this result..

The Debugger has exited with status 0.
[Session started at 2008-06-13 22:58:05 -0500.]
Please enter 1st number: 34
Please enter 2nd number:3
Please enter 3rd number:2
sum is:39
ave is:13
product is:204
smallest:2
greatest:34
greatest:3

aross99
Jun 13, 2008, 11:21 PM
Go back and take a look at what I showed you - you didn't copy it correctly...

Programming like this is all about the details. One little error and things that you think are OK, can be completely different. These types of bugs are very hard to find also, because you typed what you MEANT to, but it doesn't do what you THINK it does.

Look at my example again and you will see what you did wrong...

adrix7
Jun 13, 2008, 11:48 PM
thanks! i got it i have single result in the greatest integer...

however it's not giving me the highest value..

//output

The Debugger has exited with status 0.
[Session started at 2008-06-13 23:36:33 -0500.]
Please enter 1st number: 34
Please enter 2nd number:23
Please enter 3rd number:56
sum is:113
ave is:37
product is:43792
smallest:23
greatest:34

The Debugger has exited with status 0.
[Session started at 2008-06-13 23:39:23 -0500.]
Please enter 1st number: 23
Please enter 2nd number:34
Please enter 3rd number:56
sum is:113
ave is:37
product is:43792
smallest:23
greatest:34

//code
product = number1 * number2 * number3;
cout << "product is:" << product << endl;

//smallest = number1 < number2 and number3;
//cout << "smallest # is:" << smallest << endl;

if (number1 < number2 && number1 < number3)
cout << "smallest:" << number1 << endl;

if (number2 < number1 && number2 < number3)
cout << "smallest:" << number2 << endl;

if (number3 < number1 && number3 < number2)
cout << "smallest:" << number3 << endl;

//greatest = number1 > number2 or number3;

if (number1 > number2 && number1 < number3)
cout << "greatest:" << number1 << endl;

if (number2 > number1 && number2 < number3)
cout << "greatest:" << number2 << endl;

if (number3 > number1 && number3 < number2)
cout << "greatest:" << number3 << endl;

return 0;

}

wlh99
Jun 15, 2008, 12:17 PM
thanks! i got it i have single result in the greatest integer...

however it's not giving me the highest value..

//output


You are finding the middle number. Look at aross99's example again. While doing so consider the following English statement.

"If number one is greater than number two, and number one is also greater than number three, then number one is the greatest."

adrix7
Jun 15, 2008, 03:18 PM
oh my god how did i miss that! due to my laziness i copy and paste those commands (smallest and greates) thats why i've retained the < less than character :D


got to be careful next time! learned my lesson:o

by the way does anybody have a link to a C++ dictionary or something conicise to help me understand terms..?


thank you again guys! now im on to chapter 3 at last!!!

aross99
Jun 15, 2008, 03:58 PM
Congratulations on getting your program to work. :)

As far as a C++ dictionary, I would suggest googling for one, or for the terms you are looking for...

lee1210
Jun 15, 2008, 05:52 PM
http://www.cprogramming.com/reference/
That is a C/C++ syntax reference, which is probably as good as you are going to get outside of the book "The C++ Programming Language" by Bjarne Stroustrup.

-Lee

lazydog
Jun 16, 2008, 03:56 AM
Here's another useful site http://www.cppreference.com/.

b e n

adrix7
Jun 17, 2008, 01:21 PM
hi im having his result failed results any idea what i did wrong? thanks:)

here are my codes:


// for the main file

#include <iostream>
using std::cout;
using std::endl;

#include "GradeBook.h"

GradeBook::GradeBook (string name)
{
setCourseName (name);
}

void GradeBook::setCourseName( string name)
{
courseName = name;
}

string GradeBook::getCourseName()
{

return courseName;
}

void GradeBook::displayMessage()

{
cout << "Welcome to the grade book for \n" << getCourseName()
<< "!" << endl;

}





// for the class file
#include <string>

using std::string;

class GradeBook
{
public:

GradeBook (string);
void setCourseName( string);
string getCourseName();
void displayMessage();

private:

string courseName;
};

adrix7
Jun 25, 2008, 01:53 PM
hi, can anyone tell what is this annoying error about and what should i do? thanks!!:)

lazydog
Jun 25, 2008, 02:36 PM
Those double quotes look a bit strange. Not sure if you are using the correct character for double quotes or if it is just the font you are using. But the error message implies that the double quotes are wrong.

b e n

By the way, instead of listing all the methods/members of a namespace you want to use, you can pull the whole namespace in with the line

using namespace std ;

iSee
Jun 25, 2008, 02:48 PM
It looks OK to me?????

Are the double-quotes around the < and > generic double-quotes?
They look like they might be the directional ones, which wouldn't be valid in C/C++.

If you're not sure, try deleting and retyping them.

Edit: beat to the punch.

adrix7
Jun 25, 2008, 06:23 PM
:Dyou were right guys its the double quotes, changed it and got it working:)


been trying to figure how to get the functions working.. such as name change.. and character parameters.. etc:confused:

would you mind checking this out:D


thanks again:)

lazydog
Jun 26, 2008, 03:10 AM
Okay have had a quick look. Looks like you are missing a constructor:-


int main()
{

GradeBook gradeBook1 ("CSO1 Introduction to C++ Programming", "Mona Cherri");


This needs the following constructor:-

In gradeBook.h:-


public:
GradeBook( const & string, const & string ) ; // consructed with course and instructor name.


In gradeBook.cpp


GradeBook::GradeBook( const string & course_name, const string & instructor_name )

: courseName( course_name ), instructorName( instructor_name ), yesName( "" )

{
}


Your code is okay but it is best to initialise class members in the above way. Also, I've used references to const strings as the parameters. It's a good idea to pass by reference rather than by value whenever possible. This avoids unnecessary copies. The const bit is there to enforce the requirement that the constructor/method should not alter the value of the variable (which it could since it is passed as a parameter by reference).

Also, if you end up using lots of things in std and many files, you're going to get sick of typing using std::xxx, so just use


using namespace std ;


One final point, it's common practise in C++ to distinguish between class member variables and temporary variables in code. For example I would rename your Gradebook members like this:-


private:
string course_name_ ; // course name for this GradeBook
string instructor_name_ ;
string yes_name_ ;


This has a couple of benefits:- when you look at your code it's easy to check that you are modifying a member value (rather than a local variable), secondly, it avoids having to juggle parameter names in methods when you want to set a member value. Here's an example of what I mean:-


GradeBook::GradeBook( const string & course_name, const string & instructor_name )

: course_name_( course_name ), instructor_name_( instructor_name ), yes_name_( "" )

{
}


If you don't like using _ there are other popular schemes one of which is using an 'm' before the variable name, eg mCourseName, mInstructorName etc.


b e n

adrix7
Jun 26, 2008, 12:43 PM
hi, can you please follow up on what i did? i cant seem to get rid of the errors:confused: thank you for you patience:D

lee1210
Jun 26, 2008, 12:50 PM
It would be much more helpful if you copy that source code and paste it in here in CODE blocks.

Include your .h file as well.

-Lee

lazydog
Jun 26, 2008, 12:58 PM
Hi

Looks to me as though gradebook.h is missing the declaration of the new constructor.

The other 3 errors are just typos, you're using name which is not one of the parameters passed to the constructor. But then again those 3 lines are redundant because course_name_, instructor_name_ and yes_name_ are all initialised anyway because they are in the initialisation list ( the initialisation list is the stuff after the : and before the opening {.

b e n