he we go again

This is a discussion on he we go again within the C++ Programming forums, part of the General Programming Boards category; EDIT: this post is unclear...please skim it and move on to the next given by teacher......causes run time error...who would ...

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

    he we go again

    EDIT: this post is unclear...please skim it and move on to the next

    given by teacher......causes run time error...who would have guessed?......

    please help...

    Code:
    //....
    asset * * group = new asset * [count];
    //..........
    group[i] = new stock; break;  //stock inherits from asset
    //^^ times 3
    //.............
    for(int j = 0; j < count; j++)
    {
    	delete group[j];
    }
    delete [] group;
    Last edited by misplaced; 11-18-2004 at 03:32 PM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Uhh... This what you're looking for?
    Code:
    asset** group = new asset*[count];
    for(int i = 0; i != count; ++i)
       group[i] = new stock( ... whatever ... );
    
    ... Lalalala ...
    
    for(int j = 0; j != count; ++j)
       delete group[j];
    delete[] group;
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by Zach L.
    Uhh... This what you're looking for?
    Code:
    asset** group = new asset*[count];
    for(int i = 0; i != count; ++i)
       group[i] = new stock( ... whatever ... );
    
    ... Lalalala ...
    
    for(int j = 0; j != count; ++j)
       delete group[j];
    delete[] group;

    uhhh, no......

    this is what's given..
    Code:
    #include <fstream.h>
    
    #include "asset.h"
    #include "stock.h"
    #include "savings.h"
    #include "property.h"
    
    void main()
    {
    	int count;
    	cout << "How any assets do you wish to enter? ";
    	cin >> count;
    	asset * * group = new asset * [count];
    	
    	for(int i = 0; i < count; i++)
    	{
    		char choice;
    		cout << "What do you want s, v, or p? ";
    		cin >> choice;
    		switch(choice)
    		{
    			case 's': group[i] = new stock; break;
    			case 'p': group[i] = new property; break;
    			case 'v': group[i] = new savings; break;
    		}
    
    		group[i]->kinput();
    		group[i]->print(cout);
    	}
    
              for(int j = 0; j < count; j++)
    	{
    		delete group[j];
    	}
    	delete [] group;
    }
    i can't believe how bad of a teacher she is....it's horrible...i want my money back....hell, i should get paid....i'm constantly fixing her mistakes; in class, on projects, things she told other students....seriously...she-...ahhh!.....

    if she gives us code, it better damn well work...but it doesn't....it never does.....it's not like she does it as a learning experience....she just doesn't know anything about what she's "teaching".....

    if i didn't already know most of this stuff i would go to the school and demand my money back.....horrible!...absolutely horrible!
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    ...now that i look back at it i see how you misunderstood my question, zach....

    i was just trying to save some space.....

    p.s. mods-not bumping posts....(look at time)
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    It doesn't work right because there isn't a default case in the switch statement, so your pointer will be invalid when acessing kinput() and/or print().

    So...
    Code:
    void main()
    {
    .....................
            switch(choice)
            {
                case 's': group[i] = new stock; break;
                case 'p': group[i] = new property; break;
                case 'v': group[i] = new savings; break;
                default: group[i] = NULL; continue;
            }
    
            group[i]->kinput();
            group[i]->print(cout);
        }
    
        for(int j = 0; j < count; j++)
        {
            if(group[j])
                delete group[j];
        }
        delete [] group;
    }
    EDIT: I noticed now... Indeed an excelent teacher. void main() to power! Shoudn't there be a fflush(stdin);??
    Last edited by xErath; 11-17-2004 at 09:52 PM.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Yep... That'd do it.
    Also note that there are two problems in the first eight lines. I think you'll catch them quickly, though.

    *edit*
    A test to see if 'count' is a valid positive integer would not be a bad thing either.

    This teacher is rather questionnable.
    Last edited by Zach L.; 11-17-2004 at 09:49 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i'm being about as clear as my teacher is.......

    the problem is when it reaches the delete loop...

    if i comment out the loop the programs completes "successfully"...that is, it doesn't throw a run-time error....

    i did add the line: if(group[j])
    and i did change it to int main....oh yea...i also added the default case for safe measures (i was entering the correct values)...

    i just thought of something while typing this......what's the correct way to delete a string such as the following?
    char *name = new char[strlen("test") + 1];
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  8. #8
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    what's the correct way to delete a string such as the following?
    char *name = new char[strlen("test") + 1];
    Code:
    delete [] name;

  9. #9
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Consider that your teacher might deliberately give you code which requires some thinking on your part to get it working.

    That's what I'd do were I teaching class. Not always, but some of the time. I'd probably tell you though that there's something wrong so you'd not be as confused.

    Good way to get kids to think rather than copy answers verbatim from a source book, though it doesn't seem to work anymore with kids refusing to think and rather post their entire assignment on some website where they'll then shout foul when noone gives them a complete perfect solution in 10 seconds...

  10. #10
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    Well even if the teacher is giving "broken" code to let the students(not kids) learn something it should at least conform to the standard, come on void main? I can let old header files slide but not void main
    Last edited by prog-bman; 11-18-2004 at 12:55 AM.
    Woop?

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by jwenting
    Consider that your teacher might deliberately give you code which requires some thinking on your part to get it working.

    That's what I'd do were I teaching class. Not always, but some of the time. I'd probably tell you though that there's something wrong so you'd not be as confused.

    Good way to get kids to think rather than copy answers verbatim from a source book, though it doesn't seem to work anymore with kids refusing to think and rather post their entire assignment on some website where they'll then shout foul when noone gives them a complete perfect solution in 10 seconds...
    yes, i understand that broken code is a good learning experience but a. she should warn us and b. it's not intentional. she REALLY does not know what she is doing. i can honestly say that i think i know more about the language than she does. i am certainly no guru either. i'm not saying she is a bad teacher because i can't keep up with her and i'm frustrated. i'm saying it because i've been doing c++ longer than anyone else in my class and i understand c++ quite well and i'm just amazed at some of the things she will tell them....there's so many things she says that are blatantly wrong. i eventually just quit correcting her.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I copied and pasted the code, replaced the classes with dummies (A,B,C inherit from D), and ran it. It didn't crash on me...

    Take a look into the headers' code, the problem's probably there.

    As a side note, I don't see why you need fstream in there. If anything, you'd be needing iostream...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    case 's': group[i] = new stock; break;
    case 'p': group[i] = new property; break;
    case 'v': group[i] = new savings; break;

    WHAT???
    It should be a new asset (according to the type)

    Don't you get any warnings with this?

    We need more information on how those types are related to each other

  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    Cant you do that if the objects inherit from assets??
    this is a comment in the first code:
    stock inherits from asset

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > Cant you do that if the objects inherit from assets??
    Probably, but void main teachers do not inspire confidence that they've done it right....

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

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