Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The two friend functions must return a reference to the istream and ostream objects. Here's correction

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
#pragma warning( disable: 4996) // only needed for VC++ 2008 compiler

class aName
{
friend std::istream& operator>>(std::istream & input,aName &name);
/*input the name from the standard input stream like the keyboard and the name is never empty*/
friend std::ostream& operator<<(std::ostream & output,aName name);
//it displays the name on the standard output like the screen

private:
char*surname,
    *firstname;

public:
    aName() { surname = NULL; firstname = NULL;}
    aName(char *aSurname,char*aFirstname)
    {
        surname = NULL; firstname = NULL;
        set(aSurname, aFirstname);
    }

    void set(char** name, const char* aName)
    {
        if( *name != NULL)
            delete[] *name;
        *name = NULL;
        if( aName != NULL)
        {
            *name = new char[strlen(aName)+1];
            strcpy(*name, aName);
        }
    }
    void set(const char *aSurname,const char* afirstname)
    {
        set(&surname, aSurname);
        set(&firstname, afirstname);
    }
};

istream& operator>>(istream& in, aName& nm)
{
    // TODO:  complete this function.
    return in;
}

int main()
{
    aName n;
    cin >> n;
}

[edit]Oops! I just noticed I did half your homework:(

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>surname=aSurname;

First you have to allocate space for surname then call strcpy() to copy it

surname = new char[strlen(aSurname)+1];
strcpy(surname, aSurname);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

put links to SNIPPED in your signature then contribute to the discussions here at DaniWeb.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You need to post the input data file (zip it up first!) because the program you posted produces these errors on the three example lines you posted in your post #1.

Dml Error: at line 1
Dml error: Missing ; at line 2
Press any key to continue . . .

it works upto 10 columns.

when the column name exceeds 10

it shows "segmentation fault"

>>record->column_delim=(char**)malloc(10*sizeof(char*));

That line looks awfully suspicious of the reason for your error. It is only allocating room for 10 pointers

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

i have written the code in notepad and compiled in UNIX environment with gcc compiler .

The most horrible programming environment ever. Why code it on Windows and compile it on *nix? You have a perfectly good IDE on *nix called vi, and I know for a fact there are even better ones. On Windows you have several free IDEs that will do auto-intent -- VC++ 2008 Express and Code::Bocks are just two of them. Learn to use IDEs because they will make your life a lot simpler so that you can become a more efficient coder.

And if you still insist on using Notepad, I know it has a tab key -- learn to use it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The program may not display the data immediately after calling cout, but hold it somewhere in memory until there is enough data to be displayed. That behavior saves a lot of time because displaying text is pretty cpu intensive. The flush() function forces the os to output the data to the output stream immediately instead of waiting for the output buffer to fill up.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>will there be a newline in the buffer after running this code?
Yes, and I think we have already explained that.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
serkan sendur commented: i just wanted to add to your reputation papa +4
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Install and try FireFox and see if it has that same problem.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>This and other "new" ways of doing things makes it seem as if Microsoft is inventing a whole new language.

They did :) CLR != C++ != C#

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You usually have to clear the input buffer

  1. After entering numeric data, such as short, int, long, float and double
  2. After using cin >> input operator because it stops at the first space
  3. After using a character array that is smaller than the number of characters typed at the keyboard

The code you posted is an infinite loop unless you press Ctrl+Z at they keyboard.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The project is probably not linking with the library that is produced for that dll, most likely akrip32.lib, or libakrip32.a, or something like that.

Stefano Mtangoo commented: You helped me alot! +4
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>s = new_cstring;

That is only changing the local copy of the string, not the original. Do not create a new character array, but work directly with the pointer that is in the parameter to that function.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 18: should be this: if ( strcmp(file,"*.tar") == 0) The way you had it was comparing two address, not their contents. But Vernon is right about using strstr() to see if ".tar" was entered in the file name.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Look at the function I posted above. All you have to do is change the parameter types in the three overloads

MyString MyString::operator+(const MyString& rightOp) const
MyString MyString::operator+(const char* rightOp) const
MyString MyString::operator+(const std::string& rightOp) const
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Don't you want this function to be outside of the class?

class MyString
{
    // ...
};

MyString operator+ (const MyString &lhs, const MyString &rhs)
{
    return MyString(lhs) + MyString(rhs);
}

Or something like that?

No -- that is illegal syntax for non-class methods.

error C2270: '+' : modifiers not allowed on nonmember functions

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Yup -- I tested that too and it compiled ok.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

make it a reference parameter, not a return value

void f(vector<vector<int>> & ay)
{
   // fill in the vector
}

int main()
{
    vector<vector<int>> ay;
    f( ay );
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I see two things wrong:
1) remove the const keyword at the end -- the function is not const because it changes the value of stringStorage.

2)You need another class object. What you wrote was for the += operator, not the + operator.

MyString MyString::operator+(const MyString& rightOp)
{
    MyString tmp = *this; // requires overloaded = operator
    strcpy(tmp.stringStorage, rightOp.stringStorage);
    return tmp;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Well, if you don't want the user to press a key then delete the cin.get() lines -- that's what that function is used for.

>>cin.get() is discarded somehow.
Probably because a previous cin operation was for an integer, which leaves the '\n' in the keyboard buffer, and the next cin.get() will remove that '\n' from the keyboard buffer and toss it away.

int num;
cout << "Enter a number\n";
cin >> num;
cin.ignore(); // flush the '\n' from keyboard buffer
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you mean if the user types "Hello World" <Enter key>, you want to add '\n' to the end of the string?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If you are lucky and if you haven't change the code very much you could get 0 memory leaks. You should make a backup.

The memory leaks I was talking about was in the libraries, not the OPs program. The writers of those libraries may have just been too lazy to clean up after themselves when finished.

I tried to compile those libraries and had to change the file extensions from *.c to *.cpp in order to get them to compile with Code::Blocks because they contained c++ code. I don't know how the authors ever got them to compile as C files.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
sprintf(cmd,"python mypyfile.py %s",args.c_str());
   system(cmd);

You could do it this way to, to avoid using C character arrays.

string args = "python mypyfile.py ";
    for(int i = 1; i < argc; i++)
    {
	args += ' '; // add space between args
        args += argv[i];
   }
   system(args.c_str());
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

See my previous post -- I added a line to put a space between the arguments.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Of course there is

int main(int argc, char* argv[])
{
    string args;
    for(int i = 1; i < argc; i++)
    {
        args += argv[i];
        args += ' '; // add space between args
   }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The problem appears to be in winbgim.a -- The function cls_OnClose() calls exit(). If you download the source to that library you can change that behavior. But if you do that it may have other unintentional consequences, such as memory leaks.

tux4life commented: Nice answer! +14
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

might be useful to provide a link that explains that function so that people here know what you are asking. Also state the operating system and compiler.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

post the class then we can discuss how to implement to >> operator.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Each has its own purpose: MyClass A; will disappear from view then the function exits (unless its global), and you can not specify the type of object, whether it is grandfather of class B or class C. The only way to do that is to use a pointer.

class BaseClass
{
   // blabla
};

class Derived1 : public BaseClass
{

};

class Derived2 : public BaseClass
{

};

int main()
{
    vector<BaseClass*> bList;
    BaseClass* pBase = 0;
    if( <condition1> )
      pBase = (BaseClass *)Derived1;
   else
      pBase = (BaseClass *)Derived2;

   bList.push_back(pBase);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Microsoft compilers no longer supports generating makefiles. Instead, it has a command-line tool to use the solution file. See this article.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

try this:

#if defined _WIN64 || defined _WIN32
cout << "This is Windows." << endl;
#endif
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Answer here. Next time try googling for the error number and you will probably get the answer you want.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The above doesn't give a compiler error for me, although it doesn't evaluate the or condition.

If using a Microsoft compiler you have the wrong macros #ifdef _WIN32 || _WIN64

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Hey AncientDragon,

thanks for the great visual reply. I tried your method in MSVC 2008 and it works. Unfortunately I have all of my code in MSVC 6.0 so I get compile errors if I use MSVC 2008. Do you know how to implement your method for MSVC 6.0, if I double click on button I get a "Push Button Properties" window, is there a way to get a "add member function" window in MSVC 6.0??? Thanks for your help.

Colm

That was vc6 that I posted. MFC for VC6 and VC2005/8 are not compatible -- Microsoft made a lot of changes to MFC including replacing many classes with templates.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Below are three pictures -- The first shows the basic main dialog with one "second" button. The second shows the window after double clicking on it, and the third shows what happens after hitting the Ok button

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

To get a list of all the *.cap and *.inf files you need to call win32 api functions FindFirstFile() and FindNextFile(). This would be very simple to do in a managed CLR program, but somewhat a mess in plain c++ programs.

vector<string> capList;
WIN32_FIND_DATA data;
HANDLE hFile = FindFirstFile("c:\mydir\*.cap", &data);
if( hFile != INVALID_HANDLE_VALUE)
{
    do {
         if( strcmp(data.cFileNam,".") != 0 && strcmp(data.cFileNam,"..") != 0 && !(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
          capList.push_back(  data.cFileNam );
   } while( FindNextFile(hFile, &data) != ERROR_NO_MORE_FILES);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

It would be much simpler to read the file an entire line at a time, then search the line for <order> tags

string line;
size_t pos;
while( getline(fin, line) )
{
    if( (pos = line.find("<order>")) != string::npos )
    {
          string name = line.substr( pos+1 );
          pos = name.find("</order>");
          name = name.substr(0, pos);
          // now you have the text between <code> and </code> tags
    }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

All those links are already available in your control panel.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

It would be helpful if you posted the contents of that *.cap file. The code you have posted is a bit confusing to follow, so knowing the file contents will help us help you.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

does sum1 make a solution for it..???
i m wrkin on DevC++

No. We do not write your program for you, but we will help you write it yourself. Show us some effort, such as post the code you have tried, and ask specific questions about your code. But do no expect anyone here to do your homework for you, that's your job, not ours.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If you want me disprove that, send me: $40 for rolling papers, $350 for an ounce, and $200 for microbrews.

And that would only prove I'm an idiot for doing that :)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>I can prove you wrong on that. - MosaicFuneral
Ok smartass, how can you prove that I don't know any programmers show smoke dope. And I'm not the only one who claims smoking pot and programming don't mix. Just read post #9 here -- the pot smokers flunked the programming class.

jephthah commented: you get 'em! :) +12
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

read this article Seems to me it would be a whole lot easier to use managed streams. Don't ask me how because I have not done it. Something like this article.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I was a coder for over 20 years and know no one who smoked marijuana. Tobacco yes, marijuana no. And it makes sense to me -- can't program when stoned or intoxicated.

MosaicFuneral commented: I can prove you wrong on that. -1
VernonDozier commented: Very true. +18
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

merged the two threads.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Another remark: in nearly all benchmarks, outputting an integer takes longer than a string.

That would be expected because it has to convert int to string before displaying it on the console screen.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

As you can see between posts #12 and #13 speed not only is compiler dependent but also machine dependent.

But I'm surprised to see that Code::Blocks using MinGW produces faster code than VC++ 2008.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

VC++ 2008 Express on Vista Home Premium with 5 gig RAM 2 Mz CPU, quad core.

Debug build

cout string : 19562 ms
printf string : 6474 ms
cout int : 24368 ms
printf int : 6833 ms
Press any key to continue . . .

Release build

cout string : 18720 ms
printf string : 6115 ms
cout int : 23166 ms
printf int : 6677 ms
Press any key to continue . . .

Code::Blocks V 8.02 release build

cout string : 10671 ms
printf string : 6146 ms
cout int : 11373 ms
printf int : 6645 ms

Process returned 0 (0x0) execution time : 34.873 s
Press any key to continue.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There is no point comparing the difference because such a comparison will be compiler dependent. How one compiler implements cout and printf might be different than how some other compiler implements them. One compiler might wrap cout with printf() while another compiler might wrap cout with win32 api function WriteFile() (in MS-Windows os), bypassing printf() altogether.

tux4life commented: Good point. +12
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

did you try my previous suggestion yet?