Re: First C++ Project

From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 10/08/03


Date: Wed, 08 Oct 2003 04:06:31 GMT


"Brian K" <compgeek6@hotmail.com> wrote in message
news:fbe85a.0310071824.eabd36f@posting.google.com...
> I am new to C++ and taking an intro course. The first project
> consists of drwing a box around a screen and then drawing two
> intersecting lines.

I feel that an introductory course on C++ that starts
using nonstandard libraries such as for graphics, is
a *very* poor course. You should be taught the *language
only* first, only later be taught to add on platform
specific stuff like graphics.

I would find it ok if you were doing this 'drawing'
with text characters, such as _ - + | etc.

Standard C++ i/o only deals with characters, not
graphics.

OK, done with my rant. :-) Read on.

>
> So far i have the following code but am not able to get it to compile.
> I am not sure where my errors are, but figure they are probaly deeper
> then the two i am seeing and the warning.
>
> Here is the code I have (I am using VIsual C++)
>
> Please be kind, as I am quite the newbie!! I don't want someone to do
> my homework for me, just let me know how to get it working on my own.
> This site was highly reccomended for that reason!
>
>
> Thanks
> CG
>
>
>
> # include <c:\program files\microsoft visual
> studio\myprojects\homework2\grafix.cpp>
> # include <c:\program files\microsoft visual
> studio\myprojects\homework2\grafix.h>

1. Neither of these two headers are part of standard C++.

2. In order for us to diagnose a program using them, we
   need to seem their contents, or at least a good
   description of any entities (e.g. functions) declared
   by them that you use in your program.

3. The #include of a ".cpp" file raises a "red flag".
   You can use #include to include any file you like,
   but a file with ".cpp" extension is traditionally
   used for object definitions, and should be linked
   with, not #included. (If more than one source file
   #include-ing it are linked together, and it contains
   object definitions, you'll be violating the "one
   definition rule (aka ODR)".

4. You say you are unable to get this to compile. But
   you do *not* tell us what kind of problems you're
   having (e.g. what error messages, referring to
   which specific lines). Without that, we can only
   make general assertions about the language, and
   educated guesses, as I'll do below.

> using namespace std;

The namespace 'std' is the namespace reserved by the language
for standard library identifiers (declared by standard headers,
non of which you've #include-d here.) So (unless one or
both of the headers you did #include has #include directive(s)
naming standard headers, this statement has no effect.

>
> main ()

int main()

>
> int x1 (x1);

Here you define an object named 'x1', and initialize it
with the value of another object named 'x1'. Where is
this other object defined? Doesn't the fact that both
objects have the same name look odd to you? :-)

> int x2 (x2);
> int y1 (y1);
> int y2 (y2);

Ditto for these.

What I suspect you really want is simply:

int x1;
int x2;
int x3;
int x4;

or give them sensible initial values such as zero:
(not required, by I always initialize and recommend
 that people do).

int x1(0);
int x2(0);
int x3(0);
int x4(0);

>
> {
>
> void drawScreen ();
> {
> draw line (0,0,1023,0);
> draw line (1023,0,1023,767);
> draw line (1023,767,0,767);
> draw line (0,767,0,0);
> }

I'll guess that that 'draw' is the name of a function
declared by one of the headers you #included. Or is
it 'line'? or is it 'draw_line'? It's really moot,
since the syntax you have used is invalid. Identifiers
(function and variable names etc.) are not allowed to
have whitespace characters in them. The syntax for
a function call is:

functionnamewithnospaces(arguments,separated,by,commas);

>
> void drawLine (intx1,intx2,inty1,inty2);

Aha! Is this perhaps the function you're trying to refer
to above? Note the spelling. :-) Also, once you get
the spelling right, a prototype (which is what this is),
must appear *before* any calls to the function which
it declares.

>
>
> cout << "Enter Value for x1= ";
> cin >>x1;

Neither 'cout' nor 'cin' are visible here, since you
did not #include the header which declares them
(<iostream>). In order to use these, put

#include <iostream>

just before the 'using namespace std;' above.

(If #include <iostream> appears in either or both
of the headers you did #include, this is *extremely
poor* design, and you're being done a huge disservice
being taught this way.

>
> cout <<endl<< "Enter Value for x2= ";

Same issue with 'endl'. Needs #include <ostream>

> cin >>x2;
>
> cout <<endl<< "Enter Value for y1= ";
> cin >>y1;
>
> cout <<endl<< "Enter Value for y2= ";
> cin >>y2;
>
> draw line (x1,x2,y1,y2);

Did you really perhaps mean:

 drawLine(x1,x2,y1,y2);

>
>
> }//EOM

Will you please tell us which school you're attending,
and the name of the course, instructor, and textbook(s)
you're using?

And feel free to ask questions about anything I've
written here.

Also be sure to read this group's FAQ at:
www.tinyurl.com/pxrb

which should help you greatly in determining what is
C++ and what is not, as well as providing much other
useful information for the novice.

(That link is a "temporary" one from Google's cache,
the "official" link seems to be currently out of order.)

HTH,
-Mike



Relevant Pages

  • Custom Draw LIstCtrl and scrolling
    ... I wrote a custom draw CListCtrl that allow to display different row height on windows mobile ... void CListCtrlCommands::OnSize(UINT nType, int cx, int cy) ... CRect rect; ... GetClientRect(&rect); ...
    (microsoft.public.vc.mfc)
  • Re: Graphics help please
    ... I've got something similar, with a 3D turtle. ... You can also draw a line from to, with the ... final int width = 1024; ... static public void main ...
    (comp.lang.java.programmer)
  • RE: glitch when moving/resizing window (lots of drawing)
    ... HRGN getRectRgn(int originX, int originY, int rectWidth, int rectHeight); ... // draw body ... movePlayer = RIGHT; ... playerSpeedY -= gravityAccel; ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Some Advice.
    ... int main{ ... you should use identifiers that are "self-documenting". ... you should provide inclusion guards for headers. ... function definitions into separate source files. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: static hide - the static is not hide
    ... Note that this merely fills the region and doesn't actually draw any ... pdc.FillSolidRect; ... if (isBitmap) // draw Bitmap ... int cxClient = rect.Width; ...
    (microsoft.public.vc.mfc)