William Hemsworth 1,339 Posting Virtuoso

Well, the void is there to make a function, to give more simple code. But to do it without the function is just as easy.

#include <iostream>
#include <string>
using namespace std;

int main()
{
  string name, surname;

  cout << "what is your name?" << endl;
  cin >> name >> surname;

  for (size_t i = name.length()-1; i; --i)
    if ( ispunct(name[i]) )
      name.erase( i );

  for (size_t i = surname.length()-1; i; --i)
    if ( ispunct(surname[i]) )
      surname.erase( i );

  cout << surname << ", " << name << endl;
}
William Hemsworth 1,339 Posting Virtuoso

Well, theres a few ways you can do this, one method which I would probably use would be to read in the names, and manually remove all punctuation. To do that, you simply loop through each character, check if it is punctuation, if so, then erase it like this:

#include <iostream>
#include <string>
using namespace std;

void removePunct(string &str) {
  for (size_t i = str.length() - 1; i; --i)
    if ( ispunct(str[i]) )
      str.erase( i );
}

int main()
{
  string name, surname;

  cout << "what is your name?" << endl;
  cin >> name >> surname;

  removePunct( name );
  removePunct( surname );

  cout << surname << ", " << name << endl;
}

Hope this helps.

William Hemsworth 1,339 Posting Virtuoso

>Providing your developing on a windows machine and want unportable code.
Fair enough ;)

#include <fstream>

int main()
{
  std::ifstream in( "source", std::ios::in );
  std::ofstream out( "target", std::ios::out );
  out << in.rdbuf();
}

Shortest portable way I can think of.

William Hemsworth 1,339 Posting Virtuoso

Or simply?

#include <windows.h>

int main()
{
  CopyFile( "source", "destination", true );
}
William Hemsworth 1,339 Posting Virtuoso

Do you not know how a search engine works?
Try typing in "how to create file in c++" (your original question without the typo),
and the first link has your answer.

William Hemsworth 1,339 Posting Virtuoso
William Hemsworth 1,339 Posting Virtuoso

Uhm... yes?

Do you think you will live past the age of 80? :P

William Hemsworth 1,339 Posting Virtuoso

Uhm, a piano, some new speakers, books, and clothes ;)

William Hemsworth 1,339 Posting Virtuoso

>Feel free to show me a better way that pauses the execution screen that pops up so that i can check for debugging purposes.

I just did :icon_confused:


>Site rules "CODE TAGS".

I see you decided to ignore that rule then?

William Hemsworth 1,339 Posting Virtuoso

Why people insist on using system("pause") I just don't know, especially when you can just use cin.get() or cin.ignore() If you insist on having a function thats identical, here you go ;) :

#include <iostream>
#include <windows.h>

void Pause(char *message = "Press any key to continue . . . ") {
  std::cout << message;
  HANDLE hStdin; 
  DWORD cNumRead; 
  INPUT_RECORD irInBuf[1]; 

  if ( HANDLE(hStdin = GetStdHandle( STD_INPUT_HANDLE )) == INVALID_HANDLE_VALUE )
    return;

  while ( true )  { 
    if (! ReadConsoleInput( hStdin, irInBuf, 1, &cNumRead) )
      return;

    for (DWORD i = 0; i < cNumRead; ++i)
      if ( irInBuf[i].EventType == KEY_EVENT && irInBuf[i].Event.KeyEvent.bKeyDown ) {
        std::cout << '\n';
        return;
      }
  }
}

But, just stick to cin.ignore() :icon_cool:

William Hemsworth 1,339 Posting Virtuoso

A friend sent me this quite some time ago, I liked it :icon_cheesygrin:

"Don't be reckless with other peoples heart's, and don't put up with people who are reckless with yours."
Thats some good advice.

William Hemsworth 1,339 Posting Virtuoso
William Hemsworth 1,339 Posting Virtuoso

>There's nothing inherently wrong with returning a duplicated copy of the string in allocated memory

I will agree with that to some extent :icon_wink: but only if the caller knows exactly what he's doing, as by doing this, it makes your code more prone to errors. I'm quite sure it's bad practice to do this as i've caused myself problems in the past with memory leaks. One of my most common mistakes was something similar to this:

char *SubString(char *str, int beg, int fin) {
  char *sub = (char*)malloc( fin - beg + 1 );
  // Rest of the code
  return sub;
}

int main() {
  // Oops
  someFunction( SubString("abcdef", 2, 4) );
}

It's better to be safe than sorry in my opinion :icon_cheesygrin:

William Hemsworth 1,339 Posting Virtuoso

return strdup(temp); Bad idea, you should never return a pointer to allocated dynamic memory, as you can quite easily forget to release that memory once you've done with it. The correct way to do this is to add a parameter containing a pointer to a buffer which you can write to.

Instead of this:

char *getline(FILE *input)
{
  char temp[MAX_LEN];
  fgets(temp,MAX_LEN,input);
  temp[strlen(temp)-1]='\0';
  return strdup(temp);
}

Try:

int getline(FILE *input, char *ptr, int destSize)
{
  char temp[MAX_LEN];
  /* wanted MAX_LEN of data, is there something to read? */
  if (fgets(temp, MAX_LEN, input) != NULL) {
    size_t net_len = strlen(temp) -1;

    /* trim the newline/return if it's there */
    if (temp[net_len] == '\n') {
      temp[net_len] = '\0';
    }
  } else {
    /* handle the error... */
  }
  strcpy_s(ptr, destSize, temp);
}

Hope this helps :icon_cheesygrin:

William Hemsworth 1,339 Posting Virtuoso

Learn to type, and read the rules. As you've given no information at all, the best I can do is assume this is what you need.

William Hemsworth 1,339 Posting Virtuoso

You can't do it the way your hoping to, but with some simple casting, it can be achived.

MyClass<char> mc1;
MyClass<int> mc2;
MyClass<float> mc3;

void *arr[3];
arr[0] = reinterpret_cast<void*>( &mc1 );
arr[1] = reinterpret_cast<void*>( &mc2 );
arr[2] = reinterpret_cast<void*>( &mc3 );

And, to then use an item from the array, you can do the following:

// void someFunction(MyClass<int>*)
someFunction( reinterpret_cast<MyClass<int>*>(arr[1]) );

Hope this helps :icon_lol:

William Hemsworth 1,339 Posting Virtuoso

... music is my life :*


^ Is new to daniweb

< Wants to go iceskating...

v Is hungry? :)

William Hemsworth 1,339 Posting Virtuoso

>what's the value of line_start that's on line 7 of the code you posted?
Uhm... look at the comment on line 8 ;)

William Hemsworth 1,339 Posting Virtuoso

To add a new line, you have to rewrite the whole file. There is no way to insert data into the middle of a file. Ancient Dragon made a snippet which does something similar, but instead of adding a line, it deletes one. If you study the code, i'm sure you could figure out how to make it add a line, instead of deleting one. http://www.daniweb.com/code/snippet777.html

William Hemsworth 1,339 Posting Virtuoso

Also, don't post multiple threads for a reply on the same problem.
Read the rules before posting again.

William Hemsworth 1,339 Posting Virtuoso

You haven't given enough detail in your post, and in future use code-tags.

>PLEASE.... I NEED URGENT HELP//////
No need for the upper-case characters, this may be urgent to you, but it's not for us.

Here is the code well formatted, with your error highlighted.

//The function starts here
void search() {
  ifstream o1;
  o1.open("file"[B],[/B] ios::binary); // Comma missing here

  int flag = 0;
  char name[10]; //SEARCH VALUE

  cout << "Enter the element's name to be searched...";
  cin >> name;

  while ( o1.read((char*)&object, sizeof(object)) ) {
    if ( strcmpi(name, object.retname()) == 0 ) {
      //object.retname() is a function defined in class to return element's name
      flag = 1;
      cout << "\nElement found..." << object.retname();
      break;
    }
  }

  if (flag == 0)
    cout << "\nElement not found...";

  o1.close();
}

You are using strcmpi to compare the strings, by using this it will check for an exact match. To do what you want, you will have to manually read the name, to only show elements which match the first few letters you typed in.

Hope this helps.

Salem commented: Good stuff +25
William Hemsworth 1,339 Posting Virtuoso

Depends on what your using... are you making a console application? Or are you trying to detect it within a windows application? If so, then look up the WM_MBUTTONUP message, or you could try using windows hooks. For a better answer, be more specific in your post.

William Hemsworth 1,339 Posting Virtuoso

It can be quite complex, but with the help of google, I managed to put this together as an example :)

#include <windows.h>
#include <iostream>
using namespace std;

void KeyEventProc(KEY_EVENT_RECORD); 

int main()  { 
  HANDLE hStdin; 
  DWORD cNumRead; 
  INPUT_RECORD irInBuf[128]; 

  hStdin = GetStdHandle( STD_INPUT_HANDLE ); 

  if ( hStdin == INVALID_HANDLE_VALUE ) 
    return 0;

  while ( true )  { 

    if (! ReadConsoleInput( hStdin, irInBuf, 128, &cNumRead) )
      return 0;

    for (DWORD i = 0; i < cNumRead; i++)  {
      if ( irInBuf[i].EventType == KEY_EVENT ) {
        KeyEventProc( irInBuf[i].Event.KeyEvent );
      }
    }
  } 

  return 0; 
}

void KeyEventProc(KEY_EVENT_RECORD ker) {
  if (ker.bKeyDown == false) { // Only if key is released
    switch ( ker.wVirtualKeyCode ) {
      case VK_LEFT:   cout << "Left\n";   break;
      case VK_RIGHT:  cout << "Right\n";  break;
      case VK_UP:     cout << "Up\n";     break;
      case VK_DOWN:   cout << "Down\n";   break;
    }
  }
}

Hope this helps.

William Hemsworth 1,339 Posting Virtuoso

>No child should be giftless on xmas day.
Except very naughty children :)

William Hemsworth 1,339 Posting Virtuoso

#include <windows.h> The start of a big fun windows application.. Woo!? ;)

William Hemsworth 1,339 Posting Virtuoso

Many here in our country have no believe in your god.

Let me fix that for you?

Many here in our country have no belief in your god.

:)

William Hemsworth 1,339 Posting Virtuoso

I prefer http://www.isketch.net/isketch.shtml, It is the ultimate game to play with some friends when your bored :) No password needed, just pick a username, and press logon, then choose a room.

William Hemsworth 1,339 Posting Virtuoso

Around 02-04 :)

William Hemsworth 1,339 Posting Virtuoso

Thats insane. It seems to me like the parents just want to screw up their childs life. Imagine going through highschool... being called Adolf Hitler, no chance of a girlfriend and very few friends - if any. In one of my classes, there is one person named James Bond, and you can noticeably see how it affects his life (not in a good way).

I'd definitely say this is child abuse :icon_rolleyes:

William Hemsworth 1,339 Posting Virtuoso

>pls?...
Pathetic.

>i really don't know what to do..
What don't you know how to do? All the information is right there, just do one step at a time and i'm sure you can manage it. When you get stuck on a specific problem, we will help. But don't expect anybody to do it for you.

>includes two functions named calcavg() and variance()

float calcavg() {
  // Code Here
}

float variance() {
  // Code Here
}

int main() {
  // Code Here
}

ect..

Getting the picture?

Salem commented: *agrees* +25
William Hemsworth 1,339 Posting Virtuoso

Sure, just don't add it to the resource file, and open the bitmap by its filename instead.

William Hemsworth 1,339 Posting Virtuoso

Nvm..

William Hemsworth 1,339 Posting Virtuoso

638

William Hemsworth 1,339 Posting Virtuoso

hehe :)

William Hemsworth 1,339 Posting Virtuoso

Ahh.. okay :)

William Hemsworth 1,339 Posting Virtuoso

1280 x 1024, and to make things worse.. it only occurs every once in a while :icon_rolleyes: It's definitely happened more than a few times for me, but.. its nothing I can't live with, refreshing the page 1-2 times usually does the trick. :)

William Hemsworth 1,339 Posting Virtuoso

Ahh?! WTH is this? ;)

William Hemsworth 1,339 Posting Virtuoso

Here is a snippet I found somewhere on the internet quite some time ago, with a little extra code added.
This code manually searches through every file on your C drive (which may take a while) and prints it to the console. With this you should be able to figure out how to search for a file quite easily.

#include <windows.h>
#include <iostream>
using namespace std;

void FindAllFiles(
      const char* searchThisDir,
      bool searchSubDirs,
      void (*UserFunc)(const char*))
{
  // What we need to search for files
  WIN32_FIND_DATA FindFileData = {0};
  HANDLE hFind = INVALID_HANDLE_VALUE;

  // Build the file search string
  char searchDir[2048] = {0};

  // If it already is a path that ends with \, only add the *
  if( searchThisDir[strlen(searchThisDir) - 1] == '\\' ) {
    _snprintf( searchDir, 2047, "%s*", searchThisDir );
  } else {
    _snprintf( searchDir, 2047, "%s\\*", searchThisDir );
  }

  // Find the first file in the directory.
  hFind = FindFirstFile( searchDir, &FindFileData );

  // If there is no file, return
  if ( hFind == INVALID_HANDLE_VALUE ) {
    return;
  }

  do {
    // If it's the "." directory, continue
    if ( strcmp(FindFileData.cFileName, ".") == 0 ) {
      continue;
    }

    // If it's the ".." directory, continue
    if ( strcmp(FindFileData.cFileName, "..") == 0 ) {
      continue;
    }		

    // If we find a directory
    if ( FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY ) {
      // If we want to search subdirectories
      if ( searchSubDirs ) {
        // Holds the new directory to search
        char searchDir2[2048] = {0};

        // If it already …
William Hemsworth 1,339 Posting Virtuoso

no-one wants to see you banned

I certainly don't want you banned, things around here would be so dull if it wasn't for you.. :)

William Hemsworth 1,339 Posting Virtuoso

>its my final priject of my first semester of BSCS
so, why are you asking us to do it for you?

William Hemsworth 1,339 Posting Virtuoso

Amazingly it compiles without error

Amazingly, this compiles without error (for me) :icon_eek:

So I'm not too surprised that his compiled x)

char ********************************************************************************
     ********************************************************************************
     ********************************************************************************
     ********************************************************************************
     ********************************************************************************
     ********************************************************************************
     ********************************************************************************
     ********************************************************************************
     ********************************************************************************
     ********************************************************************************
     ******************************************************************************** a;
William Hemsworth 1,339 Posting Virtuoso

You can't assign an array of characters like that, here is how you correct it.

#include <stdio.h>

struct data {
     char name[20];   
     char number[10]; 
};

typedef struct node {
     struct data list;
     struct node* next;
} Node;

Node *head = 0;
Node *tail = 0;
Node *New = 0;

void add(char* name, char* number) {
    New = (Node *) malloc(sizeof(Node));
    sprintf_s( New->list.name, 20, name );
    sprintf_s( New->list.number, 10, number );
    New->next = 0;

    if (tail != 0) {
        tail->next = New;
        tail = New;
    }
    else {
        tail = New;
        head = New;
    }
}

Hope this helps :)

edit: heh, just realized its already been marked as solved ;)

William Hemsworth 1,339 Posting Virtuoso

Chocolate Cookies! :icon_razz:

William Hemsworth 1,339 Posting Virtuoso

hehe x)

William Hemsworth 1,339 Posting Virtuoso

I'm not sure if I found this funny, or scary.. o_O
http://uk.youtube.com/watch?v=LWSjUe0FyxQ

William Hemsworth 1,339 Posting Virtuoso

Here is an example of how to use the GetOpenFileName function. Any decent Win32 tutorial on the net would should you how to do this, so in future, search the web.

#include <windows.h>
#include <string.h>
#include <iostream>
using namespace std; 

// Returns an empty string if dialog is canceled
string openfilename(char *filter = "All Files (*.*)\0*.*\0", HWND owner = NULL) {
  OPENFILENAME ofn;
  char fileName[MAX_PATH] = "";
  ZeroMemory(&ofn, sizeof(ofn));

  ofn.lStructSize = sizeof(OPENFILENAME);
  ofn.hwndOwner = owner;
  ofn.lpstrFilter = filter;
  ofn.lpstrFile = fileName;
  ofn.nMaxFile = MAX_PATH;
  ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
  ofn.lpstrDefExt = "";

  string fileNameStr;

  if ( GetOpenFileName(&ofn) )
    fileNameStr = fileName;

  return fileNameStr;
}

int main() {
  cout << openfilename().c_str();
  cin.ignore();
}

Either way, I hope this helps.

William Hemsworth 1,339 Posting Virtuoso

>parents?
Once again, an easy way :)

>William if you really want the ads to go away, use one of the ad blocking software suggested earlier.
>And then when you're old enough, you can give however much you want to help run DW.
I guess I will just do that. :-/

William Hemsworth 1,339 Posting Virtuoso

>I don't really have any easy legal way of donating
I don't think ill be air mailing a cheque ;)

>pay with paypal
You have to be 18 to own a paypal account, I'm 15.

William Hemsworth 1,339 Posting Virtuoso

Use a switch statement, it should look along the lines of..

switch ( z ) {
 case 1: // Add
   {
   }
   break;
 case 2: // Subtract
   {
   }
   break;
 case 3: // Multiply
   {
   }
   break;
 case 4: // Divide
   {
   }
   break;
}

You will have to understand and add most of the functionality yourself :)
For more help on this topic.

William Hemsworth 1,339 Posting Virtuoso

Uhm.. I don't really have any easy legal way of donating.. I'll just wait 3 years until im old enough :)
.. I would if I could :icon_wink: