assertion failures

This is a discussion on assertion failures within the C++ Programming forums, part of the General Programming Boards category; //main.cpp #include "drinksAtWork.h" void main(void) { drinksAtWork Daw; Daw.ChooseDrink(); //Xml(); } //drinksatwork.h #ifndef DRINKSATWORK_H #define DRINKSATWORK_H #include <stdlib.h> class drinksAtWork ...

  1. #1
    edwardtisdale
    Guest

    Post assertion failures

    //main.cpp
    #include "drinksAtWork.h"

    void main(void)
    {
    drinksAtWork Daw;
    Daw.ChooseDrink();
    //Xml();


    }
    //drinksatwork.h
    #ifndef DRINKSATWORK_H
    #define DRINKSATWORK_H
    #include <stdlib.h>

    class drinksAtWork
    {
    private:
    char *ch;
    public:
    drinksAtWork();
    void ChooseDrink();

    ~drinksAtWork();
    };



    #endif
    //drinksatwork.cpp
    #include "drinksAtWork.h"
    /*#include <file.h>*/
    #include <iostream.h>
    //#include <fstream.h>


    void drinksAtWork::ChooseDrink()
    {
    cout << ch << endl;
    cout << "Choose your can." << endl;

    /* FILE *fp;//write equivalent code using fstream*/
    cin >> ch;
    }
    drinksAtWork::drinksAtWork()
    {
    ch=new char;
    ch="ff";

    }
    drinksAtWork::~drinksAtWork()
    {
    delete ch;
    }


    at first compiled at linked but when data was entered at the command prompt I ran into an assertion error. I pasted the line of the location of the error in Edit-Breakpoints-Data (Tab)-Enter the expression to be evaluated with the not(!) sign in front of it but it would not be accepted to be evaluated. Now after having closed the compiler and reopened it at another time it has LINK : fatal error LNK1000: unknown error; consult documentation for technical support options. Someone help?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Code:
    ch=new char; 
    ch="ff";
    It seems here you are allocating space on the free store for a char, and then reallocating the pointer to the start of a string literal - "ff".

    now when you excecute

    Code:
    cin >> ch;
    You no longer have access to the char you dynamically created.....therefore your proggie is crashing,

    I dont know why you are setting the pointer to the start of "ff".....

    if you set it to a char like so;

    Code:
    *ch='f';
    .......it should work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 03:47 AM
  2. Assertion Error
    By tao in forum Windows Programming
    Replies: 7
    Last Post: 06-19-2006, 07:41 AM
  3. Assertion error when looking for window handle
    By axr0284 in forum Windows Programming
    Replies: 3
    Last Post: 02-03-2005, 03:11 PM
  4. Assertion Failure
    By drdroid in forum Windows Programming
    Replies: 22
    Last Post: 01-05-2003, 06:50 PM
  5. Assertion Failure
    By drdroid in forum Game Programming
    Replies: 9
    Last Post: 01-04-2003, 06:02 PM

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