Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

algorith output confusion!---help?

mouse 163mouse 163 Member Posts: 14
the following program DOES NOT get errors. It compiles and runs, however
I am having the following problems:
problems:

1. i don't know how to access private data members...so I left everything public

2. i didn't include a constructor (why?) not sure don't really know how - i am still initializing in main.

3.
After I delete a seat when i go to print Nemptyseats or Printseat functions the numbered list is messed up...
also after I asssigned more than 3 seats the listing of the seats is messed up as well...the names are alphabetized fine but the seats go the same as the delete prob:
123245 etc...

for example: I assign seats 1 2and 3 then delete seat 2. when i print out my listing it will say "vacant Seat Numbers"; 3,2,5,6,7,8,9,10,11,12

the deleted seat is getting thown into places it shouldn't...(the SAME number of seats ahead as the seat number deleted). I'm trouble shooting the best I can, but am kinda stumped. here is the latest version of the code...
4 lastly...i'm not sure i shouldn't have private data members...
opinions on this for the new girl to the "Class"?
thank you all so much!

[url]http://micheles-myocardium.com/c++/HW4_2.txt[/url]

Comments

  • bad_coderbad_coder Member Posts: 17
    : the following program DOES NOT get errors. It compiles and runs, however
    : I am having the following problems:
    : problems:
    :
    : 1. i don't know how to access private data members...so I left everything public
    :
    : 2. i didn't include a constructor (why?) not sure don't really know how - i am still initializing in main.
    :
    : 3.
    : After I delete a seat when i go to print Nemptyseats or Printseat functions the numbered list is messed up...
    : also after I asssigned more than 3 seats the listing of the seats is messed up as well...the names are alphabetized fine but the seats go the same as the delete prob:
    : 123245 etc...
    :
    : for example: I assign seats 1 2and 3 then delete seat 2. when i print out my listing it will say "vacant Seat Numbers"; 3,2,5,6,7,8,9,10,11,12
    :
    : the deleted seat is getting thown into places it shouldn't...(the SAME number of seats ahead as the seat number deleted). I'm trouble shooting the best I can, but am kinda stumped. here is the latest version of the code...
    : 4 lastly...i'm not sure i shouldn't have private data members...
    : opinions on this for the new girl to the "Class"?
    : thank you all so much!
    :
    : [url]http://micheles-myocardium.com/c++/HW4_2.txt[/url]
    :


    I have 2 things that may help. I just took a quick glance at it.

    1.

    for (int i = 0; i < num; i++)
    for (int j = i+1; j < num; j++)
    {
    if (s[i].LastName[0] > s[j].LastName[0])
    {
    tempseat = s[i];
    s[i] = s[j];
    s[j] = tempseat;
    }//if

    }//for

    Here you should use curly braces. The above code is bad_code
    for (int i = 0; i < num; i++)
    {
    for (int j = i+1; j < num; j++)

    {
    if (s[i].LastName[0] > s[j].LastName[0])
    {
    tempseat = s[i];
    s[i] = s[j];
    s[j] = tempseat;
    }//if

    }//for
    }//for

    2. You have included the STL Class . Why don't you use it.

    instead of
    char LastName[20];
    char FirstName[20];

    do the following
    string LastName;
    string FirstName;

    And when you do the alpha. Try a sort algorithm in the STL library if you can otherwise you can do this:

    for (int i = 0; i < num; i++)
    {
    for (int j = i+1; j < num; j++)

    {
    if (s[i].LastName > s[j].LastName)
    {
    tempseat = s[i];
    s[i] = s[j];
    s[j] = tempseat;
    }//if

    }//for
    }//for

    I am not sure this will fix your problem but it may give you some new ideas

  • scuba22scuba22 Member Posts: 61
    thanks very much....the problem IS solved but it is always nice to have someone 'QA-BUG' your code...can't believe i missed that curlee bracket!
    for fun, where can i get info on using the algorith header file?
    someone else posted a sort/alpha fx unsing it but it included a bool function in the class, unsure how I could encorporate it...

    ie
    [code]
    void alpha(Seat s[])

    {

    sort(s, s + MAX_ITEMS); //uses the algorith header
    [code]


    the above doesn't work unless this is the class or seat declaration :
    [code]
    bool operator < (const Seat &s)
    {

    if(strcmp(LastName, s.LastName) < 0)
    return true;
    else
    return false;
    }
    ;};
    [/code]


    (why?)
    I'd love to read about it's uses and inclusions





    : : the following program DOES NOT get errors. It compiles and runs, however
    : : I am having the following problems:
    : : problems:
    : :
    : : 1. i don't know how to access private data members...so I left everything public
    : :
    : : 2. i didn't include a constructor (why?) not sure don't really know how - i am still initializing in main.
    : :
    : : 3.
    : : After I delete a seat when i go to print Nemptyseats or Printseat functions the numbered list is messed up...
    : : also after I asssigned more than 3 seats the listing of the seats is messed up as well...the names are alphabetized fine but the seats go the same as the delete prob:
    : : 123245 etc...
    : :
    : : for example: I assign seats 1 2and 3 then delete seat 2. when i print out my listing it will say "vacant Seat Numbers"; 3,2,5,6,7,8,9,10,11,12
    : :
    : : the deleted seat is getting thown into places it shouldn't...(the SAME number of seats ahead as the seat number deleted). I'm trouble shooting the best I can, but am kinda stumped. here is the latest version of the code...
    : : 4 lastly...i'm not sure i shouldn't have private data members...
    : : opinions on this for the new girl to the "Class"?
    : : thank you all so much!
    : :
    : : [url]http://micheles-myocardium.com/c++/HW4_2.txt[/url]
    : :
    :
    :
    : I have 2 things that may help. I just took a quick glance at it.
    :
    : 1.
    :
    : for (int i = 0; i < num; i++)
    : for (int j = i+1; j < num; j++)
    : {
    : if (s[i].LastName[0] > s[j].LastName[0])
    : {
    : tempseat = s[i];
    : s[i] = s[j];
    : s[j] = tempseat;
    : }//if
    :
    : }//for
    :
    : Here you should use curly braces. The above code is bad_code
    : for (int i = 0; i < num; i++)
    : {
    : for (int j = i+1; j < num; j++)
    :
    : {
    : if (s[i].LastName[0] > s[j].LastName[0])
    : {
    : tempseat = s[i];
    : s[i] = s[j];
    : s[j] = tempseat;
    : }//if
    :
    : }//for
    : }//for
    :
    : 2. You have included the STL Class . Why don't you use it.
    :
    : instead of
    : char LastName[20];
    : char FirstName[20];
    :
    : do the following
    : string LastName;
    : string FirstName;
    :
    : And when you do the alpha. Try a sort algorithm in the STL library if you can otherwise you can do this:
    :
    : for (int i = 0; i < num; i++)
    : {
    : for (int j = i+1; j < num; j++)
    :
    : {
    : if (s[i].LastName > s[j].LastName)
    : {
    : tempseat = s[i];
    : s[i] = s[j];
    : s[j] = tempseat;
    : }//if
    :
    : }//for
    : }//for
    :
    : I am not sure this will fix your problem but it may give you some new ideas
    :
    :

  • bad_coderbad_coder Member Posts: 17
    : thanks very much....the problem IS solved but it is always nice to have someone 'QA-BUG' your code...can't believe i missed that curlee bracket!
    : for fun, where can i get info on using the algorith header file?
    : someone else posted a sort/alpha fx unsing it but it included a bool function in the class, unsure how I could encorporate it...
    :
    : ie
    : [code]
    : void alpha(Seat s[])
    :
    : {
    :
    : sort(s, s + MAX_ITEMS); //uses the algorith header
    : [code]
    :
    :
    : the above doesn't work unless this is the class or seat declaration :
    : [code]
    : bool operator < (const Seat &s)
    : {
    :
    : if(strcmp(LastName, s.LastName) < 0)
    : return true;
    : else
    : return false;
    : }
    : ;};
    : [/code]
    :
    :
    : (why?)
    : I'd love to read about it's uses and inclusions
    :
    :
    :
    :
    :
    : : : the following program DOES NOT get errors. It compiles and runs, however
    : : : I am having the following problems:
    : : : problems:
    : : :
    : : : 1. i don't know how to access private data members...so I left everything public
    : : :
    : : : 2. i didn't include a constructor (why?) not sure don't really know how - i am still initializing in main.
    : : :
    : : : 3.
    : : : After I delete a seat when i go to print Nemptyseats or Printseat functions the numbered list is messed up...
    : : : also after I asssigned more than 3 seats the listing of the seats is messed up as well...the names are alphabetized fine but the seats go the same as the delete prob:
    : : : 123245 etc...
    : : :
    : : : for example: I assign seats 1 2and 3 then delete seat 2. when i print out my listing it will say "vacant Seat Numbers"; 3,2,5,6,7,8,9,10,11,12
    : : :
    : : : the deleted seat is getting thown into places it shouldn't...(the SAME number of seats ahead as the seat number deleted). I'm trouble shooting the best I can, but am kinda stumped. here is the latest version of the code...
    : : : 4 lastly...i'm not sure i shouldn't have private data members...
    : : : opinions on this for the new girl to the "Class"?
    : : : thank you all so much!
    : : :
    : : : [url]http://micheles-myocardium.com/c++/HW4_2.txt[/url]
    : : :
    : :
    : :
    : : I have 2 things that may help. I just took a quick glance at it.
    : :
    : : 1.
    : :
    : : for (int i = 0; i < num; i++)
    : : for (int j = i+1; j < num; j++)
    : : {
    : : if (s[i].LastName[0] > s[j].LastName[0])
    : : {
    : : tempseat = s[i];
    : : s[i] = s[j];
    : : s[j] = tempseat;
    : : }//if
    : :
    : : }//for
    : :
    : : Here you should use curly braces. The above code is bad_code
    : : for (int i = 0; i < num; i++)
    : : {
    : : for (int j = i+1; j < num; j++)
    : :
    : : {
    : : if (s[i].LastName[0] > s[j].LastName[0])
    : : {
    : : tempseat = s[i];
    : : s[i] = s[j];
    : : s[j] = tempseat;
    : : }//if
    : :
    : : }//for
    : : }//for
    : :
    : : 2. You have included the STL Class . Why don't you use it.
    : :
    : : instead of
    : : char LastName[20];
    : : char FirstName[20];
    : :
    : : do the following
    : : string LastName;
    : : string FirstName;
    : :
    : : And when you do the alpha. Try a sort algorithm in the STL library if you can otherwise you can do this:
    : :
    : : for (int i = 0; i < num; i++)
    : : {
    : : for (int j = i+1; j < num; j++)
    : :
    : : {
    : : if (s[i].LastName > s[j].LastName)
    : : {
    : : tempseat = s[i];
    : : s[i] = s[j];
    : : s[j] = tempseat;
    : : }//if
    : :
    : : }//for
    : : }//for
    : :
    : : I am not sure this will fix your problem but it may give you some new ideas
    : :
    : :
    :
    :

    You can get a book on STL. If you are good or perhaps if you don't need a lot of info, you can check the help with whatever compiler you are using. I think in the end you will want to get a book on STL. It is very usefull but it dosen't always make the most efficient use of memory as I have found out.



Sign In or Register to comment.