View Full Version : Floundering in C++
Forsaken_One
02-08-2006, 9:46 PM
Well, I am trying to learn C++. From what I've read, and who I have spoken too, it is a good language to start with. I have purchased 2 books on C++, but I was wondering if there is anything else I should do.
Reason I am making this thread is, it's hard to sit there and just read through a book on how to do something. I mean, I can do it. I just want to know if there is anything a little more "hands-on"? If there is, maybe you can point me in the right direction.
What I am looking for, is a more visual approach to C++. If that is even possible...
Haplo
02-08-2006, 11:15 PM
C++ is a non-visual language. I suggest www.cprogramming.com and reading through the various sections. Learn C FIRST and then work up to C++. The basic differences are very minor, but if you attempt anything objective before having a full grasp of C, you WILL fail, lose interest, and give up.
Learn C FIRST and then work up to C++. The basic differences are very minor, but if you attempt anything objective before having a full grasp of C, you WILL fail, lose interest, and give up.
No, you can learn C++ completely independent of C. There's nothing important you would miss by skipping it and I'd even go as far as saying that trying to learn C first is a waste of time. As long as you learn C++ from scratch and not from books/resources that try to teach C++ to C programmers you'll be fine.
Unfortunately this might come too late since you already bought your books, but C++ isn't exactly a beginner-friendly language, especially if you have no previous programming experience. As I said in another thread here, languages like Rexx, Python, and Ruby will have you doing more interesting stuff faster.
Haplo
02-08-2006, 11:36 PM
I wouldn't ever suggest ruby to a beginner. I'ts different enough from other languages to be confusing if you actually try to learn anything else afterwards.
He's right though, you can start from scratch learning C++, but doing it procedurally. Just avoid anything relating to classes or objects. If you'd like, I can give you some modified assignments from the labs I had last semester in C++. If you do them in order as you learn, you'll learn a lot more.
I wouldn't ever suggest ruby to a beginner. I'ts different enough from other languages to be confusing if you actually try to learn anything else afterwards.
It's the ideas that matter though, not the syntax. Once you have the programming concepts under your belt learning another language is as easy as learning the syntax. Ruby's also one of the more popular languages at the moment so it wouldn't be a bad thing to know. That said, I think Rexx has the most elegant syntax out of any language I've used and would probably be one of the best options.
Dark2004
02-09-2006, 8:36 AM
C++ is a hard language. But if you utilize it, it is very powerful.
Forsaken_One
02-09-2006, 8:39 AM
@Haplo
That would be great. All the help I can get, will be great. :)
@Timp
The reason I started with C++ was reading the countless posts here and elsewhere, that siad it would be batter to start there. Because it better suites you in he long run.
But, I am not easily intimidated, so things should work out. Especially after me forking $50 out on books. ;)
Modred
02-09-2006, 2:47 PM
Especially after me forking $50 out on books.
That's nothing. My C++ textbook for both last semester and this semester cost $150.
Well, C++'s syntax is very similar to other widely used languages such as java. I like C++'s contraints better than C's.
-I don't know about Rexx, but Objective-C has a pretty elegant syntax and a very powerful objective system.
Mindslaver
02-09-2006, 7:03 PM
Stick with C++. You seem committed, so don't change paths now. Once you know C++, you're all set for Python, Ruby, Perl, and other scripting languages. The converse cannot be asserted. Try using the USACO Training (http://train.usaco.org/usacogate) if you want some practice problems. That's nothing. My C++ textbook for both last semester and this semester cost $150. What textbook is it?
Modred
02-09-2006, 7:49 PM
What textbook is it?
Starting Out With C++, 4th ed. - Tony Gaddis
It's rather good, but also take into account that it was purchased new from a school book store.
I suppose what I'm trying to get at is that programming is all about using the right tool for the right job and despite what some people may insist, there is no single superior language. It all depends on what you want to do. Too many times I've heard people say they want to make games which leads to an instant C++ recommendation plus DirectX/OpenGL/graphics library of the week, but they end up getting frustrated (as Forsaken_One seems to be) and just stop. I think it's better to take a step by step approach to programming. I started with QBasic and the positive experience I had with using it encouraged me to expand my horizons. I can't imagine starting out with C or C++ and still being interested in programming. :D
Forsaken_One
02-09-2006, 8:15 PM
Well today I read about 80 pages, abd practiced in my notebook. Since I was in school. :P
But, things are starting to clear up. It's rather easy, Imo. But, I'll see as I get farther down the road. But as far as the syntax goes, it's a big jump at first... But, "It's a piece of pie!"
Thanks for the help everyone. I'll post back in awhile, with my first program. :)
Modred
02-09-2006, 8:48 PM
I can't imagine starting out with C or C++ and still being interested in programming.
Several people in my c++ classes are frustrated with the language, and at least one has changed her major as a result.
So yeah, be careful trying to take in too much at one time. Take it slow and make sure you can implement something before you jump ahead to a new topic.
Forsaken_One
02-09-2006, 9:09 PM
One thing I do know, is that I will stay within my boundries. :P
Question about Dev-C++
When you are done compiling and everything works alright... I launch the program, and enter the right information... As in, the prompt asks me for my age. I enter "63". It should tell me, I need to pay for it. Instead, I hit enter, ant I exit out of the window... Here is the code.
//
//
#include <iostream>
using namespace std;
int main(void)
{
int age;
cout << "Enter your age: ";
cin >> age;
if (age > 12)
if (age >=65 )
cout << "YAY! You = teh free!";
else
cout << "H4x!11! j00 n33|) 70 P4Y11!1" ;
else
cout << "j00 are teh OLD, so j00 get in for teh free!11!";
return 0;
}
I don't use Dev-C++ but I'm guessing the issue is the dialog box closes itself as soon as the program's done running. I would look through the preferences for something pertaining to that.
Forsaken_One
02-09-2006, 10:24 PM
Hmm... I guess I will have to search for it I guess...
Modred
02-09-2006, 10:38 PM
This is a Windows problem. When you call a command-line executable from within the Windows GUI environment, the DOS shell closes immediately upon program termination. Open a command prompt and cd to the directory that contains your program and then type program.exe (of course, type the correct filename for the program).
Also, I see a logic error with your if structure, but I'll let you try and figure it out on your own.
EDIT: I assumed you were clicking on the program in Windows Explorer.
Haplo
02-09-2006, 11:34 PM
Here's a hint, if you don't bracket your if statements, it doesn't do what you're trying to make it do. And yes, you're probably seeing the windows/dos problem.
Forsaken_One
02-10-2006, 12:16 AM
w00t!
Guess who figured some more shtuff out!
Go to teh movies. :P
//
//
#include <iostream>
using namespace std;
int main(void)
{
int age;
cout << "Enter your age: ";
cin >> age;
if (age > 12)
if (age >=65 )
cout << "YAY! You = teh free!";
else
cout << "H4x!11! j00 n33|) 70 P4Y11!1" ;
else
cout << "j00 are teh OLD, so j00 get in for teh free!11!";
system("PAUSE");
return 0;
}
Celsius to Fahrenheit:
//
// Program to convert temperature from Celsius into Fahrenheit.
// Fahrenheit = Celsius * (212 - 32)/100 +32
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
// Enter the tenperature in Celsius
int celsius;
cout << "Enter the tenperature in Celsius.";
cin >> celsius;
// Calculate the factor
int factor;
factor = 212 - 32;
//
// Convert
//
//
int fahrenheit;
fahrenheit = factor * celsius/100 + 32;
//Output
cout << "Fahrenheit value is: ";
cout << fahrenheit << endl;
system("PAUSE");
return 0;
}
Modred
02-10-2006, 2:07 AM
If I were to write that second program, it would be something like this:
#include<iostream>
using namespace std;
int main()
{
float celsius;
cout << "Enter the temperature in degrees Celsius: ";
cin >> celsius;
cout << "Fahrenheit value is: ";
cout << (celsius*9/5+32) << " degrees.\n";
return 0;
}
Yes, I used a different forumula, but that's beside the point. My program is much shorter and uses less memory (only one variable). I have no idea why you're including cstdio or cstdlib or passing an int and a pointer to a char to main.
Actually, if I were writing this program I'd definitely include iomanip and format the output with fixed, showpoint, and setprecision(). But that's a bit much for the concept I was trying to get across.
Memory usage is largely irrelevant on modern computers. All in all we're talking about less than one kilobyte of memory for variable usage. I find both programs to be readable, but I've seen cases where a "clever" programmer tries to minimize variable usage and the amount of code and while it works, it's a horrible mess to read. For most applications, memory usage and especially code length is irrelevant. Most optimizing compilers will output equally efficient machine code anyways.
Modred
02-10-2006, 3:07 PM
I'll agree with you Tim that memory usage is largely unimportant, especially in a program so small. But there's no need to store everything in variables in such a simple program either.
Forsaken_One
02-10-2006, 3:10 PM
Well, I am almost through with my C++ book. "Soaked it in like a 'Sponge!" :P
I would say in anout 2 days, I'll be ready to start my basic program. :P I'll have a couple of questions on how to do a couple of things in a few hours, I have about 40 more pages to read. :P Then the "Final-Exam" to see how I do. Then I will start! Yay!
I've always been a printf man myself. And, you say 'memory usage is irrelevant' but you ALWAYS have to keep the time<->space continuum in mind when programming, else you end up programming like those idiots who teach comp science classes, or like microsoft. efficiency >> no efficiency. I'm sure Adobe said the same thing for photoshop, and it's a massive peice of crap.
BTW
#include<iostream>
using namespace std;
int main()
{
float celsius;
printf("Enter the temperature in degrees Celsius: ");
cin >> celsius;
printf("Fahrenheit value is: %.2f degrees.", ((celsius*9/5)+32));
return 0;
}
The parentheses are kinda picky to get it right, and the printf with the %.2f makes it format the float to 2 decimal places.
Mindslaver
02-11-2006, 12:53 PM
Be careful when using Dev-C++. If you neglect to write system("PAUSE");
right before your call to return() or exit(), the application will quit without doing anything. This is generally a problem only for console applications.
Ah. Well I use a mac, and terminal has no such problem. For testing in XCode it displays the terminal output in a screen where it displays all input/output and the resulting code of the application, which doesn't clear until I tell it to. Regular terminal does the same thing.
Forsaken_One
02-12-2006, 6:32 PM
Ok, time for me to get some help...
After using this code from widgets:
/////////////////////////////////////////////////////////////////////////////
// Name: minimal.cpp
// Purpose: Minimal wxWindows sample
// Author: Julian Smart
// Modified by:
// Created: 04/01/98
// RCS-ID: $Id: minimal.cpp,v 1.57 2002/04/07 21:12:45 JS Exp $
// Copyright: (c) Julian Smart
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// ================================================== ==========================
// declarations
// ================================================== ==========================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWindows headers)
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
// ----------------------------------------------------------------------------
// resources
// ----------------------------------------------------------------------------
// the application icon (under Windows and OS/2 it is in resources)
#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
#include "mondrian.xpm"
#endif
// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------
// Define a new application type, each program should derive a class from wxApp
class MyApp : public wxApp
{
public:
// override base class virtuals
// ----------------------------
// this one is called on application startup and is a good place for the app
// initialization (doing it here and not in the ctor allows to have an error
// return: if OnInit() returns false, the application terminates)
virtual bool OnInit();
};
// Define a new frame type: this is going to be our main frame
class MyFrame : public wxFrame
{
public:
// ctor(s)
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
long style = wxDEFAULT_FRAME_STYLE);
// event handlers (these functions should _not_ be virtual)
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
private:
// any class wishing to process wxWindows events must use this macro
DECLARE_EVENT_TABLE()
};
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// IDs for the controls and the menu commands
enum
{
// menu items
Minimal_Quit = 1,
// it is important for the id corresponding to the "About" command to have
// this standard value as otherwise it won't be handled properly under Mac
// (where it is special and put into the "Apple" menu)
Minimal_About = wxID_ABOUT
};
// ----------------------------------------------------------------------------
// event tables and other macros for wxWindows
// ----------------------------------------------------------------------------
// the event tables connect the wxWindows events with the functions (event
// handlers) which process them. It can be also done at run-time, but for the
// simple menu events like this the static method is much simpler.
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
EVT_MENU(Minimal_About, MyFrame::OnAbout)
END_EVENT_TABLE()
// Create a new application object: this macro will allow wxWindows to create
// the application object during program execution (it's better than using a
// static object for many reasons) and also declares the accessor function
// wxGetApp() which will return the reference of the right type (i.e. MyApp and
// not wxApp)
IMPLEMENT_APP(MyApp)
// ================================================== ==========================
// implementation
// ================================================== ==========================
// ----------------------------------------------------------------------------
// the application class
// ----------------------------------------------------------------------------
// 'Main program' equivalent: the program execution "starts" here
bool MyApp::OnInit()
{
// create the main application window
MyFrame *frame = new MyFrame(_T("Minimal wxWindows App"),
wxPoint(50, 50), wxSize(450, 340));
// and show it (the frames, unlike simple controls, are not shown when
// created initially)
frame->Show(TRUE);
// success: wxApp::OnRun() will be called which will enter the main message
// loop and the application will run. If we returned FALSE here, the
// application would exit immediately.
return TRUE;
}
// ----------------------------------------------------------------------------
// main frame
// ----------------------------------------------------------------------------
// frame constructor
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
: wxFrame(NULL, -1, title, pos, size, style)
{
// set the frame icon
SetIcon(wxICON(mondrian));
#if wxUSE_MENUS
// create a menu bar
wxMenu *menuFile = new wxMenu;
// the "About" item should be in the help menu
wxMenu *helpMenu = new wxMenu;
helpMenu->Append(Minimal_About, _T("&About...\tF1"), _T("Show about dialog"));
menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
// now append the freshly created menu to the menu bar...
wxMenuBar *menuBar = new wxMenuBar();
menuBar->Append(menuFile, _T("&File"));
menuBar->Append(helpMenu, _T("&Help"));
// ... and attach this menu bar to the frame
SetMenuBar(menuBar);
#endif // wxUSE_MENUS
#if wxUSE_STATUSBAR
// create a status bar just for fun (by default with 1 pane only)
CreateStatusBar(2);
SetStatusText(_T("Welcome to wxWindows!"));
#endif // wxUSE_STATUSBAR
}
// event handlers
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
// TRUE is to force the frame to close
Close(TRUE);
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxString msg;
msg.Printf( _T("This is the About dialog of the minimal sample.\n")
_T("Welcome to %s"), wxVERSION_STRING);
wxMessageBox(msg, _T("About Minimal"), wxOK | wxICON_INFORMATION, this);
}
The thing I don't know, is where to even start looking... What I want to do(or at least get a few "hints" as to what I should do) is when you select File.
I do not know how to add other things in there...
Now, what I am asking, is for you not to do the coding for me, but kind of guide me through what I should be doing...
You're jumping way ahead. You need to hit up if/for/while/do while loops and then successfully use structs and then classes before you should go jumping head first into a win32 GUI application.
Forsaken_One
02-12-2006, 6:52 PM
I finished my book yesterday. So, I am familiar with the very basics of structures. Which is where my book ended...
And with the program, it won't be doing anything "crazy". I just want it to spit out some text. To make my life easier when making things that involve many variables.
Well, win32 is where I stop. I'm mainly an Objective-C/Cocoa programmer, and doing GUIs in that is far different (and easier) than in win32.
Forsaken_One
02-12-2006, 7:21 PM
So, any recomendations as to what I should look for?
Have you looked at the documentation? There are some tutorials there too.
http://www.wxwindows.org/docs.htm
Interesting, a cross platform interface library o.0
vBulletin® v3.7.2, Copyright ©2000-2008, Jelsoft Enterprises Ltd.