Narue 5,707 Bad Cop Team Colleague

1) Platform and implementation dependent. What compiler and OS are you using?

2) This is in any book on C++.

>please help...we haven't discussed it in class...
You'll be in big trouble if you wait until everything is discussed in class before you do your own research.

Narue 5,707 Bad Cop Team Colleague

>may i know how do i implement the circular linked list?
Quick and dirty:

#include <iostream>

using namespace std;

struct node {
  int data;
  node *next;

  node ( int init, node *link )
    : data ( init ), next ( link )
  {}
};

int main()
{
  node *list = new node ( -1, 0 );
  node *it = list;

  list->next = list;

  for ( int i = 0; i < 10; i++ ) {
    it->next = new node ( i, list );
    it = it->next;
  }

  it = list->next;

  for ( int i = 0; i < 5; it = it->next ) {
    if ( it->data == -1 ) {
      cout<<endl;
      ++i;
    }
    else
      cout<< it->data <<"->";
  }
}
Narue 5,707 Bad Cop Team Colleague

>I have tried using system command but to no use.
How did you try it? Is this in a separate thread? Does it not work because of a syntax error or something else? Be as specific as possible and give code examples. Otherwise you'll get nothing but stoney silence.

>Note : I am using Borland Turboc3 3.0 compiler.
Jeez, get a newer compiler. :rolleyes:

Narue 5,707 Bad Cop Team Colleague

>If you meet any error plz inform me.
Okay.

>#include<conio.h>
Nonstandard header, typically only Windows or DOS compilers will have it. But AFAIK Borland compilers are the only ones that have a fully featured conio.h. Using it is dicey at best because the functions from it that you use may or may not be on another compiler that has conio.h, much less those that do not.

>main()
Be explicit as C will refuse to accept this in the future:

int main ( void )

>getch();
Nonstandard function, use at your own risk. But getchar works just as well. Of course the counter argument is that getchar doesn't return until a newline (or error) is encountered. But really, how much is it to ask a user to type return rather than any key?

>exit(0);
Why are you returning 0 if the program is terminating on an error? 0 means success, so you would be better off including stdlib.h and returning EXIT_FAILURE.

>char ch;
This tells me you're compiling as C++. Declarations can only be placed at the beginning of a block in C89. C99 changed this, but because you're using conio.h I feel reasonably comfortable calling you on the error as I don't know of any compilers that conform to C99 and support conio.h.

>while(ch!='e'||ch!='E')
&& would bemore appropriate than ||.

>}
>}
main returns an int, so return an int or suffer undefined behavior.

Narue 5,707 Bad Cop Team Colleague

>Is there a way to convert a user entered (cin) char to an integer?
If you want to read each digit separately then it's a lot harder. Your best bet would be to read the entire number as a string and then parse it:

#include <cctype>
#include <iostream>
#include <string>

using namespace std;

int main()
{
  string s;

  if ( getline ( cin, s ) ) {
    static string hex_digits ( "0123456789ABCDEF" );

    string::const_iterator it = s.begin();
    string::const_iterator end = s.end();

    for ( ; it != end; it++ ) {
      string::size_type val = hex_digits.find ( toupper ( *it ) );

      if ( val != string::npos )
        cout<<"The value of "<< *it <<" is "<< val <<endl;
    }
  }
}
Narue 5,707 Bad Cop Team Colleague

From a purely "ignorant user" standpoint, Firefox has some annoying browsing aspects that I've encountered. One of the most irritating is when going back or forward, the browser sends you to the top of the page rather than the place you were last like IE does. IE is comfortable, but Firefox has some handy features. Because I'm unable to make a decision, I arbitrarily use Firefox on my notebook and IE on my desktop.

Beyond the "ignorant user" perspective, Firefox begins to look progressively better than IE in feature sets, customizability, security, and performance (in my experience). But because I do a lot of netdiving, the browsing experience is important and IE has a tendency to beat Firefox in that category. But this is purely personal preference.

Narue 5,707 Bad Cop Team Colleague

Empty your sent items folder if there aren't any large items in the inbox.

Narue 5,707 Bad Cop Team Colleague

Post the error that you're getting. Often, a syntax error may not be on the line given by the message. It could be a few lines up and the effect of the error isn't seen by the parser until that line.

Narue 5,707 Bad Cop Team Colleague

>It looks as if you are automatically assuming that Z is an integer
That's a reasonable assumption seeing as how Z was declared as int.

>When it's not an integer, but it's treated as one, you could end up in some sorta loop further down in your program.
The problem is with cin. The >> operator of cin will figure out what type the object is and convert the data from the standard input stream to that type. If there's no conversion then cin will leave the unconverted data in the stream and enter a failure state. This is a common problem with loops like this:

int number;

while ( cin>> number )
  cout<< number <<endl;

If a letter is entered, this will be an infinite loop because cin will continue to fail on the invalid data in the stream. The solution is to remove the offending input and clear the stream or read all data as a string and parse it for error handling. The latter is easy and the former can be done (rather naively) like this:

#include <iostream>

using namespace std;

bool get_number ( int& number )
{
  while ( !( cin>> number ) ) {
    if ( cin.eof() )
      return false;
    else {
      char ch;

      cin.clear();

      cout<<"Invalid input, please try again: ";
      while ( cin.get ( ch ) && ch != '\n' )
        ;
    }
  }

  return true;
}

int main()
{
  int number;

  while ( get_number ( number ) ) …
Dave Sinkula commented: A little harsh on the admin, but a good thorough explanation and solutions nonetheless. +1
Narue 5,707 Bad Cop Team Colleague

If you bothered to read the last week or so of posts then you would have seen that I gave a complete implementation for something like this. You could easily modify that or use it as a basis for your own implementation. But no, you wanted someone to do your homework to your exact specifications. Anything else would require real work. :mad:

Narue 5,707 Bad Cop Team Colleague

>iostream is somewhat more restrictive than the older iostream.h.
If by "more restrictive" you mean the requirement to qualify the std namespace when using a standard name. iostream is vastly more powerful and flexible than iostream.h and is more fully featured.

>iostream.h is deprecated
No, iostream.h is gone. It was completely removed when the language was standardized. Deprecated means that the feature is still supported but may be removed in future revisions.

>It is sometimes recommended to use
Of course a better recommendation would be either explicitly qualifying names that you intend to use with a using declaration:

using std::cout;

or with every use:

std::cout<<"blah blah"<<std::endl;

The using directive is no better than how iostream.h leaves every name in the global namespace.

Narue 5,707 Bad Cop Team Colleague

>difference between c and c++ languages
++

>difference between templates and class, and structures and templates
There's no comparison, the two solve different problems.

>structures in c language and class
C structures have fewer features.

>difference between java and c++
A world.

>explain memory allocation in c asnd c++
Ask for memory and then use it. In C the most common way is

p = malloc ( n * sizeof *p );

or

p = malloc ( sizeof *p );

And in C++ the equivalent is

p = new type[n];

or

p = new type;

>explain what is procedural language as well as object oriented language distinctly in simple words
Procedural language: Do this, then this, then this until the problem is solved.
OO language: Use these objects to solve the problem.

So...at what point do you plan on doing your own research? People won't always hand out the answers to you, so it would be a good idea to get into the habit of reading and searching for information that you want rather than just posting your (vague and thus difficult to answer properly or concisely) questions and presumptuously expecting others to do your work for you.

alc6379 commented: VERY good post! +3
Narue 5,707 Bad Cop Team Colleague

>Any ideas why I can't include it?
Yes, fstream.h is not a standard C++ header. Nor is iomanip.h. Come to think of it, void main isn't standard either (regardless of what your compiler's documentation says). If you want to go fully standard, use C headers with the .h dropped and prefix them with C, and use C++ headers with the .h dropped. Then prefix every standard name with std:: (or you can use using namespace std if you want) because all standard names are in the std namespace. Also, there's no need to manually close the file. ofstream's destructor will handle that for you. Lastly, you had a bug where the first loop declares a variable yet you try to use it after the loop. This is wrong because the variable is declared within the scope of the loop and when the loop ends the variable is destroyed. You can do what you were doing with older versions of Visual C++, but not anymore:

#include <cmath>
#include <fstream>
#include <iomanip>

int main()
{
  int a[25];
  double s[25];

  for(int n=0; n < 25; n++)
    s[n] = std::sin(3.14159265*(a[n]=n*15)/180);

  std::ofstream out("SineDataV2.txt", std::ios::out);

  for(int n=0;n<25;n++)
    out << std::setw(5) << a[n] <<' '<<std::setw(12)<<s[n]<<'\n';
}
Narue 5,707 Bad Cop Team Colleague

>What I'm having trouble with is figuring out a way to keep track of which numbers were entered.
You're on the right track with this:

int num[1];

An array of one is pretty useless, but if you change that to, say 10, you can use j to figure out how many numbers there were and another index to print them:

for ( int i = 0; i < j; i++ )
  cout<< num[i] <<endl;
Narue 5,707 Bad Cop Team Colleague

There's no portable way to do this. Maybe if you told us what your operating system and compiler were we could help more.

Narue 5,707 Bad Cop Team Colleague

>Is it possible to open notepad and type some gibberish and save it as a .exe file and run it?
If you want to work out the machine code for your system that would be equivalent for a properly assembled executable, then figure out what combination of keyboard characters will give you that machine code...I suppose it might be possible to do this if Notepad doesn't add anything in the process of saving the file as an executable.

Narue 5,707 Bad Cop Team Colleague

Visual Studio is an IDE, so you should just be able to create a project and add your header files and source files to the proper folders in that project. Then it's just a matter of typing F7 to build everything.

Narue 5,707 Bad Cop Team Colleague

Don't play around with casting like that unless you know what you're doing. Type punning can be tricky business, but barring transmission issues with your sockets, this should work:

#include <cstring>
#include <iostream>

using namespace std;

int main()
{
  int i1 = 12345;
  char buffer[sizeof ( int )];

  memcpy ( buffer, (unsigned char *)&i1, sizeof ( int ) ); // Simulate send/recv

  int *pi = (int *)buffer;
  cout<< *pi <<endl;
}
Narue 5,707 Bad Cop Team Colleague

>Just change the for loops into while loops!
And this change is supposed to solve what, exactly? It makes no difference what kind of loop it is as long as they do the same thing, which they do. In this case a for loop is the better choice anyway because it shows the intention of the code more clearly.

>#include<iostream.h>
This is not a standard C++ header. I highly recommend using iostream and qualifying standard library names with std:: (one way or another) if you want your code to be correct. If your compiler doesn't allow that then get a newer compiler. Standard C++ offers many advantages over pre-standard C++.

>void main()
main has never returned void and probably never will. This invokes undefined behavior according to the C++ standard. If your compiler accepts it and you really want to, you can use it in your own programs, but please refrain from posting such drivel around here and trying to palm it off as good code.

Narue 5,707 Bad Cop Team Colleague

>asking how to write a calender in c++ using for loop
No problem:

int main()
{
  for ( int i = 1; i <= 12; i++ )
    print_calendar ( i, 2004 );
}

Of course, the print_calendar function is slightly more complicated. ;) Here's a quick stab at it:

void print_calendar ( int month, int year )
{
  int i;
  char month_name[20];
  struct tm date;

  date.tm_mday = 1;
  date.tm_mon = month - 1;
  date.tm_year = year - 1900;

  if ( mktime ( &date ) == -1 )
    return;

  strftime ( month_name, 20, "%B", &date );

  cout<< month_name <<'\n';
  cout<<"\nSun Mon Tue Wed Thu Fri Sat\n";

  for ( i = 0; i < date.tm_wday; i++ )
    cout<<"    ";

  do {
    cout<< left << setw ( 4 ) << date.tm_mday;

    if ( ++i % 7 == 0 )
      cout<<'\n';

    ++date.tm_mday;
    mktime ( &date );
  } while ( date.tm_mday != 1 );

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

>Though it isn't recommended to do a statement like so
Right answer, wrong reason. Assigning a string literal to a pointer to char is a deprecated feature in C++. The correct way would be:

const char *b = "123";

>why the object "b" isn't deleted? <---puzzle at here
Not to take away from Stack Overflow's answer, but I can summarize. The memory for b is released, but the memory for what b points to is not. The reason is that string literals have static storage duration. In other words, the memory exists and can be accessed (but never modified) for the duration of the program as long as you have a pointer to it.

Narue 5,707 Bad Cop Team Colleague

>Is it possible to create programmes to run on coumputers thad do not have any operating systems?
No, since to do so would require you to write an operating system to some extent.

Narue 5,707 Bad Cop Team Colleague

>It wouldn't be hard for someone to modify it and make it safer.
Don't forget that someone would also have to modify it to make it correct as well as safer. :) I was implying previously that your code has a bug.

» It is standard, but the standard just isn't widely implemented yet.
>Standard only in C99.
Yes, that would be the standard that isn't widely implemented yet. Unless you know of any others that is. ;)

>Bitwise XOR Operator ^
>a^b: 1 if both bits are different. 3 ^ 5 is 6.
I'm well aware of what it does and how it does it. What confuses me is why people still think that it's a neat trick. The XOR swap is difficult to get right because of obscurity, and when it is right, it isn't very flexible. On top of that, it may not be as efficient as you think.

Narue 5,707 Bad Cop Team Colleague

>Though remember sprintf() does not calculate for buffer overflow.
Neither does itoa. ;)

>A safer version of sprintf() is snprintf() which unfortunately isn't very standard yet.
It is standard, but the standard just isn't widely implemented yet. As it is, C89 is the dominant C standard, and snprintf is nothing more than an extension on many compilers.

>Writing your own version of itoa() on the contrary is quite simple.
Yes, but doing it correctly is not as trivial as some would have you believe.

>*p1 ^= *p2;
>*p2 ^= *p1;
>*p1 ^= *p2;
It's beyond me why people still use this.

Narue 5,707 Bad Cop Team Colleague

>Narue, when you say pointers are passed by value, I don't understand what you mean.
The pointer is passed by value, even if you can get to the original object by dereferencing it. This is a poor man's call-by-reference because you aren't actually calling by reference, just passing an address so that you can fake the behavior of call-by-reference.

I admit that it's a subtle difference, but trust me that it's an important one to understand completely. :)

Narue 5,707 Bad Cop Team Colleague

>2. Using a pointer to simulate pass by reference.
Much better. ;)

>Too late(?)
Better late than never.

Narue 5,707 Bad Cop Team Colleague

>1. By value.
Okay.

>2. By pointer.
Pointers are passed by value, let's not add to the massive amount of confusion surrounding pointers, k? ;)

>3. By reference.
Okay.

You can also pass values and types to a function by way of templates. But the original question is rather vague, so it might be best to wait for clarification before answering definitively.