Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: I need help with C(++)
« on: April 03, 2003, 09:12:18 PM »
If it's a c++ project, you could just use the string class :-)
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: I need help with C(++)
« Reply #1 on: April 03, 2003, 09:27:29 PM »
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: I need help with C(++)
« Reply #2 on: April 04, 2003, 09:47:31 AM »
One last point,

It's generally not a good idea to declare arrays 'on the stack' like this.

You could make the array static, or allocate space with malloc (or new[] if using C++)

Also, my last example was a bit wrong in that you get the return character as the last char of your string. To fix this you get the number of chars read from fgets() and set the last character-1 to 0

eg
for (...)
{
   int len;
   printf("Type string: ");
   len = fgets(p->data, bufferSize-1, stdin);
   p->data[len-1] = '\0';
}
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: I need help with C(++)
« Reply #3 on: April 04, 2003, 10:53:02 AM »
@Piru

Thanks for that, you are completely correct. Everybody ignore me, I'm not fully awake :-)

I just posted without actually checking the behaviour of fgets() - I did edit my post just now but forgot to check since I'm at work mucking about with god awful vbscript (ewww!)

Anhow, listen to the guy, he knows what he's talking about!
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: I need help with C(++)
« Reply #4 on: April 04, 2003, 10:58:54 AM »
Right, from the top :-D


#include
#include
#include

/* create a string buffer type */

const size_t bufferSize = 80;

typedef struct {
   char data[bufferSize];
} FixedStringBuffer;

void main(void)
{
   const size_t numberofstrings = 5;
   FixedStringBuffer strBuf[numberofstrings], *p;
       
   for(p=strBuf;p   {
      int len;
      printf("Type string: ");
      len = fgets(p->data, bufferSize-1, stdin);
      if (len && p->data[len-1] == '\n')
         p->data[len-1] = '\0';
   }
}

int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: I need help with C(++)
« Reply #5 on: April 04, 2003, 12:00:57 PM »
It's got pointers, a structure and safety bound IO. What more does s/he want ?

:-)

-edit-

As long as you follow it, you can explain it if they get suspicious :-D
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: I need help with C(++)
« Reply #6 on: April 05, 2003, 05:43:58 PM »
There's probalbly some newline chars or whatever left in the input buffer after scanf()

Try sticking fflush(stdin) before your gets() and see if that helps.

int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: I need help with C(++)
« Reply #7 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 all replies
Re: I need help with C(++)
« Reply #8 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 Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: I need help with C(++)
« Reply #9 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 Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: I need help with C(++)
« Reply #10 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 Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: I need help with C(++)
« Reply #11 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