C++ Palindrome program help

This is a discussion on C++ Palindrome program help within the C++ Programming forums, part of the General Programming Boards category; I was wondering if someone could run this into their compiler for me to see if it runs and works. ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    7

    C++ Palindrome program help

    I was wondering if someone could run this into their compiler for me to see if it runs and works. The lab I was working in closed before I could complete this, so I had to try and write the rest in word after I loaded it from my disk. I don't have Visual Studio or any compiler here at my home pc. Any assistance would be great, thanks.




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

    using namespace std;

    class Stack
    {
    int x;
    int OnTop;
    int size;
    char* S;

    public:
    Stack(int SizeOfStack = 100);
    ~Stack();
    bool StackIsFull(void);
    bool StackIsEmpty(void);
    void Push(char x);
    char Pop(void);
    }

    //contructor
    Stack();

    bool Stack::StackIsFull(void)
    {
    return OnTop >= size -1;
    }

    bool Stack::StackIsEmpty(void)
    {
    return OnTop <0;
    }

    void Stack::Push(char x)
    {
    S[++OnTop] = x;
    }

    char Stack::Pop(void)
    {
    return S[OnTop--];
    }

    //Main function

    int main()
    {
    //Declare variables
    HANDLE hInputFile;
    HANDLE hOutputFile;
    HANDLE hInputFileMap;
    HANDLE hOutputFileMap;
    PVOID pvInputFile;
    PVOID pvOutputFile;
    DWORD dwInputFileSize = 0;
    DWORD dwOutputFileSize = 0;
    DWORD dwErr;
    char inputFileName [255];
    char outputFileName[255];
    char cDrome[10];
    int iDrome;
    bool IsDrome;
    int i;

    cout<<"Enter origin path and file name: ";
    cin>>inputFileName;
    cout<<endl<<endl;
    cout<<"Enter destination path and file name: ";
    cin>>outputFileName;
    cout<<endl;

    //create handle for input file
    hInputFile = CreateFile(inputFilename, GENERIC_READ, 0, NULL, OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL, NULL);
    if (hInputFile == INVALID_HANDLE_VALUE)
    {
    dwErr = GetLastError();
    cout<<"Error number: ";
    cin>>dwErr;
    cout<<endl;
    cout<<"Input file could not be opened.";
    cout<<endl;
    cout<<endl;
    return(FALSE);
    }

    //create handle for input file mapping
    hInputFileMap = CreateFileMapping(hInputFile, NULL, PAGE_READONLY, 0, 0, NULL);
    if (hInputFileMap == NULL)
    {
    dwErr = GetLastError();
    cout<<"Error number: ";
    cin>>dwErr;
    cout<<endl;
    CloseHandle(hInputFile);
    return(FALSE);
    }

    //create mapped view of the input file
    pvInputFile = MapViewOfFile(hInputFileMap, FILE_MAP_READ, 0, 0, 0);
    if (pvInputFile == NULL)
    {
    dwErr = GetLastError();
    cout<<"Error number: ";
    cin>>dwErr;
    cout<<endl;
    cout<<"Could not map view of the input file";
    cout<<endl;
    CloseHandle(hInputFileMap);
    CloseHandle(hInputFile);
    return (FALSE);
    }

    //determine size of the input file and compute appropriate size for output
    dwInputFileSize = GetFileSize(hInputFile, NULL);

    //set string pointer to the first address of the input file
    PSTR pchANSI = (PSTR) pvInputFile;

    //determine the exact size needed for the output file
    for (i = 0; i < (int)dwInputFileSize; i++)
    {
    iDrome = (int)pchANSI[i];
    itoa(iDrome, cDrome, 10);
    dwOutputFileSize = dwOutputFileSize + strlen(cDrome) + 1;
    }

    //subtract one fm the size to eliminate the last space
    dwOutputFileSize = dwOutputFileSize - 1;

    //create handle to the output file
    hOutputFile = CreateFile(hOutputFilename, GENERIC_WRITE | GENERIC_READ, 0, NULL,
    CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
    if (hOutputFile == INVALID_HANDLE_VALUE)
    {
    dwErr = GetLastError();
    cout<<"Error number: ";
    cin>>dwErr;
    cout<<endl;
    cout<<"Output file could not be created.";
    cout<<endl;
    cout<<endl;
    UnmapViewOfFile(pvInputFile);
    CloseHandle(hInputFileMap);
    CloseHandle(hInputFile);
    return(FALSE);
    }

    //create handle to the output file mapping
    hOutputFileMap = CreateFileMapping(hOutputFilename,NULL, PAGE_READWRITE, 0,
    dwOutputFileSize, NULL);
    if (hOutputFileMap == NULL)
    {
    dwErr = GetLastError();
    cout<<"Error number: ";
    cin>>dwErr;
    cout<<endl;
    cout<<"Output file map could not be opened.";
    cout<<endl;
    cout<<endl;
    UnmapViewOfFile(pvInputFile);
    CloseHandle(hOutputFile);
    CloseHandle(hInputFileMap);
    CloseHandle(hInputFile);
    return(FALSE);
    }

    //map view of the output file
    pvOutputFile = MapViewOfFile(hOutputFileMap, FILE_MAP_WRITE, 0, 0, 0);
    if (pvOutputFile == NULL)
    {
    dwErr = GetLastError();
    cout<<"Error number: ";
    cin>>dwErr;
    cout<<endl;
    cout<<"Could not map view of the output file.";
    cout<<endl;
    cout<<endl;
    UnmapViewOfFile(pvInputFile);
    CloseHandle(hOutputFileMap);
    CloseHandle(hOutputFile);
    CloseHandle(hInputFileMap);
    CloseHandle(hInputFile);
    return(FALSE);
    }

    //set string pointer to the first address of the output file
    PSTR pchASCII = (PSTR) pvOutputFile;

    //determine if word is a palindrome
    bool IsDrome(char* c)
    {
    Stack S;
    for (int i = 0; x < strlen(c); i++) S.Push(c[i]);
    int i = 0;
    while (!S.StackIsEmpty())
    if (c[i] != S.Pop()) return false;
    return true;
    }
    }

  2. #2
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    please use code-tags
    and repost ur code
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    7

    C++ Palindrome program help please

    How do I use code tags?

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You start with [code], then write all your code. The tag'll preserve all your spacing and put it in an easier to read font.

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Then after your code you put [/code] and the formatting'll go back to normal. I would showed you in one post, but it would've been hard to get the code tags to not mess up the formatting.

    Demo:

    Code:
    Your code here
    
         cool, huh?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    can i do this in word or notepad...like i said...i don't have visual studio at home

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You can write the source code however you want, as long as it ends up in ASCII format with a .cpp extension.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    i think i did it right....i reposted the program just now.

  9. #9
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Reposted with Code Tags on here.

    Please post all replies there.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 11:58 PM
  3. Palindrome Program using Memory mapping
    By rrsanch in forum C++ Programming
    Replies: 1
    Last Post: 09-25-2004, 12:49 AM
  4. Palindrome program help please
    By hunter_04 in forum C++ Programming
    Replies: 7
    Last Post: 09-20-2004, 08:38 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21