www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/07/04/11:10:31

From: "Al Morgan" <muaddib AT proaxis DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Newbie problems... Whats wrong with this code?
Date: Sat, 3 Jul 1999 22:31:31 -0700
Organization: Posted via RemarQ, http://www.remarQ.com - The Internet's Discussion Network
Lines: 168
Message-ID: <931066419.823.65@news.remarQ.com>
References: <Q4xf3.524$nk6 DOT 2262 AT news1 DOT online DOT no>
NNTP-Posting-Host: 206.163.140.92
NNTP-Posting-Date: Sun, 04 Jul 1999 05:33:39 GMT
X-Trace: 931066419.823.65 4VELK9AUP8C5CCEA3C qube-01.us-ca.remarq.com
X-Complaints-To: newsabuse AT remarQ DOT com
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2314.1300
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Tim Hansen <tmdp AT online DOT no> wrote in message
news:Q4xf3.524$nk6 DOT 2262 AT news1 DOT online DOT no...
> Hi!
> I decided to learn C++ today and I've already got some problems so I hope
> someone could point me in the right direction?
>
> I found a tutorial on the web called 'From the ground up: a guide to c++'
> and sort of got the hang of things so I decided to make up some of my own
> code (based on info I got from here, but my little program was too big for
> the Java C++ compiler so I had to find a real compiler (enough background
> info yet???). I've now installed Djgpp and it seems to be up and running
> fine (I tried out the hello world program in the user manual and that
worked
> like a dream! however...).
>
> Ok, to cut a long story short. I've tested my program. Way to many errors
> though. What I was wondering is wether it is my code that is wrong or if
> I've set up Djgpp wrong? To compile my file I used 'gxx -o test.exe
test.cc'
> the code I've wrote (yeah yeah, I know it's probably crap, you don't need
to
> tell me!) is:
>
> void main(void)
>
>

>
>  char Venner[2];
>  int Alder[2];
>  int GjennomsnittAlder;
>  char Youngest;
>  char Oldest;
>
>   Venner[0] = "David";
>   Venner[1] = "Stig";
>   Venner[2] = "Eirik";
>
>   Alder[0] = "21";
>   Alder[1] = "28";
>   Alder[2] = "26";
>
>   GjennomsnittAlder = (Alder[0] + Alder[1] + Alder[2]) / 3;
>
>   if (Alder[0] < Alder[1] && Alder[2])
>    Youngest = Venner[0];
>   if (Alder[1] < Alder[0] && Alder[2])
>    Youngest = Venner[1];
>   if (Alder[2] < Alder[0] && Alder[1])
>    Youngest = Venner[2];
>
>
>   if (Alder[0] > Alder[1] && Alder[2])
>    Oldest = Venner[0];
>   if (Alder[1] > Alder[0] && Alder[2])
>    Oldest = Venner[1];
>   if (Alder[2] > Alder[0] && Alder[1])
>    Oldest = Venner[0];
>
>   switch(Youngest)
>   {
>    case 'David' : cout << "Hey David is the youngest!" << endl;
>    break;
>
>    case 'Stig' : cout << "Stig is the youngest???" << endl;
>    break;
>
>    case 'Eirik' : cout << "Is Eirik really the youngest???" << endl;
>    break;
>   }
>
>   switch(Oldest)
>   {
>    case 'David' : cout << "David is the oldest???" << endl;
>    break;
>
>    case 'Stig
> ' : cout << "Are you sure Stig is the oldest?" << endl;
>    break;
>
>    case 'Eirik' : cout << "Eirik is the Oldest!!!" << endl;
>    break;
>   }
>
> }
>
> Mostly the error messages say stuff like 'assignment from char to char
lacks
> a cast' and 'character assignment too long' also 'cout' undeclared...
>
> So what am I doing wrong, should I find another tutorial or have I set up
> Djgpp wrong. Thanks for the help (in advance ;-) C'ya!
>
> Tim Hansen.
>
>

This confused me at first... But are you writing this in swedish or german
or something?

Ok, the first problem I see is this:

>   if (Alder[0] < Alder[1] && Alder[2])
>    Youngest = Venner[0];

The way you have it says the following:
if alder[0] is less than alder[1] AND alder[2] is true (not zero) then blah
blah blah...

I don't think that's what you want, you want something like this:

if (Alder[0] < ALder[1] && Alder[0] < Alder[2])
    Youngest = Venner[0];

You have to write out the whole thing for each condition.

Second problem:

A 'char' is a single character, i.e. char Verner[2] could be used as a
string of two characters, or an array with two elements, each holding one
char.  The easy way to use strings is like this:

char Verner[20]; // this would hold a stingle string of 60 characters. or,
even better:
char Verner[2][20];  This would hold an array of two strings.  The way you
have your program written, this is how you would want to do it.

There is another way, involving pointers, but I don't want to throw that
stuff at you yet.

The third and final problem (that I saw), is this:

>    case 'David' : cout << "Hey David is the youngest!" << endl;

This would be legal in a language like QBasic, but not in C++.  in C++ there
are no built i string processing functions, so you can't just say

myName = "Bill";

There are, however, header files that do that, "string.h" I belive.  Using
these functions, you don't want a case statement, rather, a big if() one,
like this:

The function you need in strcmp(char *str1, char *str2);  It returns 0 if
both strings are equal (counter intuitive, isn't it?).  So you'd use it like
this:

#include <string.h>

if(!strcmp(Youngest, "Erik"))
    cout << "Erik is the youngest!";

! is NOT in C++, if you don't know it, it changes true to false and vice
versa.

An even better way, though, would be like this:

cout << Youngest << " is the youngest!";
cout << Oldest << " is the oldest!";

instead of all the casing.

I hope all this is clear, and it helps.

    Al [ muaddib AT proaxis DOT com ]


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019