Narue 5,707 Bad Cop Team Colleague

>But what about graphics and using Assembly?
What about graphics and using assembly? RTFM first if you have an compiler specific question.

Narue 5,707 Bad Cop Team Colleague

>Uh, yeah. Parse error between monitor and chair.
Wetware compilers are so unreliable. Sounds like you need an upgrade. ;)

Narue 5,707 Bad Cop Team Colleague

>This is not likely ever to be true.
You mean false? I'd say it's highly likely to be true since EOF must be a negative quantity and characters gathered from a narrow stream must fit within an unsigned char. Fortunately, the other half of the condition will cause the loop to terminate properly.

Narue 5,707 Bad Cop Team Colleague

>Can you please help me on calling interrupts in that standard?
Didn't you already ask that question, or was it someone else?

>By I think, one of the h files can be used for this purpose.
windows.h, and you have to implement the functionality yourself:

#include <windows.h> 

void gotoxy ( short x, short y )
{
  COORD coord = {x, y};
  SetConsoleCursorPosition ( GetStdHandle ( STD_OUTPUT_HANDLE ), coord );
}
Narue 5,707 Bad Cop Team Colleague

>What a kind of comparision do we do?
It does a lexical comparison using the integral value of a character:

int i = 0, j = 0;

while ( a[i] == b[j] ) {
  if ( a[i] == '\0' )
    return 0;
  ++i;
  ++j;
}

if ( a[i] < b[j] )
  return -1;
else
  return +1;

For example, in ASCII, a < z < A < Z, so test would be "less than" TEST using strcmp.

Narue 5,707 Bad Cop Team Colleague

>as for you Narue I think I know about book stores
I try to assume a complete lack of intelligence and common sense in the people I help. That way I'm never surprised when they don't understand simple concepts.

>I was wondering if there was a specific book that I could buy
Learning Python, Programming Python, and the Python Cookbook should give you a good start.

>because I have'nt seen any Python books at the BOOK STORES
Well, if you haven't seen them, how could you buy them? Generally, bookstores tend to avoid hiding their inventory for the "worthy" customers. It hurts business.

Narue 5,707 Bad Cop Team Colleague

One generally purchases books from a thing called "The Bookstore". There are even "bookstore"s online such as www.amazon.com.

Narue 5,707 Bad Cop Team Colleague

>Can you tell me how to do it now?
Sure, go to msdn.microsoft.com and read. Normally I would give you some code, but I find your "gimme gimme" tone annoying.

Narue 5,707 Bad Cop Team Colleague

Yes, but not with straight C++. You're looking at operating specific API calls.

Narue 5,707 Bad Cop Team Colleague

From www.dinkumware.com, go to Dinkum References, then choose C++ Library Reference. Click the little footprint image and have fun.

Narue 5,707 Bad Cop Team Colleague

It's a good thing you thanked us in advance, because you certainly wouldn't thank us after we told you that this isn't rent-a-coder-for-free.com.

Narue 5,707 Bad Cop Team Colleague

>Na, I don't know how you know all this stuff
Well, I'm in a somewhat unique position. I've actually implemented the standard libraries, whereas most people are only on the client end.

>where can I get access to the find out what's inside these "standard libraries"
The best reference you can get is the ISO C++ standard. It's available as a book from Wiley (ISO/IEC 14882:1998), or with the latest technical corrigendum from www.ansi.org. The most recent document is ISO/IEC 14882:2003. Of course, if you can't handle that (and most people can't), www.dinkumware.com has a good reference for the standard library.

Narue 5,707 Bad Cop Team Colleague

All of your streams are actually typedef's. istream is a typedef for basic_istream<char, type_traits<char> >, ostream is a typedef for basic_ostream<char, type_traits<char> >, ifstream is a typedef for basic_ifstream<char, type_traits<char> >, ofstream is a typedef for basic_ofstream<char, type_traits<char> >, and so on.

The idea is that streams don't care what type of character they use, so you can define a character type and set up the char_traits for it, maybe make your own stream buffer, then plug it in to the existing framework. Boom! You've saved yourself several hundred lines of reinventing the wheel.

Unless your book doubles as a reference, or happens to be exceptionally detailed, it won't say a thing about the template classes hidden by those typedef's.

Narue 5,707 Bad Cop Team Colleague

>To get the same effect do you have to have the <iostream.h> header or <fstream.h>?
ofstream is declared in <fstream> (or <fstream.h> if you use a pre-standard compiler).

>Also, did ios::out in parenthesis work as an argument?
Yes. The constructor for basic_ofstream takes two arguments: a path and an open mode. The open mode for basic_ofstream is defaulted to ios_base::out, which is equivalent to ios::out.

Narue 5,707 Bad Cop Team Colleague

>ofstream outClientFile("clients.dat", ios::out);
ofstream opens files using the ios::out mode by default, so you could do this:

ofstream outClientFile("clients.dat");

And the effect would be the same.

Narue 5,707 Bad Cop Team Colleague

>Okay, so a linked is just a form of "indexing"?
A linked list is just a way to structure data so that certain operations are more efficient than others. Insertion into an array is inefficient while indexing is fast, switching to a linked list reverses those roles. The more you work with data, the more you'll realize that the same data can be represented any number of ways. Some of them good, some of them bad. That's why we have so many data structures. And all a data structure does is hold data.

>How would you make an array work with a file or even a linked list for that matter?
when processing a file, you read a record and then do something with it. When working with an array, that something would be to add the record to the array. When working with a linked list, that something would be to add the record to the linked list.

Narue 5,707 Bad Cop Team Colleague
string array[50]; // 50 string objects

struct rec {
  char name[11];
  int age;
};

rec records[50]; // 50 rec objects

Everything is fairly consistent, even if it doesn't seem that way at first. You access an element of one of those arrays by taking the index and then using the member access operator:

strcpy ( records[5].name, array[5].substr ( 0, 10 ).c_str() );
cout<< records[5].name <<endl;
Narue 5,707 Bad Cop Team Colleague

>what's the big deal with having to sort a linked list?
?

>sort them according to what (i.e. date, time, numerically, priority, etc.)?
That's up to the application. If you have a linked list of employee records then you can sort by first name, last name, date of hire, salary bracket, (etc...) or even all of them in any order.

Narue 5,707 Bad Cop Team Colleague

>Isn't an linked list more powerful than an array
Which data structure is more powerful depends solely on how you intend to use it. That's why there are so many data structures. You can simulate any data structure with an array, but the benefits of those data structures are diminished when you do so. Different problems, different solutions, and all that jazz. :)

>because nodes are classes/structs that can hold member functions and not just member data?
You can have an array of structs/classes, so that's hardly a reason to favor linked lists.

>but are you telling me that linked lists are used for file process only?
No, I was giving you an example where arrays start to fail and linked lists start to look good. The linked list is a fundamental data structure that's used all across the board, not just in file processing. The limits of where you can use them are determined by your imagination alone.

Narue 5,707 Bad Cop Team Colleague

>HS students need more stimulation than hardcore programmers
Then perhaps you should teach a language better suited for students than C++. Java perhaps, or Python. Both have relatively easy to use graphics APIs available right out of the box. C++ is a hardcore language designed for hardcore programmers. ;)

Narue 5,707 Bad Cop Team Colleague

>i would like to use a window for user interface instead of dos console.
My first question is "why"? Introducing graphics should be done only after the students have a strong foundation in the core language and standard libraries. Otherwise, they'll find themselves battling C++ as well as whatever graphics API you choose to give them.

Then of course there's the argument that if you don't teach the core skills of programming first, then all your students will be able to do is make pretty windows that do nothing useful. And that's a best case situation. If they get discouraged and quit because they never won the "little" battles on the command line and gained confidence then you won't even see pretty windows that do nothing.

Personally, I think you're trying to do too much, too fast. Especially for a high school course. But if you really want to make learning harder (maybe to weed out anyone who doesn't have a passion for programming) then tell us your compiler and we'll be able to make better suggestions.

Try googling for Allegro or SDL for a generic solution.

Narue 5,707 Bad Cop Team Colleague

>was i right about this (below):
More or less. Linked lists can be more efficient than arrays, but the inverse can also be true. It depends on what you need. You use an array when you need fast random access, you know roughly how many items ther will be, and items will only be added to the end of the array. This is because arrays can access any item with an index very quickly. With linked lists, you would be forced to follow links until you found the right item. However, insertion into an array is potentially expensive because you need to shift every item that goes after the new item to make room. Resizing an array (if possible) is tedious and error prone, so it's best avoided when possible.

You use a linked list when you don't know how many items there will be, you may insert an item anywhere in the list, and random access is not a primary goal. Because linked lists are non-contiguous, insertion is a trivial matter of simply resetting a few links, so the operation is very efficient no matter where you insert. Linked lists are inherently dynamic, so you don't have to bend over backward to get them to resize themselves.

>I am not sure how to utilize this in real world programming!
File processing. You need to read all of the lines in a file and then sort them. You could use an array, but then you would …

Narue 5,707 Bad Cop Team Colleague

>can I get an example of a Binary Tree (top down)
The link I gave you covers these in detail.

>and a B-Tree (ground up)?
These are complicated enough that example code will be hard to find, but you shouldn't have any trouble finding B-tree info on google. I won't give you code or concepts because the search will be good for you.

>what the two types of Trees are good for, in real word programming?
Binary trees (usually of the binary search tree variation) have a wide range of uses. A good simple example is a large phonebook, where the records must be sorted, but still quickly accessible. B-trees are primarily used as external storage structures for databases.

Narue 5,707 Bad Cop Team Colleague
user_name = 'user'
for i in range ( 1, 100 ):
  user = user_name + str ( i )
  print user
else:
  print 'The loop is done'
Narue 5,707 Bad Cop Team Colleague

>1. Why do you need Self-Referential Classes?
It's the easiest way to implement non-contiguous data structures. You don't need them, but they make life easier.

>2. ^^^What is a Node, is it just a random name for this Class?
Node

>3. what does the (const Node *) mean in
It's a pointer to a Node that shouldn't be modified.

>4. why is nextPtr the, "Link?"
That's basic data structure terminology. A "link" leads from one node to another.

>Node *newPtr = new Node(10);
It creates a Node, calls the constructor with the value 10, and assigns the address of it to newPtr.

>delete newPtr;
It releases the memory pointed to by newPtr. This only works predictably for memory returned by new.

>6. What is that definition REALLY saying (plain english)?
That's an awful explanation of a linked list. Sadly, it's not that simple when first introducing people to the concept. This provides more suitable information.

>7. Can you please explain or provide an example of what is really being said here ^^^?
Basically, you can have a linked list of polymorphic types.

>8. How does a Tree work and what is it's purpose?
Go here and read up on the tutorials that talk about binary trees. When someone talks about a tree, they usually mean a binary search tree.

Narue 5,707 Bad Cop Team Colleague

>but does ( a, a + 10 ) insinuate a vector?
Not really. The standard algorithms take iterators, which only suggest a sequence. You give a beginning and an end, and the right thing just happens. You can think of the sequence as a vector, but that concept tends to break down when you consider iterators for non-linear containers such as std::set.

In the code I gave, sure, that analogy works because vectors are almost always implemented as an array. a is like saying v.begin() and a + 10 is like saying v.end(). However, it's not consistent with other potential uses of the iterator concept, so maintaining it would only serve to confuse.

Then again, I could be wildly misinterpreting your question. If that's the case, could you be more specific? :)

Narue 5,707 Bad Cop Team Colleague

>please give me an easier example
That's as easy as it gets. If you want something more basic then you're looking at writing a sort function manually, and that's decidedly not basic compared to just calling std::sort.

Is this a homework assignment?

Narue 5,707 Bad Cop Team Colleague
#include <algorithm>
#include <iostream>
#include <iterator>

using namespace std;

int main()
{
  int a[] = {5,7,4,6,3,8,0,1,9,2};

  sort ( a, a + 10 );
  copy ( a, a + 10, ostream_iterator<int> ( cout, " " ) );
}

;)

Narue 5,707 Bad Cop Team Colleague

>Why can't it just be separated as I was doing it.
It can. Depending on the needs of the application, the choice is yours about when to do what as long as everything works as it should.

>What fields are initialized or treated as "strings"?
If a sequence of characters is terminated with the '\0' character, it's a string.

Narue 5,707 Bad Cop Team Colleague

You're still thinking in base ten. Start thinking in base sixteen and things will be easier:

#include <iostream>
#include <string>

using namespace std;

void print_hex ( int dec )
{
  static const string hex_dig ( "0123456789ABCDEF" );

  if ( dec != 0 ) {
    print_hex ( dec / 16 );
    cout<< hex_dig[dec % 16];
  }
}

int main()
{
  cout<<"0x";
  print_hex ( 27 );
  cout<<endl;
}
Narue 5,707 Bad Cop Team Colleague

>The next step is to memcpy them back into variables.
It's the same thing with different source and destination arrays:

char field1[13] = {0};
char field2[7] = {0};

memcpy ( field1, table[i], 12 );
memcpy ( field2, table[i] + 12, 6 );
Narue 5,707 Bad Cop Team Colleague

C++ doesn't have native support for working with monetary values. You need to either find a library that does what you want, or roll your own:

#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

string currency ( double value )
{
  ostringstream sout;

  sout<< fixed << setprecision ( 2 ) << value;
  
  string s = sout.str();
  int n = 0;

  for ( string::size_type i = s.length() - 4; i > 0; i-- ) {
    if ( ++n == 3 ) {
      s.insert ( s.begin() + i, ',' );
      n = 0;
    }
  }

  return '$' + s;
}

int main()
{
  cout<< currency ( 1234567.890 ) <<endl;
}
Narue 5,707 Bad Cop Team Colleague

You may like this trick. :) If you treat your records as strings then you can easily place them in a single array and work with them using standard string operations. The cost is two extra characters per element.

#include <stdio.h>
#include <string.h>

#define MAX_ELEMENTS 2

char table[MAX_ELEMENTS][20];

void insert_record ( const char *record, int row )
{
  memcpy ( table[row], record, 12 );
  memcpy ( table[row] + 13, record + 12, 6 );
}

void print_record ( int row )
{
  printf ( "%s\n", table[row] );
  printf ( "%s\n", table[row] + 13 );
}

int main ( void )
{
  insert_record ( "abcdefghijkl123456", 0 );
  insert_record ( "lkjihgfedcba654321", 1 );
  print_record ( 0 );
  print_record ( 1 );
}

If the cost is too much for you then you can use fixed widths and forget about strings with only minor changes:

#include <stdio.h>
#include <string.h>

#define MAX_ELEMENTS 2

char table[MAX_ELEMENTS][18];

void insert_record ( const char *record, int row )
{
  memcpy ( table[row], record, 12 );
  memcpy ( table[row] + 12, record + 12, 6 );
}

void print_record ( int row )
{
  printf ( "%.*s\n", 12, table[row] );
  printf ( "%.*s\n", 6, table[row] + 12 );
}

int main ( void )
{
  insert_record ( "abcdefghijkl123456", 0 );
  insert_record ( "lkjihgfedcba654321", 1 );
  print_record ( 0 );
  print_record ( 1 );
}
Narue 5,707 Bad Cop Team Colleague

>Here's another: It will help one understand how to do it manually, increasing one's knowledge.
That's more like what I was expecting.

Narue 5,707 Bad Cop Team Colleague

>Because C is still out there along side C++
And if you're using C++ then you should take advantage of it. If the C solution is used because "C is still out there along side C++" then why use C++ in the first place? I'm sorry, but I find that excuse lacking.

>Is bitset a headerfile wich exists
bitset is a standard template class defined in the <bitset> header. It's handy every now and then, but for the most part it's not a commonly used class from the standard library.

>It must be iostream that does it since I didn't include any other headerfile.
Don't rely on that behavior though. When you use a something from the standard library, you should include the correct header for your code to be valid on every compiler.

Narue 5,707 Bad Cop Team Colleague

>but how do you accomdate for the letters a-f if the digits are greater than 9?
Use a string:

static const string hex ( "0123456789ABCDEF" );

This is a simple problem once you figure it out. The function shouldn't need to be more than 4 or 5 logical lines.

Narue 5,707 Bad Cop Team Colleague

>You just need to use bit_print(), use the following function:
Why must everyone do things the hard way?

#include <bitset>
#include <iostream>
#include <string>

using namespace std;

int main()
{
  cout<< bitset<16> ( 2685 ).to_string() <<endl;
}

;)

Narue 5,707 Bad Cop Team Colleague

>1) Do you understand what the idea is for this exercise when reading the task?
Yes, they want you to take three integers and pack them into one integer by partitioning a certain number of bits for a value that will never exceed those bits.

>2) If so, could you give an example of what is meant by it?
If I wanted to pack 3-29-05 (today) into an int by placing the day in the low-order 5 bits, the month in the next 4 bits up, and the year in the last 7 bits, the resulting binary value would be 0000101001111101, or 2685.

This can be easily done with the bitwise operators:

#include <iostream>

int main()
{
  int day = 29;
  int month = 3;
  int year = 5;
  int coded = 0;

  coded = year; // Put the year in the low order bits
  coded <<= 4; // Shift the year by 4 bits to make room for the month
  coded |= month; // Put the month in the low order bits
  coded <<= 5; // Shift the year and month by 5 bits to make room for the day
  coded |= day; // Put the day in the low order bits and you're done

  std::cout<< coded <<std::endl;
}
Narue 5,707 Bad Cop Team Colleague

>I still don't understand what this has got to do with my question concerning
>how the string class is defined in my small programm
Your question was confusing. ;)

>but still you speak about a string 'class' --> wich part determines the class
The class is defined for you when you include <string>. It's a class from the standard C++ library, so all you need to do is create an object of the class:

#include <string>

std::string object;
Narue 5,707 Bad Cop Team Colleague

>Could you explain this abit more Narue
Characters are actually small integers. When you say '0', you mean that you want the integer that represents '0' in the native character set. In ASCII, that value is 48. In other character sets, the value will likely be different, but you can still use '0' without knowing or caring what the integral value is.

>Because ASCII is limited you mean?
Because ASCII isn't the only character set available. For example, what happens when code you write that assumes ASCII is run on a system that uses EBCDIC? The program won't work properly because the integral values for EBCDIC are different than ASCII. That's why it's considered good practice to use the character representation instead of the integral value ('a' instead of 97 for example).

>And the string class is defined by --> ostringstream out;?
ostringstream is a completely different beast from basic_string. ;) basic_string is not a derived class, it stands alone. ostringstream derives from ostream and uses a basic_stringbuf instead of a basic_streambuf to do the dirty work.

Narue 5,707 Bad Cop Team Colleague

>I understand that using '0' (string value) is equal to the decimal amount of 48
No, it's equal to the integral value of '0'. Not everyone uses ASCII, and you would do well to remember that.

>we never declared an array
The string class overloads the subscript operator, so you can index a string just like you would an array.

>how does i get's the value of the digits
It doesn't. i is just an index into the string, where the values are held. I get the impression that you're confused because the value of i is equal to the next digit stored in the array. You may want to change the value of your floating-point value to avoid confusion. Try using 0.987 instead of 0.123.

Narue 5,707 Bad Cop Team Colleague

A memory leak is simply allocating memory and failing to release it when you're done. Technically, the following illustrates a memory leak:

#include <iostream>

using namespace std;

int main()
{
  int *p = new int;

  *p = 10;

  cout<< *p << endl;
}

In practice, the operating system will typically reclaim all memory allocated to a process when the process terminates, but that's not required by the language definition. However, long running programs can consume all fast memory and grind the program to a near halt, or crash the system if virtual memory is also exhausted. So any call to new must be paired with a call to delete:

#include <iostream>

using namespace std;

int main()
{
  int *p = new int;

  *p = 10;

  cout<< *p << endl;

  delete p;
}
Narue 5,707 Bad Cop Team Colleague
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

short amount (double val)
{
	short a = 0, i, totvalue = 0;
	ostringstream out;

	out<< val;
	string s = out.str();

	for (i = 2; i < s.length(); ++i) // Number 1
	{			
		totvalue += s[i] - '0'; // Number 2 & 3
	}

	return totvalue;
}

int main()
{
	double a = 0.999;

	cout<<"Value = " << amount(a) <<endl;

	return 0;
}
Narue 5,707 Bad Cop Team Colleague

The best is the one that works best for your needs. Try out several, there are quite a few free ones you can use to get a feel for what features you want.

Narue 5,707 Bad Cop Team Colleague

Sure, a good way to write a portable text editor is to design an ed clone.

Narue 5,707 Bad Cop Team Colleague

>why is it that in your example, the decimal numbers aren't adding up
Because it's your project and not mine. I was just showing you the simplest way of determining how many digits of precision there are.

>why do you use int n = 0, it seems you don't use it in the function?
Leftovers from an older version.

>I understand this to be a selection, but I don't understand what it's doing?
If there are no digits past the radix, subtracting 2 from the string length would give you -1 instead of the desired 0. The equivalent is:

if ( s.length() == 1 )
  return 0;
else
  return s.length() - 2;
Narue 5,707 Bad Cop Team Colleague

Yea, pretty much. Though writing a text editor is an amusing exercise.

Narue 5,707 Bad Cop Team Colleague

>don't know the name of ',' in English
Radix

>I don't know how I can do that
Floating-point is a bitch to work with. You would be better off converting to a string as vegaseat suggested:

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int precision (double val)
{
  int n = 0;

  ostringstream out;
  out<< val;
  string s = out.str();

  return s.length() == 1 ? 0 : s.length() - 2;
}

int main()
{
  cout<< precision ( 0.0 ) <<endl;
  cout<< precision ( 0.1 ) <<endl;
  cout<< precision ( 0.12 ) <<endl;
  cout<< precision ( 0.123 ) <<endl;
  cout<< precision ( 0.1234 ) <<endl;
  cout<< precision ( 0.12345 ) <<endl;
  cout<< precision ( 0.123456 ) <<endl;
}
Narue 5,707 Bad Cop Team Colleague

>I dont want to use the console as a middle man.
So you don't want the user to see either what he types or the contents of the file at any given time? Without an intermediate buffer (the console window for example), a file is nothing but a mysterious and invisible "thing" that you can only hope is working correctly.

Now that I know what you're trying to do, it seems pretty silly.

>1. My program starts of opening a console (obviously).
So open a console and use that as your intermediary between the user and the file.

>2. Then it creates a blank file and opens it up for the user to write something in it
Same thing. The console window is your window into the file.

>So when the new line character is entered, it reads the file
Why not just save the expression as a string, write it to the file when a newline is entered, process the string for a result, and then write the result to the file?

As an intellectual exercise this would result in you writing your own text editor that just happens to be dreadfully inefficient. The only interesting part of such an exercise would be to write it so that it can be plugged into any program so that the program can use an "interactive" file.

Narue 5,707 Bad Cop Team Colleague

Think getch instead of kbhit. An interactive file isn't a file you write directly to with the keyboard. An interactive file is a file that is saved with every change made. You use the console as the middleman between the keyboard and file, but every character you enter is written straight away.

The only difference between, say, vanilla Notepad and Notepad with an interactive file is that vanilla Notepad only saves when you tell it to. Notepad with an interactive file would save constantly so that if power is interrupted and you have to reboot, you don't lose any of your work.