Narue 5,707 Bad Cop Team Colleague

I hate having to double post, but I forgot to reply to you Josh.

>What kind of logic is that?
Statistical logic. If, given a significant random sample of smokers, all of them were kind enough to consider my feelings, it's safe to assume that the majority of all smokers will as well.

>Making smoking illegal in restaurants is the right thing to do..
You missed a few steps in your logic there. You don't want to die, so making smoking illegal in restaurants is the right thing to do...

Narue 5,707 Bad Cop Team Colleague

>Actually, I have and they refused. Not every situation is the same, "genius."
Yet you want to apply the same extreme solution (ie. I'm God and can tell everyone what to do) to every situation. Brilliant.

>And we're not talking about just one person smoking, we're
>talking about smoking in public places such as restaurants.
I don't recall being forced to go to public places. Do you really choose to hang out in places where people smoke when you clearly don't like it?

ndeniche commented: well said... +2
Geek 8-) commented: no one wants to be restricted +0
Narue 5,707 Bad Cop Team Colleague

>If people's choices are hurting others then yea, it is our
>business, and we can tell them how to live their lives

>When it endangers other lives. Of course!

And naturally you're qualified to make that choice. :icon_rolleyes: Tell me, have you ever thought to ask a smoker not to smoke around you? I have, and to date nobody has ever refused to give me space or put out their cigarette when I asked politely. Most of the time they immediately put it out and don't light up again while I'm around. Smokers aren't evil people who intentionally try to hurt you, so stop trying to act righteous.

Aia commented: Asking is always nice. Forcing is "almost" always wrong. +5
Narue 5,707 Bad Cop Team Colleague

Use the KeyPress events to check the character being entered. You can find a sample here.

Narue 5,707 Bad Cop Team Colleague

Daniweb has an IRC channel. :icon_rolleyes: There are also channels on many servers for the programming language or technology you're interested in. Big ones I've seen are on the EFNet, Freenode, and Undernet servers.

But Daniweb's channel needs more active people.

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

Quite frankly, if you need layman's terms, you don't want to use emacs.

Narue 5,707 Bad Cop Team Colleague

If I recall correctly, the download sites for each of those will tell you how to install them on your system.

Narue 5,707 Bad Cop Team Colleague

Probably GCC.

Narue 5,707 Bad Cop Team Colleague

Powershell is a command line. Your question makes just as much sense as if you asked us how to write C++ with the DOS prompt. And the answer is the same: use it to open a text editor, write your code in the text editor, use it to run your compiler, and use it to run the program.

Narue 5,707 Bad Cop Team Colleague

>one of them said that C uses 2 bytes in windows for int
It hasn't been that way for a while. On many modern implementations, int is 4 bytes and short is 2 bytes.

Narue 5,707 Bad Cop Team Colleague

>not if by default it equals to false.
You can default it to whatever you want, but the fact still remains that you're using the value of an argument to completely change the meaning of a function. That's an example of bad design. Take realloc for example:

q = realloc ( p, size );

What does this line of code do?

Narue 5,707 Bad Cop Team Colleague

It sounds like you want to refactor the changing part of the create member function into the change member function. Then in the create member function, you call change:

class Test {
public:
  void create()
  {
    // Create stuff
    change();
  }

  void change()
  {
    // Change stuff
  }
};

And your switch remains the same.

>Is there a way to change/use only a part of a function?
To answer your question, yes there are ways, but no there aren't any good ones. For example, you could add a boolean flag parameter that says whether to create or not. But that changes the meaning of the function and gives it two jobs rather than one. Bad idea.

Narue 5,707 Bad Cop Team Colleague

>I don't know what that code means.
Then you don't know functions well enough to use them yet.

#include <cstdlib> // Required for the system function
#include <iostream>

using namespace std;

void GetICAOWords ( const char letter );

int main()
{
  string inpStr;

  cout<<"Enter any word: ";
  cin>> inpStr;

  for ( string::size_type i = 0; i < inpStr.size(); i++ )
    GetICAOWords ( inpStr[i] );

  system("Pause");
}

void GetICAOWords ( const char letter )
{
  switch ( letter ) {
    // All of your cases
  }
}

You declare a function:

void GetICAOWords ( const char letter );

Define it:

void GetICAOWords ( const char letter )
{
  switch ( letter ) {
    // All of your cases
  }
}

And then call it

GetICAOWords ( inpStr[i] );

The declaration only tells the compiler the name, return type, and parameter types of the function. That's why you end it with a semicolon and not a block of code. The definition is where you actually assign a block of code to the function. The key thing to remember for now is that the definition of a function should be self-contained. That is, everything it does only relies on the parameters.

When you call a function, you give the parameters values by passing arguments. inpStr is an argument that gives a value to letter, which is a parameter. It goes like this if you take the function out of the problem and replace it with inline code:

Narue 5,707 Bad Cop Team Colleague

Try this instead:

void GetICAOWords ( const char letter )
{
  switch ( letter ) {
    // All of your cases
  }
}

That way you aren't confused by strings and indexing.

Narue 5,707 Bad Cop Team Colleague

Show us the attempt that failed and we can tell you why.

Narue 5,707 Bad Cop Team Colleague
if ( cin.get ( myString, 100 ) )
  cout<< myString[0] << endl;
  cout<< myString[1] << endl;
  cout<< myString[2] << endl;

If your conditional has more than one statement, wrap it in braces:

if ( cin.get ( myString, 100 ) ) {
  cout<< myString[0] << endl;
  cout<< myString[1] << endl;
  cout<< myString[2] << endl;
}

What you have now is equivalent to this:

if ( cin.get ( myString, 100 ) ) {
  cout<< myString[0] << endl;
}

cout<< myString[1] << endl;
cout<< myString[2] << endl;

Your code only appears to work like you want because with correct input it does what you expect. Try the following and type ctrl+z to signal end-of-file. The condition will fail, and what you would expect is no output, but since you didn't use braces, only the first cout statement is skipped. The actual output is two bangs:

#include <iostream>

using namespace std;

int main()
{
  char myString[100];

  for ( int i = 0; i < 100; i++ )
    myString[i] = '!';

  cout<<"Your statement" << endl;

  if ( cin.get ( myString, 100 ) )
    cout<< myString[0] << endl;
    cout<< myString[1] << endl;
    cout<< myString[2] << endl;
  system("Pause");

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

>It runs now, but just flashes on the screen after I input a word.
That's because you don't do anything to stop the program from ending. When the program ends, the window that Dev-C++ created to host it is closed. You can fix that by putting in a request for input:

#include <iostream>

using namespace std;

int main()
{
  char myString[100];

  cout<<"Your statement\n";

  if ( cin.get ( myString, 100 ) )
    cout<< myString <<'\n';

  cout<<"Press Enter to continue. . .";
  cin.get();

  return 0;
}

>Does it designate myString as an array with 100 characters?
Yes.

>If so, should I be able to use myString[0], mystring[1], etc.?
I don't see why not.

Narue 5,707 Bad Cop Team Colleague

>(exit(0) is a function to terminate the program after your work is over
>you may use it if you wish and is accessible from process.h)
exit is a function in cstdlib, or stdlib.h on your dinosaur of a compiler. The difference is that process.h isn't a standard header, so your code won't compile on many compilers. Also, exit ( 0 ) is identical in function to return 0; from main. So there's really no point in using it here.

It's always a good idea to avoid including headers you aren't actively using, which means that conio.h and stdio.h should be removed.

#include <iostream.h>

int main()
{
  char myString[100];

  cout<<"Your statement\n";

  if ( cin.get ( myString, 100 ) )
    cout<< myString <<'\n';

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

>What is the difference between "\n" and '\n'.?
>Also what is the difference between " " and ' '??
Nothing. :D

But seriously, the first is a string literal and the second is a character literal. The difference, aside from the data type, is that a string literal is an array of const char that always has a '\0' character at the end and a character literal isn't an array and doesn't have a null character.

Narue 5,707 Bad Cop Team Colleague

>if (0 ==start);
You have a rogue semicolon.

Narue 5,707 Bad Cop Team Colleague

>By input stream you refer to Array and whitespace-blank?
cin is an input stream. cout is an output stream.

>For this means that it will store upto 100 charecters .Right?
If it's just an array, yes. If it's a string, it will store up to 99 characters and always leave room for the '\0' terminator at the end of every C-style string.

Narue 5,707 Bad Cop Team Colleague

>I know one thing that pointers indicate to a value but after that no clue.
This should clear up several things about pointers.

>but on Left you have used 1-d char array
But it's not. It's a pointer to a one dimensional array. In this case you can think of it as a two dimensional array where the first dimension is set dynamically.

>I think you are again using 2 D array as 1D array
No, I'm using a one dimensional array as a one dimensional array. A two dimensional array is an array of arrays. If you declare ma as a two dimensional array, ma[0] is a one dimensional array.

>Besides what is meant by overfilling of the array that you use setw() here.
You need to tell cin when to stop. If you don't, it'll stop when it reaches whitespace. But what happens if the input stream contains 50000 characters with no whitespace? Well, cin will try to write all 50000 characters to your array. If your array can only hold 100 characters, that's a really bad thing.

Narue 5,707 Bad Cop Team Colleague

>#include<iostream.h>
>#include<stdio.h>
Don't use iostream.h, it's not valid C++ anymore. New compilers will refuse to compile your code. Also, stdio.h is deprecated in C++. The two headers you should be using are:

#include <iostream>
#include <cstdio>

And because the modern C++ standard library is in the std namespace, you need to qualify it. The easiest way to do that is using namespace std; , but that's generally not a good idea for reasons that you'll learn later.

>void main()
void main is wrong in both languages. It's never been correct in 40 years. main returns an int, always, without fail. This is your template for all new programs in C++:

#include <iostream>

using namespace std;

int main()
{
}

You don't need to return anything because 0 is returned implicitly by default. And this is the template for C:

#include <stdio.h>

int main ( void )
{
  return 0;
}

The latest C standard also returns 0 implicitly, but it's not widely implemented enough yet that you can adopt the convention. Not to mention the C community is leaning toward considering it bad practice to use that feature.

>gets(a);
gets is a bad idea. It's never safe, so you should just forget that it exists at all. In C, the replacement is fgets, and in C++ the replacement is getline.

>cout<<a[x];
I highly recommend not mixing C and C++ style I/O.

>but when I code I tend to …

Narue 5,707 Bad Cop Team Colleague

>-can we really treat char arrays as integers and use cin>>words (char type)
You can use cin's >> operator with any data type that overloads that operator. That means all of the built-in types and any custom types that support the operator. So yes, you can use cin's >> operator to read a C-style string. However, while I kept my example simple, to be strictly correct with an array of char you should take care to restrict the field width so that the array isn't overfilled:

#include <iomanip>

...

while ( i < 10 && cin>> setw ( 100 ) >> words[i] )

I say that because I can see you using this instead of getline to read strings, and/or not using the C++ string type for a while.

>-Secondly if words[10][100] is a 2-d array then can we really use as a single dimensional array
A two dimensional array is really just an array of arrays. If you index the first dimension, you get a one dimensional array and can use it as a one dimensional array.

>same way should it be
>int n;
>cin>>n;
>words[n][100];
No, C++ doesn't support non-constant array sizes, so you can't declare an array with user input. However, you can simulate an array using pointers. Most of the time you'll want to do this with both dimensions, but it's actually easier (though a tad more obscure) to do it only with the first dimension:


       
Narue 5,707 Bad Cop Team Colleague

>neither of them work
Because they both attempt to modify a string literal. Change this:

char * b = "Hello|World";

To this:

char b[] = "Hello|World";

I know it looks like nothing, but the difference is huge. The first is a pointer to a string literal, which is read-only and cannot be changed. The second is an array initialized with the contents of a string literal, which is owned by you and can be modified.

On another note:

>#include <iostream.h>
The iostream.h header doesn't exist in C++. You want to use <iostream> and get a newer book that tells you how to use it and a newer compiler that supports it.

>while (a < strlen(ret))
To use strlen you need to include <cstring>.

>int a; cin>>a; //Keep on screen
It's easier and more intuitive to simply use cin.get(); . What you have requires you to type the end-of-file key combination to end the program, but cin.get only requires you to hit Enter.

Narue 5,707 Bad Cop Team Colleague

>IMO it would have been better to have left her to her own devices to
>come up with a solution, instead of writing the entire code.
True, but in this case...not so much of an issue. That solution is rather poorly implemented and overly complex. If you're going to use a multidimensional array, the problem is trivial and takes practically no thought at all:

#include <iostream>

using namespace std;

int main()
{
  char words[10][100];
  int i = 0;

  cout<<"Enter a sentence: ";

  while ( i < 10 && cin>> words[i] )
    ++i;

  while ( --i >= 0 )
    cout<< words[i] <<' ';
  cout<<'\n';
}

The real problem is when you have to do the reversal in-place. Even then, I could still do it with less complexity than pengwn managed to put into the simpler solution. :icon_rolleyes:

Narue 5,707 Bad Cop Team Colleague

>most of what i want to compile is stuff i find on source forge or similar places that i can't get binary installers.
That kind of stuff should be very clear about what language is used so that you can pick the tool. But it's probably not a good idea to try building applications from source if you don't have any development experience. You might want to take up C or C++ with the gcc compiler to get a feel for how it all works.

Narue 5,707 Bad Cop Team Colleague

>i was wondering how you go about compiling programs from source for windows XP.
We could be of more help if you told us what programming language you want to compile. But at the very least, you need a tool that will turn your source file into something that can be executed, called a compiler or interpreter.

Narue 5,707 Bad Cop Team Colleague

>But can you provide me a link to the latest standard docs..
The actual standard isn't freely available, though you can buy the PDF from www.ansi.org for $18. However, the draft documents are close enough (with only differences in wording for the most part).

You can find the C89 standard (the old one) here, and the C99 standard (the current one) here. I don't have a handy link to the C++ draft standard, but I'm sure google can give one to you with minimal effort.

Narue 5,707 Bad Cop Team Colleague

>Just don't become a teacher okay?
As you wish. From this point on, I'll refuse to help you ever again.

Narue 5,707 Bad Cop Team Colleague

I can't say it any simpler. Make the name const and treat it as const. If you can't figure it out, you'll have to work around it not being const.

Matt Tacular commented: Not needed, just don't comment if you can't help the poster in some way. +0
Narue 5,707 Bad Cop Team Colleague

>Is there any way I can make the name a constant?
Yes, qualify it with const in your class and use an initialization list in the constructor:

class humanPlayer {
  const std::string playerName;
public:
  humanPlayer ( const std::string& name )
    : playerName ( name )
  {}
};
Narue 5,707 Bad Cop Team Colleague

Unexpected characters means you're probably accessing an array out of bounds. When running the program, print out the indexes whenever you access an array for printing. If the index isn't what you expected, that's the symptom of the bug and you can trace it back to where the index got the wrong value.

Narue 5,707 Bad Cop Team Colleague

>Is there a way I can do an actual array of arrays? Not a multidimensional one.
There's no such thing as a multidimensional array in C++. It's always an array of arrays. What you want is a ragged array, and the only way to do it is to manually simulate the arrays:

#include <iostream>
#include <ios>
#include <limits>

int main()
{
  int **a = new int*[2];

  a[0] = new int[5];
  a[1] = new int[3];

  for ( int i = 0; i < 5; i++ )
    a[0][i] = i + 1;

  for ( int i = 0; i < 3; i++ )
    a[1][i] = 3 - i;

  int index;

  std::cout<<"Enter 1 for ascending or 2 for descending: ";

  while ( !( std::cin>> index ) || !( index == 1 || index == 2 ) ) {
    std::cerr<<"Invalid input\n";
    std::cin.clear();
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    std::cout<<"Enter 1 for ascending or 2 for descending: ";
  }

  int limit = 5;

  if ( index != 1 )
    limit = 3;

  for ( int i = 0; i < limit; i++ )
    std::cout<< a[index - 1][i] <<' ';
  std::cout<<'\n';

  delete[] a[1];
  delete[] a[0];
  delete[] a;
}

That's a lot harder and a lot easier to screw up. Alternatively, and this is your best option, use a vector of vectors:

#include <iostream>
#include <ios>
#include <limits>
#include <vector>

int main()
{
  std::vector< std::vector<int> > v;

  v.push_back ( std::vector<int> ( 5 ) );
  v.push_back ( std::vector<int> ( 3 ) );

  for ( int i = …
Narue 5,707 Bad Cop Team Colleague

>I was thinking maybe an array of arrays
You were thinking correctly. Something like this perhaps?

#include <iostream>
#include <ios>
#include <limits>

int main()
{
  int a[][5] = {
    {1,2,3,4,5},
    {5,4,3,2,1},
  };
  int index;

  std::cout<<"Enter 1 for ascending or 2 for descending: ";

  while ( !( std::cin>> index ) || !( index == 1 || index == 2 ) ) {
    std::cerr<<"Invalid input\n";
    std::cin.clear();
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    std::cout<<"Enter 1 for ascending or 2 for descending: ";
  }

  for ( int i = 0; i < 5; i++ )
    std::cout<< a[index - 1][i] <<' ';
  std::cout<<'\n';
}

The only problem here is that each sub-array has to have the same number of cells (5 in the previous code). So if you want a ragged array you either need to do some dynamic allocation (which I don't recommend just yet) or pad the shorter arrays with a sentinel value like -1.

Narue 5,707 Bad Cop Team Colleague

>Okay. What about "bad" ones?
Search for "C++/CLI tutorial" on google. I'm sure you can find plenty of bad ones. I don't know of any decent ones that are thorough enough and beginnery enough.

Narue 5,707 Bad Cop Team Colleague

>Do you have any good idears to prevent it?
Yes, if you have code that looks like cin>> s where s is an array, it's wrong. As you've seen, that doesn't protect against a buffer overflow. You can fix it by setting a maximum field width if you really have to use the >> operator:

#include <iostream>
#include <iomanip>

int main()
{
  char buffer[5];

  std::cin>> std::setw ( 5 ) >> buffer;
  std::cout<< buffer;
}

But for reading strings, the getline method is often a much better choice:

#include <iostream>

int main()
{
  char buffer[5];

  std::cin.getline ( buffer, sizeof buffer );
  std::cout<< buffer;
}

>Can anyone tell me what this particular line does...
It's an initialization list for the class constructor. You can get the same effect (in this case) with this code:

bot()
{
  password = 567;
}
Narue 5,707 Bad Cop Team Colleague

>I believe that ANSI C actually standardized the fact that a function with
>no return type specified will be taken as int.
The implicit int rule was removed in the most recent standard. It was a poor practice before, and it's illegal now.

>I believe thats the reason most compilers let u have it your way with that..
Sort of. The latest standard isn't widely implemented yet, so most compilers will be using the previous standard to compile. In that standard, implicit int is still allowed. But the community doesn't recommend it for various good reasons.

Narue 5,707 Bad Cop Team Colleague

>Firstly, I would lke to know is C++ GUI the same as C++.NET or there isn't such thing as C++.NET?
It's the other way around. There isn't such a thing as C++ GUI. There are a number of libraries that let you create a GUI using C++, but that's different from a single library that can be compared with C++.NET.

>Secondly, although you won't suggest me to learn C++.NET and you
>would ask me to learn C#, but I'm will still be asking for C++.NET/C++ GUI tutorials.
There's a reason that C# is recommended over C++ for .NET work, and the reason is that C++ was butchered into supporting .NET and that new language (C++/CLI) is painfully inconsistent and awkward to use. But if you're really interested, you don't want tutorials. You want a good book.

>What I mean by good are easy to understand, written in simple English,
>no errors/mistakes in the tutorials.
That kind of good is extremely hard to come by.

Narue 5,707 Bad Cop Team Colleague

>Hmm..but I do vaguely recollect someone mentioning something similar
>a long time back on the newsgroups.
Array initializers are required to initialize the cells to the data type's variation of 0. So double would be 0.0, float would be 0.0f, int would be 0, char would be '\0', and a regular pointer would be NULL. memset can't do that, so at the very least we could only use it for integral types.

Narue 5,707 Bad Cop Team Colleague

>But int arr[BUFSIZ] = {0}; internally uses memset to actually perform its task.
Would you like me to give you some time to take that back before I reply? Because you really aren't going to like it if I do. ;)

Narue 5,707 Bad Cop Team Colleague

In that case memset is better. In just about every other case not involving arrays of integral type, it's a really bad idea. memset does a zero-fill of the array, and most types in C++ either don't allow that, or it doesn't represent a zero value. Since this is C++, the copy function template from <algorithm> would be a much better choice.

Narue 5,707 Bad Cop Team Colleague

Are you actually using the Win32 API to do this? You were talking about forms, so I gathered that you were using Windows Forms, which is .NET. Maybe you should post your code.

Narue 5,707 Bad Cop Team Colleague

>I don't really know the specific one to raise.
The event is raised when you click the button. You need to handle that event in the calling form. Your documentation should cover how to do this in detail.

Narue 5,707 Bad Cop Team Colleague

Yes, you can do it that way if you want. I don't think it's the best solution, but I get the feeling you don't want to follow my advice since you completely ignored my last post.

Narue 5,707 Bad Cop Team Colleague

That sounds like the kind of situation where you would raise an event and pass the data as an argument to the event.

Narue 5,707 Bad Cop Team Colleague

Why in the world do you need those unsigned int variables to initialize the array? You can just do this and get the same effect:

int countryPopulationArray[20] = {0};
Narue 5,707 Bad Cop Team Colleague

>I encounterred a person who said that C++ is an unsafe language.
It is and it isn't. C++ doesn't do much to protect you from doing something wrong, so in that light it's an unsafe language. However, if you do things right, it's perfectly safe. I'd say that person was wrong for making such an absolute and general statement, but a lot of absolute and general statements have some ring of truth.

>but when I input an non-int type,the program will fall in a bad loop.
That's because the code isn't written to handle unexpected input. In this case, cin expects a valid integer. If you don't give it a valid integer, it goes into an error state and won't let you read any more input until the errors are corrected and the error state is cleared. This isn't a case of C++ being unsafe, it's a case of the author not knowing how to properly handle I/O in C++. Try this:

#include <iostream>
#include <string>
#include <ios>    // for streamsize
#include <limits> // for numeric_limits

using namespace std;
class bot
{
private:
  int password;
public:
  bot():password(567){}
  virtual ~bot(){};
  bool checkpwd(const int pwd)
  {return (password == pwd);}
};

int s;
bot b;

int main()
{
  for(;;)
  {
    cout<<"Enter password: ";

    if ( cin>>s ) {

      if(b.checkpwd(s))
      {
        cout<<"Access permitted"<<endl<<endl;
        break;
      }
      else
        cout<<"Access denied"<<endl<<endl;
    }
    else if ( !cin.eof() ) {
      // Notify the user
      cerr<<"Invalid password\n";

      // Clear the error state
      cin.clear();

      // Remove the …
Narue 5,707 Bad Cop Team Colleague

How would that make things more readable? B is only accessible to A, so the only place you can create the typedef is inside A, and the only place you need to qualify it with A:: is in an out of line definition. If you typedef B as C, you still need to qualify it with A:: in an out of line definition. You'd actually be making the code less readable.