Salem 5,265 Posting Sage

It was already posted on the C++ forum
http://www.daniweb.com/techtalkforums/thread44924.html
Now, it's just a duplicate post.

Salem 5,265 Posting Sage

It isn't even valid C, never mind C++.

I don't know which crusty old fossil compiler you're using (but I guess it's Turbo C), but you seriously need to upgrade to something in this century.

Here are a bunch of edits just to make the thing compile using a C compiler.
Watch out for all the !! comments.

#include<stdio.h>
/*!!#include<conio.h> non-standard header */
/*!!#include<io.h> non-standard header */
/*!!#include<fcntl.h> non-standard header */
#include<string.h>
#include<stdlib.h>
/*!!#include<alloc.h> non-standard header */
/*!!#include<dos.h> non-standard header */

/*!! stubs for non-portable functions */
/*!! write these using portable code */
void clrscr ( void ) {
}
int getch ( void ) {
  return 0;
}

/*!! why so few parameters, and why so many globals? */
int display();
long int getsize(char[]);
void editb();
void edits();
void bkst();
void stst();
void issue();
void viewret();
void retbook();
void bklt();
void stlt();

/*!! added */
/*!! which crusty old DOS header did you get this from? */
struct date {
  int da_year;
  int da_mon;
  int da_day;
};
/*!! end add */

typedef struct date DATE;

DATE dt,is,rt;
int dtest;

typedef struct
{
  char sno[6];
  char name[20];
  float fine;
  char dept[3];
  int token;
}student;

typedef struct
{
  char bno[5];
  char bname[50];
  char aname[20];
  DATE iss;
  DATE ret;
  char status[1];
  char isto[6];
  int cost;
  char descpt[200];
}book;

int id,im,iy,rd,rm,ry;

void assign(book *b)
{
  b->iss.da_year=iy;
  b->iss.da_mon=im;
  b->iss.da_day=id;
  b->ret.da_day=rd;
  b->ret.da_mon=rm;
  b->ret.da_year=ry;
  return;
}

/*!! bunch of functions which are NOT implemented */
/*!! we can't review code if …
Salem 5,265 Posting Sage

Well post your latest effort, and then perhaps we can help you solve the problem.

Are you using C-Style char arrays for your strings, or std::string strings from C++ ?

Salem 5,265 Posting Sage

What are you after - vacuous post of the week award?

How about including some useful information like
- which OS
- which compiler
- which database(s)
- which API(s)

Not to mention....
- what have you tried already to solve the problem?

Salem 5,265 Posting Sage

> then go ahead and do whatever turns you on, but later on you will probably regret it.
I agree, it's just another variation on people learning with say TurboC.

Learn the language first, then learn an implementation.

Salem 5,265 Posting Sage

> Thats sorting the array isnt it?
No, just a hugely expensive way of printing them out in order - just like you asked.

Now put all that together and post your latest attempt at solving this problem.

Salem 5,265 Posting Sage

Gee, normally noobs ask for help to finish their homework.
Now they're too helpless to even think of a name for it, nevermind do it :rolleyes:

Salem 5,265 Posting Sage

Well this is practically doing it all for you anyway, so what the hell.

Once you've done my previous suggestion, then do
for ( i = max ; i >= min ; i-- ) for ( j = 0 ; j < n ; j++ ) if ( a[j]==i )....

Salem 5,265 Posting Sage

Here's a hint:
Write a loop which determines the min and max values stored in the array.

Salem 5,265 Posting Sage

> )I'm hoping to learn c++ what's a good book to teach me?
First post in the forum is labelled "C++ Books"
http://www.daniweb.com/techtalkforums/thread10232.html

> What's good program to use (im looking at making game & programs)?
Most people go with dev-c++
http://www.bloodshed.net/dev/devcpp.html

Salem 5,265 Posting Sage

> void main(void)
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044841143&id=1043284376

> gets(text);
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1049157810&id=1043284351

Here's a little program to show the same thing using arrays and pointers

#include <stdio.h>
int main ( ) {
  char test[] = "this is a test\n";
  char *p;
  int i;
  for ( i = 0 ; test[i] != '\0'; i++ ) putchar(test[i]);
  for ( p = test ; *p != '\0'; p++ ) putchar(*p);
  return 0;
}
Salem 5,265 Posting Sage

"but I’m having issues installing."
You're expecting to catch the right fish with that bait?

Salem 5,265 Posting Sage

Read beej very carefully
http://beej.us/guide/bgnet/output/htmlsingle/bgnet.html
It has an example of how to use select.

Some points to note
> send(m_socket, strSend.c_str(), sz, 0);
Both send() and recv() can fragment the message. There is no guarantee that if you ask to send 20 bytes that it will happen in a single call. Even if the send() is a single call, the recv() can still be fragmented.
Making the whole thing non-blocking just makes this much more likely.

> So you think giving my program a 1 ms delay will correct this issue indefinitely?
Not at all.
Some points to ponder.
1. The sleep duration is a MINIMUM value only, so sleeping for 1 second and returning an hour later would be in spec.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/sleep.asp
"Suspends the execution of the current thread for at least the specified interval."

2. The OS time base is usually 10 or 20mS. Very short sleep periods get rounded up to the base OS scheduling interval.
http://www.geisswerks.com/ryan/FAQS/timing.html
Next OS upgrade, you could be looking for a different answer.

3. The actual amount you sleep will vary according to system load at the time. I mean, if your timer becomes very finely balanced, then all sorts of wierdness could result like
- my program runs fine during the day and crashes in the evening (sensitive to network load)
- my program is OK in debug, but crashes …

Dave Sinkula commented: I always forget about rep on this site. +4
Salem 5,265 Posting Sage

Help with what?
Are you expecting people to just take your code and debug it for you and hand it back to you? That's not likely to happen.

I mean, if you have a specific question like
- why doesn't this line compile, or
- why does it crash with some type of input, or
- I don't understand how to...
Then that's fairly easy to answer.

You seem to have the bulk of the code there, and on superficial inspection it seems to be doing the right thing.

You could clean up the warnings a bit I suppose

$ gcc -W -Wall -ansi -pedantic -O2 bar.c
bar.c:42: warning: ISO C does not allow extra ‘;’ outside of a function
bar.c:58: warning: ISO C does not allow extra ‘;’ outside of a function
bar.c:61:1: warning: "/*" within comment
bar.c:75: warning: ISO C does not allow extra ‘;’ outside of a function
bar.c:87: warning: ISO C does not allow extra ‘;’ outside of a function
bar.c:148: warning: ISO C does not allow extra ‘;’ outside of a function
bar.c:212: warning: ISO C does not allow extra ‘;’ outside of a function
bar.c:260:17: warning: "/*" within comment
bar.c:272: warning: ISO C does not allow extra ‘;’ outside of a function
Salem 5,265 Posting Sage

So what's the question?

How to read the file?

One thing to work on is some kind of process table, and 3 functions (doFCFS, doRoundRobin etc) which work on that process table.

Salem 5,265 Posting Sage
Salem 5,265 Posting Sage

> and its due to 15 - 4 - 2006
Seems that's not the only thing which has been missed....

Salem 5,265 Posting Sage
Salem 5,265 Posting Sage

It's a pity you didn't format your C code while you were at it :rolleyes:

Where's all the indentation to denote block structure, and blank lines to separate one function from another?

Salem 5,265 Posting Sage

> what i want to do is create a time delay of 100 micro seconds for every 18 charecters sent
Do this calculation:
Your port is set to 19200 baud, and framing is set to 8-N-1.
So each character has a start bit, 8 data bits, a stop bit and no parity (10 in all).
That gives you a line speed of 1920 characters per second.
At that speed, sending each character takes a little over 500 microseconds.

So a 100uS delay is basically the time taken to send a couple of bits of data. There's no way to guarantee that the OS won't insert much bigger delays due to scheduling of other tasks, and it certainly isn't enough time for the receiver to do much about it either.

Microscopic programmed delays are usually the wrong approach. There is simply too much variability in the average OS to make it anything like reliable.
You should be looking to use some other method of flow control, say xon-xoff.

Perhaps you need to explain what a delay of 100uS is supposed to get you, then perhaps we could suggest a better answer.

Salem 5,265 Posting Sage

Well you could state your OS and compiler.

There is no standard way to achieve sub-second delays.

Salem 5,265 Posting Sage

> for(int i=2; i<=((int)sqrt((double)n)); i++)
And it would be so much quicker if you didn't call sqrt() on every iteration of the loop!
n is constant (in this function), so it's root is constant also. Calculate it once and store in another variable to compare against.

Also, since 2 is prime, that's an easy case to get rid of, and you can start at 3.

Also, if you start at 3, then you can do i+=2 to only check all the odd numbers from there on.

hollystyles commented: good feedback +2
Salem 5,265 Posting Sage

> The first three people to complete this program will be entered into the annual contest for Top Coder!
Seems the best thing to do is to goto the site and verify this.

No wait, this is just another "ruse" to get someone else to do your homework isn't it?

Salem 5,265 Posting Sage

> for(int j=2;j<i/2;j++) //since a no. cannot be divisible by a
> { //no. greater than its half.
It's square root of actually, but hey what the heck.

At least it's not the only mistake in your code.

Salem 5,265 Posting Sage
Salem 5,265 Posting Sage

> if (*fp == NULL)
You could start here - never mind the rest of it.

Say
if (fp == NULL)

Man, didn't your compiler complain about that line?


> while( fgets(questions[x], sizeof(questions), fp) != NULL)
If you lie about the buffer size, then it's no better than gets()

Besides, if you want to read up to 25 lines, its

for ( x = 0 ;
      x < 25 && fgets(questions[x], sizeof(questions[x]), fp) != NULL )
      x++ ) {
  char *p = strchr( questions[x], '\n' );
  if ( p ) *p = '\0';  /* blow away a newline - if you want to that is */
}
Salem 5,265 Posting Sage

http://www.daniweb.com/techtalkforums/announcement8-3.html
If you really want people to pay attention to your code.

Salem 5,265 Posting Sage

Apparently, I wasted my time :mad:
http://www.daniweb.com/techtalkforums/thread43435.html

Not only have you dropped the initialisation of univ in main(), but you've gone back to using fscanf to read the file.
Not only that, you're now using global variables like "temp", which get allocated on every iteration of the loop and never freed.
Don't forget the not using a temporary variable when calling realloc, nor the unnecessary casting of malloc and realloc.

Y'know what, if you're just going to ignore people, just say so - ok?

Salem 5,265 Posting Sage

Start with a smaller program, and compile more often would be my advice.
Writing 5 lines at a time, pressing "compile" and fixing the problems which you find is the way to learn how to do this yourself. If you get stuck, then we only have a few things to fix and you hopefully learn something new.
Writing 100+ lines and dumping it on a message board and hoping someone else will fix it all for you isn't.

There really are too many syntax errors which should have been picked out long ago.
For example, main has
- one extra }
- no parameters passed to read(), despite read() for example trying to access argv
- calling fclose() on a file handle which wasn't opened in main
- a whole bunch of unused variables.

> while (!feof(luku_tied));
Better watch that ;, this will either do nothing or loop forever.

> if(uusi = (char* ) malloc(sizeof(struct)) == (char*)NULL)
1. Don't cast the return result of malloc in C
2. Read up on operator predecence rules. As written, uusi gets the boolean result of ==
Your other malloc call got that bit right.

Oh, and you might want to work on the indentation a bit as well, some of it is really misleading as to what is going on.

Salem 5,265 Posting Sage
Salem 5,265 Posting Sage
Salem 5,265 Posting Sage

I think the whole point of this exercise is for you to write a function along the lines of

void convertToBase( int number, int base, char *result );

Assuming you know how to do this for base 10, through application of the / and % operators, the rest should follow naturally.

Salem 5,265 Posting Sage

I didn't pay any attention to his program, it wasn't formatted with code tags.

All I did was illustrate the relationship between for loops and while loops, and how ridiculously easy it is to convert one into the other.

Salem 5,265 Posting Sage

> I make this program but with while loop then i was told to make it using for loop, which i tried but failed.

So turn

i = 0;
while ( i < 500 ) {
  // do stuff
  i++;
}

To

for ( i = 0 ; i < 500 ; i++ ) {
  // do stuff
}
Salem 5,265 Posting Sage

> version creates 'x' as a const pointer object.
When you've finished reading this
http://c-faq.com/aryptr/constptr.html

Finish reading this
http://c-faq.com/aryptr/index.html

Salem 5,265 Posting Sage

http://unixhelp.ed.ac.uk/CGI/man-cgi?termios
Clear the ECHO bit.

You might also consider
http://www.hmug.org/man/3/getpass.php
but I think that's a deprecated function.

Salem 5,265 Posting Sage

> My assignment 's deadlin is by tomorrow. the assignment is avl tree.
So you've been sitting on the problem for a week, and now you expect someone to save your ass in 24 hours?

> I really need the source codes urgently as without it, the whole program would not be evaluated by my tutors
I fail to see the point of letting the tutors evaluate someone else's work.
I just hope they give you a good quizzing about how it works, then they'll surely see that it isn't your own work.

> I am going to fail definitely
#include <very_small_violin.h>

Salem 5,265 Posting Sage

> stdafx.obj : error LNK2005: "void __cdecl DebugTest(char *)" (?DebugTest@@YAXPAD@Z) already defined in Internet.obj
This is what you normally get when you #include one .cpp file inside another, or put actual code (not declarations) inside header files.

Salem 5,265 Posting Sage
  1. Please use [code][/code] tags when posting code - see the readme first threads.

  2. void main()
    main returns an int.

  3. for(int polygon = 0;polygon < 3;polygon++)
    This doesn't compile - polygon isn't declared. Try to post what you actually compiled.
    There's no way for us to guess whether what you missed out and is causing a problem, or what you missed out just to make a shorter post.

  4. cout << x0[vertices] << " "; <--------problem
    Well all of your arrays go out of scope at the } which follows each if statement.

Maybe something like

float x0[][10] = {
  {20, 40, 90, 120, 70},
  {200, 200, 240, 300, 300, 250},
  {400, 400, 420, 450, 450, 480, 500, 475, 445, 445}
};
cout << x0[polygon][vertices];
Salem 5,265 Posting Sage

1. what does "buff" stands for , after all it has no Value.
buff is short for buffer.

2.BUFSIZ shoulde be defined or resized each reading from source file?
No, it is a fixed constant declared in stdio.h (for an ANSI-C compiler anyway).
You are using an ANSI-C compiler and not some ancient fossil like TurboC.

3.how can get rid of the double menu display?
Don't use getchar(), scanf(), getc() to read a single character.
Use fgets() to read a line from stdin, then read what you need from the buffer.
It's just like my use of fgets() in the previous post.

4.is there a way to write to target file so it wont skip chars?
I've no idea - post the problem code.

Salem 5,265 Posting Sage

> void main
main returns int

> char user_choice;
> while (user_choice!='z')
This variable is uninitialised at the point you first use it.

> university univ;
This isn't initialised either.
Which is very important when you get to make_file(), since you do
- dereference an uninitialised pointer
- try and realloc an uninitialised pointer

> if (un->ptr==NULL) un->ptr=(student*)realloc(un->ptr,(count+1)*sizeof(student));
The test serves no purpose, since at best it only extends the array once.

In main(), you need this to start off with a NULL pointer university univ = { 0 }; And this function needs to be something like

int make_file(FILE *in,university *un) {
  char buff[BUFSIZ];
  int  count=0;

  while ( fgets( buff, sizeof buff, in ) != NULL ) {
    student s;
    if ( sscanf( buff, "%s %ld %f %ld",
                 s.name, &s.id, &s.avg, &s.hw_submit ) == 4 ) {
      /* success decode, extend array and copy the info */
      void *temp = realloc( un->ptr, (count+1)*sizeof(student) );
      if ( temp != NULL ) {
        un->ptr = temp;          /* update array */
        un->ptr[count] = s;      /* copy data */
        count++;                 /* one more stored */
      } else {
        /* no more room, return with what we have */
        return count;
      }
    } else {
      /* that line didn't make sense, report it */
      fprintf( stderr, "Bad line %s", buff );
    }
  }
  return count;
}
Salem 5,265 Posting Sage

> Been trying ot figure out this for couple days but keep getting syntax errors for some reason.
Well post what you tried, then perhaps we can lead you to the answer.

Salem 5,265 Posting Sage

> properties->settings->advanced->options
So I'm guessing you're not using DOS then.

So why on earth are you trying to use a DOS compiler then?
Do you have a fondness for museums and a longing to be a driver of a steam engine?

Salem 5,265 Posting Sage

I'm wondering how many boards I'm going to see this spam on....

In fact, it appears in so many places (thanks google) that I regard it as marketing spam to make people click on the link rather than any serious attempt to ask a meaningful question.

Salem 5,265 Posting Sage

> Is the data OK before you sort it?
You didn't answer this question.

Post your latest code.

Salem 5,265 Posting Sage

> if ((year % 400)==0)
How is this even compiling, when all you have is a parameter called 'y' ?

> return 0;
How is this even compiling, when you declared it as returning void?

Salem 5,265 Posting Sage

Can you paste a log of your run, showing the prompts being printed and the values you typed in?

Salem 5,265 Posting Sage

> I'm not sure why the last three are getting mixed up.
Is the data OK before you sort it?

> if(min!=i)
Consider using a lot more { } in this function, because it seems to me that not everything is being run at the correct time. Yes, braces can be optional in some circumstances, but if you take the approach of always using them, you minimise your surprises later on.

Salem 5,265 Posting Sage

Of course you do.

Biut it makes no sense to pass the whole array to the function each time, when you're only reading into the array one element at a time.

Salem 5,265 Posting Sage

Try

level[i] = cal_Fitness_Level(age[i], sex[i], wTime[i]);