Narue 5,707 Bad Cop Team Colleague

Whether it works or not really depends on what you want it to do. What were you expecting?

Narue 5,707 Bad Cop Team Colleague

Yes, do research on kbhit. You should find out quickly whether your implementation supports it or not, and how to write your own if it doesn't.

Narue 5,707 Bad Cop Team Colleague

It's possible, but not practical since on the surface an interactive terminal session has identical functionality and is considerably faster because an interactive file constantly reads from and writes to a slow physical device.

Narue 5,707 Bad Cop Team Colleague

Is there any reason this has to be an interactive file rather than an interactive terminal session that writes to a file periodically?

Narue 5,707 Bad Cop Team Colleague

he ha ha ha ha ha ah aha aha aha aha haa ha ha ha ha ha ha aha ha aha ahja aha ha ah 11 :rolleyes: :p :lol: :mad: :( :mrgreen:

Please refrain from bumping two year old threads.

Narue 5,707 Bad Cop Team Colleague

>Sadly though, it is still taught
Indeed.

>Is the speed of the algo is in question
Yes, always. Bubble sort (as taught) does a lot of unnecessary work. It can be improved slightly, but that's almost always left as an exercise.

>understandbly when the dataset moves into the range of millions
Thousands. Bubble sort's growth is such that even sorting 10,000 records could be noticeably slow (less than that if the records are non-trivial). If you have millions of records then a suitable data structure or an nlogn sort would be more appropriate. The quadratic sorts are only useful for small data sets, or as components in implementing a more sophisticated algorithm.

>how can you reorder numbers that are user input using selection sort in a 1 dimension array and output each element
You've asked this same question four times if I recall correctly. I also recall answering it fully, with code examples. What's the problem?

Narue 5,707 Bad Cop Team Colleague
Narue 5,707 Bad Cop Team Colleague

>char FirstCharacter[1]; // Character string to hold to value found by
This must be an empty string or it cannot be a string at all. C-style strings require a null character at the end as a terminator. You would really be better off just using a single char.

>if (FirstCharacter[0] == "*")
You don't compare strings with the == operator. Include <cstring> and do this:

if ( strcmp ( FirstCharacter, "*" ) == 0 )

Of course, this assumes that you fix the first problem and FirstCharacter actually is a null terminated string. You can do it like this:

char FirstCharacter[2] = {0};

// ...

FirstCharacter[0] = OutputFile.peek();

if ( strcmp ( FirstCharacter, "*" ) == 0 )
  cout<<"Dummy record"<<endl;

The "= {0}" part guarantees that a null character is placed in FirstCharacter[1]. But, as I said before, it's better to just use a character to begin with:

char FirstCharacter;

// ...

FirstCharacter = OutputFile.peek();

if ( FirstCharacter == '*' )
  cout<<"Dummy record"<<endl;

Or even better:

// ...

if ( OutputFile.peek() == '*' )
  cout<<"Dummy record"<<endl;

That avoids the need to even use a temporary variable.

Narue 5,707 Bad Cop Team Colleague

Is there any reason you're using inheritance when a few non-member functions for String would work just as well with less work?

Narue 5,707 Bad Cop Team Colleague

>i have just got my self a Borland 4.5 from ebay
You got ripped off, no matter what you paid for it. Borland 4.5 is too old to be useful these days, and Borland gives away their 5.5 compiler for free.

>like to be able to put a shoping cart on my website
C and C++ really aren't the best options for this. Look into Perl or PHP if you want to write it yourself.

Narue 5,707 Bad Cop Team Colleague

You can only use a function if it's been declared first. That basically means that you tell the compiler "Hey you, compiler! This function exists. Let me use it now". When you write the body of a function, you're both declaring and defining it. If you don't want to define all of your functions before the functions that call them, use a prototype:

#include <iostream>

void DemonstrationFunction();

int main()
{
  using namespace std;

  cout << "In main\n";
  DemonstrationFunction();
  cout << "I'm back in main\n";
  return 0;
}

void DemonstrationFunction()
{
  std::cout << "In Demonstration Function\n";
}

And in the future, use code tags when posting code or I'll go Narue on your butt. ;)

Narue 5,707 Bad Cop Team Colleague

I've added code tags for you this time. Please review this page before posting again, and be sure to use code tags when posting code.

Narue 5,707 Bad Cop Team Colleague

>I figured that it probably was due to the system running out of memory to allocate
No, not directly at least. You can get a segmentation fault because malloc failed and returned a null pointer, but the system running out of memory (while incredibly unlikely) wouldn't result in a seg fault. A seg fault is caused by accessing memory outside of your address space. You're given a chunk of memory from address m to address n. If you try to access address p, you'll get a seg fault because it's not within m to n.

>However, if I free the memory like you did before the loop is exited, my program should never crash
No. Memory leaks will only slow your system down and possibly grind it to a halt. If you program crashes, it's because you did something wrong. Though releasing memory you allocate is always a good idea.

Narue 5,707 Bad Cop Team Colleague

>so if I had done some other things later on in my program
Once freed, the memory is no longer yours. That means that in theory, another process could allocate the memory immediately after you're done freeing it and overwrite the values you had stored there before printf is called in your program. So your print isn't guaranteed to work, and on top of that, you've invoked undefined behavior because you don't own the memory you're accessing.

Narue 5,707 Bad Cop Team Colleague

>I'm not sure if I should click on compile or build.
A missing brace is a syntax error. You can get that during the compile stage, so either Compile or Build will work because Build compiles first, then links.

Narue 5,707 Bad Cop Team Colleague

>rectangle box( pt1, pt2, pt3, pt4);
>box.perimeter( pt1, pt2, pt3, pt4)
Tell me, is this redundant? You pass the same parameters to perimeter as you do to the constructor. Looking at nothing but these two lines, I would assume that p1, p2, p3, and p4 are all saved in box so that they can be accessed later by member functions of rectangle. So you shouldn't need to pass them again to perimeter because it's a member function. This will work just as well:

int rectangle::perimeter() 
{  
  return pt1.getDistance(pt2); 
}

Though you should get a warning because getDistance returns a double and perimeter returns an int. That's a narrowing conversion that could lose data.

Narue 5,707 Bad Cop Team Colleague

When you change the definition of a member function, you need to change the declaration to match it.

Narue 5,707 Bad Cop Team Colleague

>any ideas?
>void perimeter();
You can't print a void type.

Narue 5,707 Bad Cop Team Colleague

Hello thanks for the assitance... I still can;t get this line of code to work... //cout << "perimeter is : " << box.perimeter() << endl;

You haven't defined the perimeter member function of rectangle, so you should be getting a linker error. The only advice for that I can offer is to write it so that you're actually doing something instead of calling a nonexistent function. ;)

Narue 5,707 Bad Cop Team Colleague

Member functions can be simply called in one of two ways. If the function is declared as static then you call it using the scope resolution operator and the name of the class:

class C {
public:
  static void foo();
};

// ...
C::foo();

If the member function is not declared as static then you must call it using the member access operator and an object of the class:

class C {
public:
  void foo();
};

// ...
C obj;
obj.foo();

In your case, the line in question should be changed to:

cout << "the distance is" << pt1.getDistance(pt2);

Alternatively you could say:

cout << "the distance is" << pt2.getDistance(pt1);

Because this shouldn't effect the distance between the two points. The key is understanding that at least one of the objects passed to perimeter should be used as the base object that you call getDistance on and the other object should be passed as the argument to getDistance.

Narue 5,707 Bad Cop Team Colleague

Unfortunately, graphics.h is a rather old library, so tutorials covering it are sparse. Your best bet would be to search google for tutorials that mention graphics.h. Turbo C++ might also offer example code in the help files.

Narue 5,707 Bad Cop Team Colleague

>The <iostream.h> library has been deprecated for many years now
If iostream.h were deprecated then all conforming compilers would be required to support it, which isn't the case. iostream.h was never a standard header, so it can't be deprecated. A correct statement would be "The <iostream.h> library has been nonstandard for many years now". The difference is becoming more important as newer compilers refuse to accept code that uses iostream.h.

Narue 5,707 Bad Cop Team Colleague

>iostream or iostream.h???
iostream. iostream.h isn't a valid C++ header anymore.

>it's like I'm not using a C++ compiler
Maybe you aren't. The file extension usually tells your compiler what language specification to use. If the file is a .c file then you're probably compiling as C, if it's a .cpp file then you're compiling as C++. Most compilers will also let you use a parameter to determine what language to compile as.

Narue 5,707 Bad Cop Team Colleague

>first of all that seemed a little uncalled for
You refuse to use anything but an ancient compiler because you "like it", and you want to write DOS based graphical programs (that rely on real-mode memory access) on the Windows platform (that forces protected-mode operation). It sure seemed called for to me.

>post something useful other than the "grow your self some brains" suggestion
If you lose the tunnel vision and read the entire post, you'll find that I answered your question completely with the given restrictions. I ruled out the option of upgrading to a newer compiler, and suggested that you install DOS. If you're willing to take any old graphics library instead of something approximately the same as graphics.h then you can search google and maybe find something that supports your compiler, but in the end it's wiser to upgrade and exercise the myriad of options that are opened up.

>by the way take some of your own advice!!!
I'm not the one trying to write DOS programs in Windows.

p.s. If your choice of compiler and graphical code is due to your class, then please accept my appologies and transfer the insults to your teacher, who clearly is a moron stuck in the stone age.

Narue 5,707 Bad Cop Team Colleague

You haven't left yourself any options, so you're SOL unless you grow a brain and join the 21st century. Sure, you could downgrade to an OS that supports DOS programs, but that's really a step in the wrong direction.

Narue 5,707 Bad Cop Team Colleague

>But my queestion was little bit unclear.
Your question was clear enough, and the answer is the same. If it turns out that your question is actually how to read a text file so that you can easily store the results of each quiz in a list, that's a different story.

Narue 5,707 Bad Cop Team Colleague

To find the largest value in a list, save the first value and walk through the list, saving the largest:

T largest = list[0];
for (int i = 1; i < list.size(); i++) {
  if (list[i] > largest)
    largest = list[i];
}
cout<<"The largest value is "<< largest <<endl;

To find the smallest, just reverse the test:

T smallest = list[0];
for (int i = 1; i < list.size(); i++) {
  if (list[i] < smallest)
    smallest = list[i];
}
cout<<"The smallest value is "<< smallest <<endl;

To find the average of all values in a list, sum them and then divide by how many items there were:

T sum = 0;
for (int i = 0; i < list.size(); i++)
  sum += list[i];
cout<<"The average is "<< sum / list.size() <<endl;
Narue 5,707 Bad Cop Team Colleague

>Do you need to put the ; after the last }?
No.

>Do you need to put a return statement before the last }?
In C, yes. In pre-standard C++, yes. In standard C++, no, 0 will be returned automagically. Whether you choose to or not for standard C++ is a matter of style and consistency. My style is to not explicitly return a value unless I return for failure elsewhere:

#include <iostream>

using namespace std;

int main()
{
  cout<<"Hello, world!"<<endl;
  // No return
}
#include <cstdlib> // For EXIT_SUCCESS and EXIT_FAILURE
#include <iostream>

using namespace std;

int main()
{
  if (some failure condition) {
    cerr<<"Error"<<endl;
    return EXIT_FAILURE;
  }

  cout<<"Hello, world!"<<endl;

  return EXIT_SUCCESS; // Return success for symmetry, 0 works too
}

>what number do you put in the (), i.e. return (0), return (1), etc
return is not a function, so you don't need to use parentheses if you don't want to. Any integer is a valid return value, but the only portable values are 0 and two macros defined in cstdlib, EXIT_FAILURE and EXIT_SUCCESS.

>What does the number mean?
It's an exit code. If your program returns 0 then that tells any calling process that it terminated successfully. Any other value is treated as unsuccessful termination and the actual value gives more detail. For example, 1 could mean that a file failed to open, 2 could mean that invalid user input was read, etc...

>Which one is the correct …

Narue 5,707 Bad Cop Team Colleague

>Why?
Because bubble sort is one of the worst sorting algorithms invented, promotes bad habits, and any of the other quadratic sorts are equally good practice. I personally believe that bubble sort should only be used as an example of how not to sort data.

Narue 5,707 Bad Cop Team Colleague
char a[256]; // Static allocation
char *p = new char[256]; // Dynamic allocation

>and why is supposed to be so popular in use?
You aren't restricted to a "one size fits all" strategy, and you can generally grab more memory from the dynamic pool than from the "stack".

Narue 5,707 Bad Cop Team Colleague

>You need a bubble sort algo.
Please don't suggest bubble sort if other options are available.

Narue 5,707 Bad Cop Team Colleague
for(c=0;c<=100;c++)
{
  if(!(inFile>>fname))
  {
    break;
  }
  inFile>>prin[c]>>rate[c]>>pay[c];
}

That looks suspicious. If the file only consists of rows with three numbers then trying to read something into fname will eat the first and every fourth number. Try this instead:

for (c = 0; c < 100; c++) {
  if (!(infile>>prin[c]>>rate[c]>>pay[c]))
    break;
}

I used c < 100 instead of c <= 100 because that tends to be a common off-by-one error. If your arrays are declared as "double array[100];" then you'll be accessing the 100th index of prin, rate, and pay, all of which are out of bounds.

Narue 5,707 Bad Cop Team Colleague

I don't know how to help you because I don't know the specifics of your game. Sorry.

Narue 5,707 Bad Cop Team Colleague

It depends heavily on how your game works. For the most part, all you need to do is save relevant information about the player such as stats, items, and location. Then you can rebuild the current game state upon load.

Narue 5,707 Bad Cop Team Colleague

>In no way was that meant as a tutorial.
Obviously.

>how did you come to the conclusion that the above post was a tutorial?
Because your thread was in the C/C++ Tutorials forum before a mod was kind enough to relocate it to the proper place.

Narue 5,707 Bad Cop Team Colleague

OMG, man you're so full of it.

This post adds nothing to the thread. If all you can do is throw insults then do it through PM.

Narue 5,707 Bad Cop Team Colleague

>you even don't know the down side of "goto" stmt in C++?
I'm familiar with both sides of the argument, and I'm in a unique position to argue that goto does have it's uses even though most of the time it should be avoided. An unthinking ban is stupid.

>well then i should not waste my time replying to your posts.
Now you're just bitter that I've been questioning your knowledge.

Narue 5,707 Bad Cop Team Colleague

>it's with the utmost sincerity and should be taken in that context
In my experience, people are gullible and incapable of comprehending subtlety. Thus, the majority of readers will take such statements at face value, much like the "never use goto" deal.

So Narue, I think you should mabe reconsider your position as self proclaimed officer of etiquette, and as I've noticed with a lot of your posts not be so condesending and critical of what others write.

That's your opinion and you're entitled to it. However, be aware that it's highly unlikely that I'll listen to you. You'll notice that the general quality of threads on this forum has improved drastically since I started visiting regularly. This could be a coincidence, but unless you can prove it (notice a trend?) I'll have to assume it's my policing of the forum.

Narue 5,707 Bad Cop Team Colleague

>I have some respect for you, don't make me lose it.
I don't care what you think about me, so I'll call it like I see it.

>It's been proven, I don't need to prove it again.
My problem isn't with your choice of favorite compiler. My problem is that you didn't back up your claims concerning the vague measure of "best". I know the facts, but not everyone does. You also can't expect everyone to google all night to determine whether you're full of hot air or not. Most people won't bother. So if you make a blanket statement, prove it then and there.

>Goggle out the benchmark scores for all C++ compilers.
Benchmarks are virtually useless, but if you believe them then more power to you.

Narue 5,707 Bad Cop Team Colleague

>void main()
This is not, and never has been, correct C++. The C++ specification requires main to return an int.

>Never ever ever use goto stmts.
Normally I'd call you a moronic conformist, but you've shown yourself to be fairly knowledgeable. So I'll give you a chance to argue your case and then consider taking back the "never ever ever" part before I become the evil voice of reason.

Narue 5,707 Bad Cop Team Colleague

>The best
That's nice. Prove it. And anytime you say that something is the best or the worst, be ready to prove it.

Narue 5,707 Bad Cop Team Colleague

[dripping sarcasm]
Brilliant tutorial! I wonder how I've survived so many years as a programmer without reading "migthyjohn's frumph" tutorial.
[/dripping sarcasm]

>and i have yet to make any sense out of it
Since you posted in the wrong forum, I get the impression that you don't read for comprehension very well. That's probably your problem. Either that or raving idiocy, but I prefer to assume the best in people before they invariably prove my assumption wrong.

Narue 5,707 Bad Cop Team Colleague

Using global variables makes your code harder to follow. But I immediately found this error:

for (int x = 1 ; x <= number_students; x++)

Why are you starting with 1 and ending at number_students when the array only goes from 0 to number_students - 1?

This is suspicious as well:

for (int x = 10 ; x < number_students; x++)

Why are you starting with 10?

Narue 5,707 Bad Cop Team Colleague

What are the params for find() and erase()?

Some stuff with a couple of types. Google for dinkumware and do your own research.

Narue 5,707 Bad Cop Team Colleague

>I am eagerly waiting for Narue's and Christian's solution of Alfarata's problem!
The design is flawed to begin with. There's no good solution. If you ask the user for a filename, it's only reasonable to require the full path. If you require the full path then what possible use could you have for discarding the extension and tacking on .csv? What if it's not a CSV formatted file?

Narue 5,707 Bad Cop Team Colleague

>this do I put it in the lab.cc source code where the main is or in the list.cc
>where the while loop starts, where the problem is?
Which of those declares the Letters array? :rolleyes:

>and where do I initialize the INITIAL_VALUE?
Presumably it's already initialized in list.h. If not, that's your problem.

Narue 5,707 Bad Cop Team Colleague

The error is pointing to this line:

while (thing > List[where] && where < howmany) where++;

For starters, List[where] has an indeterminate value regardless of the value of where because you never initialized Letters. That alone probably won't cause a segmentation fault, so let's see what INITIAL_VALUE is defined as. Most likely that's your problem. I had defined INITIAL_VALUE to be 0. But before you do anything else, initialize that array:

Item Letters [MAX_ITEMS];

for (int i = 0; i < MAX_ITEMS; i++)
  letters[i] = Item();

>List=0xbffffb78 "(some numbers and signs)
The array is uninitialized, that's understandable.

>howmany=0xbffffb74
Your debugger is probably viewing a reference variable as a pointer. The address looks consistent enough to be valid, so we'll assume that it's correct until we figure out the seg fault.

Narue 5,707 Bad Cop Team Colleague

>can any one give me C program for ROUND ROBIN ALGORITHM
No, do your own homework you lazy bum.

>please needed urgently
You should also learn not to procrastinate until the last minute.

Narue 5,707 Bad Cop Team Colleague

>This assumes that the user has some kind of brain.
You mean the hypothetical imaginary user that university professors use to avoid teaching students the reality of error checking? I've never met such a user in real life.

>you have to detect the period character in the entry string with strchr()
Filenames with multiple periods are common. A perfectly valid filename would be destroyed by this method, resulting in an obscure error.

Narue 5,707 Bad Cop Team Colleague

Give an example of the output you're getting, preferrably at each call to insert. An initial test shows everything to work just fine, but I had to guess on the contents of list.h.