Welcome, Guest. Please login or register.

Author Topic: I need help with C(++)  (Read 4744 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline z5Topic starter

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 366
    • Show only replies by z5
    • http://ada.untergrund.net
Re: I need help with C(++)
« Reply #14 from previous page: April 05, 2003, 05:47:44 PM »
bingo  :-o

You're my hero Karlos  :-D

Seriously though, you seem to know a lot about programming, yet reading some threads i have the impression that you don't do it for a living? Why not?
A.miga D.emoscene A.rchive: Relive the dreams...
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: I need help with C(++)
« Reply #15 on: April 05, 2003, 05:55:13 PM »
Another thing, if the user enters more than 80 chars for your gets() youre asking for trouble.

To limit the input to a max of 79 chars, use something like

int len;

fflush(stdin);
len = fgets(klant, 79, stdin);
if (len && klant[len-1] == '\n')
   klant[len-1] = '\0'; /* nuke the newline */


This is just the same as in te previous example with the FixedStringBuffer...
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: I need help with C(++)
« Reply #16 on: April 05, 2003, 06:01:39 PM »
Quote

z5 wrote:
bingo  :-o

You're my hero Karlos  :-D




Aww, gee :-)



-edit-

Actually, you don't generally see this problem under AmigaOS because the interactive stdin/stdout generally flush automatically when changing from input to output and vice versa. This isn't true an all plafroms. However, fflush(stdin) won't hurt on platforms that don't require it so your code stays portable.

-edit-


Quote

Seriously though, you seem to know a lot about programming, yet reading some threads i have the impression that you don't do it for a living? Why not?


No more than many who frequent this place. I don't have much formal qualifications in software (I ado have a masters degree in chemistry though), but have been coding since I was six (ZX81!)..

I'd actually like to work in software development but the industry here in the UK isn't exactly crying out for programmers just now.

Hence the shift to web developemnt, which is having just as rough a time but with slightly better prospects. Just approaching the end of a six month  work-based intensive course in the latter.

My guess is that a web designer who is comfortable programming is slightly more sought after than either a programmer or pure web designer.
int p; // A
 

Offline Jose

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show only replies by Jose
Re: I need help with C(++)
« Reply #17 on: April 05, 2003, 07:05:40 PM »
"...I ado have a masters degree in chemistry though.."
I guess that industry is even worse!?
Seriously how come you take a masters and don't work in a related area :-?

It's just by curiousity, I'm finishing my degree and don't see much perspectives either (agronomy) :-D
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: I need help with C(++)
« Reply #18 on: April 05, 2003, 07:14:13 PM »
I was following it with a PhD, but during that time the industry ended up in the crappest state ever. Every merger between the big chemical companies seemed to lose jobs rather than create them. The IT industry is suffering along the same vein currently.

To get a halfway decent job a PhD wansnt enough any more - I knew guys with post docs / industrial experience in the same job application queues as fresh PhD graduates.

Who would you give the job to?

-edit-

Also, I got scooped by the Japanese in my project, which didn't help :-)

So, having a good grasp of IT, I jumped ship. Right as the sodding .com bust happened!

So here I am :-(
int p; // A
 

Offline Jose

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show only replies by Jose
Re: I need help with C(++)
« Reply #19 on: April 05, 2003, 07:53:03 PM »
Maybe I should have started as a  construction worker some years ago. I have friends that earn more doing basic things in there  than some people with Masters, and they have plenty time to rumble and don't have many worrries  :-D
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline z5Topic starter

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 366
    • Show only replies by z5
    • http://ada.untergrund.net
Re: I need help with C(++)
« Reply #20 on: April 14, 2003, 06:11:53 PM »
Can anybody explain the following problem (it's driving me nuts) in C:

When i do 0.20 / 0.20 and put the the result in a float, i get 1.00 (which is great  :-) )

When i do the same and put the result in an integer, i get 0 (:-x). So i'm doing int = float / float

Same goes for:
 0.20/0.10 results in 2 when i put it in a float
 0.20/0.10 results in 1 when i put the result in an integer??
A.miga D.emoscene A.rchive: Relive the dreams...
 

Offline Kronos

  • Resident blue troll
  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 4017
    • Show only replies by Kronos
    • http://www.SteamDraw.de
Re: I need help with C(++)
« Reply #21 on: April 14, 2003, 06:15:05 PM »
Because that is just how it is .......

Must have something to do how and when data is tranformed
from float to int. Better learn to live with it.
1. Make an announcment.
2. Wait a while.
3. Check if it can actually be done.
4. Wait for someone else to do it.
5. Start working on it while giving out hillarious progress-reports.
6. Deny that you have ever announced it
7. Blame someone else
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: I need help with C(++)
« Reply #22 on: April 14, 2003, 06:19:36 PM »
Sounds like truncation error.

Try an explicit cast, eg

float a, b;
int x;
...
x = (int)(a/b);

Or, you could write a round() function that does

int round(float x)
{
   if (x>0.0) return (x+0.5);
   if (x<0.0) return (x-0.5);
   return 0;
}
int p; // A
 

Offline z5Topic starter

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 366
    • Show only replies by z5
    • http://ada.untergrund.net
Re: I need help with C(++)
« Reply #23 on: April 14, 2003, 06:28:00 PM »
The cast doesn''t work.

Is there a function in c where i can drop everything behind the comma,
eg
float 5.20 => 5 but there must be no rounding up.?
A.miga D.emoscene A.rchive: Relive the dreams...
 

Offline PiR

  • Full Member
  • ***
  • Join Date: Apr 2003
  • Posts: 148
    • Show only replies by PiR
Re: I need help with C(++)
« Reply #24 on: April 14, 2003, 07:32:38 PM »
Hi

The explanation is quite simple:
Due to the roundings done in floating point calculations 0.2 / 0.2 is not really 1.0, but 0.9999....
As you see the default rounding method of your compiler is 'towards zero', in other words 'cut off everything after dot', so you got your 0 integer.

I think I could give you one more warning.
Because of the nature of relation between binary and decimal, it is not even possible to represent numbers, like 0.2 in float preciselly. If you checked it more carrefully you can find you they are ROUNDED to the nearest representablefloat, that is somewhat like 0.2000(...)2.

If being precise is 'a must' for you (things like indexing arrays), using floating points is not the best idea. You should remember that these representation is always treated as 'the value nearby X'.

Good luck
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: I need help with C(++)
« Reply #25 on: April 14, 2003, 08:42:22 PM »
Quote

z5 wrote:
The cast doesn''t work.

Is there a function in c where i can drop everything behind the comma,
eg
float 5.20 => 5 but there must be no rounding up.?


In math.h I think there are floor() and ceil() functions that do what your'e looking for.

As I recall,

double floor(double x)

Returns the largest integer value (expressedl as a double) not greater than x

double ceil(double x)

Returns the largest integer (expressed as a double) not less than x

ie

floor(-3.5) => -4.0
floor(3.5) => 3.0
floor(-2.0) => -2.0
floor(2.0) => 2.0

ceil(-3.5) => -3.0
ceil(3.5) => 4.0
ceil(-2.0) => -2.0
ceil(2.0) => 2.0


Hope this helps
int p; // A
 

Offline iamaboringperson

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 5744
    • Show only replies by iamaboringperson
Re: I need help with C(++)
« Reply #26 on: April 15, 2003, 01:18:35 AM »
this should be under 'AmigaOS development'