Clinton Portis 211 Practically a Posting Shark
Clinton Portis 211 Practically a Posting Shark

implysay odifymay ymay originalway odecay otay eetmay ouryay eedsnay:

is_ay(stirng& word)
{
     int pos = 0;
     string test[4] = {"ay", "ey", "ei", "ai"};     

     for(int i=0; i<4; i++)
     {
          if(word.rfind(test[i] == word.size() - 2))
          {
               return true;
          }
     } 

     return false;
}
Clinton Portis 211 Practically a Posting Shark

Iway agreeway ithway altPWay aboutway oundsay etectionday ithinway hetay copesay ofway ouryay sc100cay lasscay. Inway orderway otay akemay ouryay instructorway appyhay houghtay, e'llway avehay otay roceedpay underway hetay assumptionway hattay anyway ordway endingway inway "ay" illway esultray inway anway "ay" oundsay. ithWay histay inway indmay, Iway resentpay otay e'yay hetay ollwingfay odecay:

bool is_ay(string& word)
{
     int pos = word.rfind("ay");
    
     return (pos == word.size()- 2);
}

ownay ouya ancay esttay ordsway orfay hetay "ay" oundsay, if(is_ay(word)) hentay eavelay itway aloneway.

jonsca commented: Next stop, a compiler that understands PL! +5
Clinton Portis 211 Practically a Posting Shark

Although I am by no means an algorithms expert, I'll throw in my 2 cents anyway.. maybe I'll get lucky.

In my opinion, I believe there may be a scope issue of the variables declared in line #5. The vectors are pass by value into perhaps a huge stack of numerous calls to the mergeSort() function. The final resulting exit case has nothing to return and thusly destroys the variables upon function completion.

I believe the u & v vectors should be declared outside of the scope of the function (or passed in as an argument if your function and returned, if your function was not void) This will allow the subsequent recursive calls to mergeSort() to directly affect the vectors in memory... because when your recursive functions calls eventually resolve to the exit case (line #3), it's just a merged u or v vector.. which was destroyed by the previous function call, and will be destroyed again at exit case function completion.

**Afterthought: maybe you could just try passing the u & v vectors in by reference in line #5 and see if that works

Clinton Portis 211 Practically a Posting Shark

Whenever you want white-space delimited data extraction from file, simply use ifstream's >> insertion operator... perfect for what you are trying to do:

while(infile >> temp)
{
     myvec.push_back(temp);

     infile >> temp;
     myvec2.push_back(temp);

     infile >> temp;
     myvec3.push_back(temp);
}
Clinton Portis 211 Practically a Posting Shark

Just looking briefly over your code... there is one consideration that deserves special attention whenever you have a deque() type function; you should make special accomidations to ensure you will never dereferrence a NULL pointer, which I think may be the case here.

Clinton Portis 211 Practically a Posting Shark

I'm still not giving out code until you post in pig latin. In fact, I propose that for the rest of this thread, everyone post in pig latin.

Clinton Portis 211 Practically a Posting Shark

I just checked the documentation for the vector class and it appears that <vector> does not offer an overloaded += operator. It seems that only the = assignment and [ ] subscript operators are offered.

It looks as though you are trying to push char type variables into your vector. Probably the common vector class member function you will use is push_back()

for(char c = '1'; c < '5'; c++)
{
     //This is how you put stuff in a vector
     something.push_back(c);
}

The vector class does offer a [ ]subscript operator which we can use to access the individual elements of the vector array:

for(unsigned int i=0, size=something.size(); i<size; i++)
{
     //whatever this function does, we will iterate through all elements of the something vector and pass each char into the function
     //Your function requires a char pointer
     somefunction(&something[i], i);
}
Clinton Portis 211 Practically a Posting Shark

I'll make you a deal... you translate and repost your request for help into pig latin.. and I'll throw some quality c++ code your way.

SgtMe commented: LOL :L +3
Clinton Portis 211 Practically a Posting Shark

problem:
you will always get zero because of line #6; you are exiting your function based soley on string sizes...


answer:
one little tip on recursion that i found out on my own... Generally, for recursive functions, the return value of your function should also be a function parameter. any function that deviates from this will lose it's recursive properties.

currently, your function does not meet this little rule, because it returns an int, but only has string type args.

your function returns an int (the number of substrings found in a string). we should also pass this value into the function as an argument. we can use this value to 'keep track' of how many substrings we've attempted to identify. Now we can establish a clear exit case; In Pseudocode: if(number of attempts >= number of substrings) then return number of substrings found.

Clinton Portis 211 Practically a Posting Shark

suggestion:

//function prototype
string unique_chars(string line, int index = 0);
//function definition
string unique_chars(string line, int index)
{
     int pos = 0;
     string temp;
     
     //recusive exit case
     if(index == line.size())
     {
           return line;
     }

     pos = line.find(line[index], index+1)

     //if no other matching characters in string, move on to the next character
     if(pos == string::npos)
     { 
          return unique_chars(line, index++);
     }
     
     //erase a matching character, then move on to the next character
     else
     {
          line.erase(line.begin()+ pos);
          return unique_chars(line, index++);
     }
}

This is untested code. I just made it up. I don't know if this compiles without error. But you have to admit it's awesome help even if it doesn't work. It's sole intention is to give you an idea.. steer ye' in the right direction. What I would like to see from you now is a show of effort on your part.

Here is a decent youtube on "intro to c++ recursion": http://www.youtube.com/watch?v=kwFMkruKI8Y&feature=related

Here is a list of string class member functions: http://www.cplusplus.com/reference/string/string/

Clinton Portis 211 Practically a Posting Shark

you have to #inlcude<cstdlib> if you want to use exit()

your compiler error is pretty straight forward. it's telling you that you are attempting to use variable(s) that have not been declared. In essence, your compiler doesn't know how to use 'price' or 'percentage' because you have not declared them.

To remedy your error(s), simply declare 'price' and 'percentage' as double types, and initialize them to 0.

double price = 0.0,
       percentage = 0.0;

Thou shalt declare all variables and initialize them before you can use them.


...can anyone else help me?

Godfather voice: I try to help you and this is what I get.. what did I do to deserve this..??

Clinton Portis 211 Practically a Posting Shark

I specifically just gave you code for 1 function.

You have to implement the code I gave you in proper c++ format, which for your project would probably be:

#headers
namespaces;

function prototypes;

main() function

function definitions


the function prototype for the one I gave you would be

double markup(double, double);

the function definion is

//Function will return a marked-up retail price
double markup(double price, double percent)
{
     //error check 
     if(price <= 0 || percent <= 0)
     {
          cout << "\n\aDomain error:  price and/or percent less than zero."
          exit(1);
     }

     //5th grade math
     price *= percent / 100 + 1;
     return price;
}
Clinton Portis 211 Practically a Posting Shark

Let's say you want a 50 percent markup... this is how ye' would do the math:

price = $2
markup = 50%

So you take 50 divided by 100 which equals .5, but since you are adding a percentage to the original price, it has to be 2 * 1.5 which would be $3.

So yes, I made an error, it should be +1 instead of +10. Good call.

Clinton Portis 211 Practically a Posting Shark

Write a program that asks the user to enter and item's wholesale cost and its markup percentage. It should then display the item's retail price.

Based on the above requiremet, it would make sense to me to have a funtion that takes two arguments: a wholesale cost and a markup percentage...

//Function will return a marked-up retail price
double markup(double price, double percent)
{
     //error check 
     if(price <= 0 || percent <= 0)
     {
          cout << "\n\aDomain error:  price and/or percent less than zero."
          exit(1);
     }

     //5th grade math
     price *= percent / 100 + 10;
     return price;
}

//Now you can do some of this funky stuff
cout << "A " << percent << "% markup for an item costing " << price << " is " << markup(price, percent);

The above code is logical and makes sense. No tricks. No special techniques. Straight-up basic c++ function. Keep it simple.

Clinton Portis 211 Practically a Posting Shark

Here is how you can compare an element, against every other element.. while we are at it, we'll erase any matching words in order to get a good word count:

string temp;

//This array will contain single occurances of words from wordsM[ ]
string unique_words[length];

//This array will serve as a counter, and will directly corrospond to unique_words[ ]
int unique_counter[length];

//Array initialization (clean the trash out of the array)
for(int i=0; i<length; i++)
{
     unique_counter[i] = 0;
}

//Pick a word
for(int i=0; i<length; i++)
{
     temp = wordsM[i];

     //Compare it with every other word
     for(int j=0; j<length; j++)
     {
          if(temp.compare(wordsM[j]) && !wordsM[j].empty())
          {
               unique_words[i] = wordsM[j];
               wordsM[j].clear();
               unique_counter[i]++;
          }
     }
}

Now you have a counter that corrosponds to an array of individual words. I gave you a really good idea of how to get this done.. see if you can make it work in your code.

Clinton Portis 211 Practically a Posting Shark

if you were my compiler, you'd know that i'm far from perfect..

Clinton Portis 211 Practically a Posting Shark

there seems to be one major problem with your code....


*** IT'S IN C ***

(try posting your problem on the C forum, not c++)

Clinton Portis 211 Practically a Posting Shark

if you hang with me, not only can you get your cup of coffee, but you'll also have someone to talk c++ with.. how easy is it to find a guy like that these days... i'm like the perfect guy :P

Clinton Portis 211 Practically a Posting Shark

why am i a creeper? what's so bad about trying to move on after a failed marriage.

Clinton Portis 211 Practically a Posting Shark

Thank you Christina.

It's nice to see someone who can really pick up on this stuff and run with it. You actually required very little help on this project.

With that said, we should hook up sometime. Maybe talk some c++ over dinner. Add Dave Weirich on facebook. I am currently using a pic of Jim Mora as my profile pic.

jonsca commented: LOL +5
Clinton Portis 211 Practically a Posting Shark

You declared a single pointer, to a single SavingsAccount object.

I believe what ye' wanted to do is to create a pointer to an array of SavingsAccount pointers; each element containing the address to a new SavingsAccount object:

//Pointer-to-pointer (or an array of pointers)
SavingsAccount** accounts = new SavingsAccount*[100];

//Initialize the accounts[ ] array with new SavingsAccount objects:
for(int i=0; i<100; i++)
{
     accounts[i] = new SavingsAccount;  //you could call an overloaded constructor here and supply args if ye' wish
}
Clinton Portis 211 Practically a Posting Shark

I have a feeling you just did this guy's homework.. :(

Clinton Portis 211 Practically a Posting Shark

1) create an array of struct objects that will hold ID and GPA information:

struct Student
{
     int id;
     double gpa;
     
}data[50];

2) Now you can fill up your array of structs:

int size=0;
while(infile >> data[size].id)
{
     infile >> data[size].gpa;
     size++;
}

3) Now ye' can sort:

#include<algorithm>

sort(&data[0].gpa, &data[size].gpa);

4) Display your sorted data with great ease:

cout << "ID:      GPA:";
for(int i=0; i<size; i++)
{
     cout << data[i].id << "      " << data[i].gpa << endl;
}
Clinton Portis 211 Practically a Posting Shark

I'd probably do somethin' like this here:

#include<iomanip>

int spaces = 0;

for(int i=30; i>0; i--)
{
     cout << setw(spaces);

     for(int j=0; j<i; j++)
     { 
          cout << '*' << endl;
     {

     spaces++;
}

spaces -= 2;

for(int i=2; i<30; i++)
{
     cout << setw(spaces);

     for(int j=0; j<i; j++)
     { 
          cout << '*' << endl;
     {

     spaces--;
}
Clinton Portis 211 Practically a Posting Shark

I would suspect the letterDisplay cstring doesn't have anything in it. I think I can fix your problem without even having to double check your 'int to char' offset. Try this:

for(int i=0; i<26; i++)   
{
     letterDisplay[i] = 'A' + i;
}

letterDisplay[26] = '\0';
Clinton Portis 211 Practically a Posting Shark

Could you provide a link to the next page so I can see figure 10.6, an example of how to code a scan line.. the 'next' link doesn't work (none of the links work for me)

Clinton Portis 211 Practically a Posting Shark

the URL for your huffman algorithm is not working.

Clinton Portis 211 Practically a Posting Shark

My suggestions:

You stated that you need to distribute hands to each player. I would recommend removing cards from deck() and pushing them into arrays assigned to each player; each array containing 5 Card objects.

In order to determine what hand someone has, I would recommend making a function for every possible winning hand available in poker. Each function could be prototyped to return a boolean value; true if their hand qualified as a winner for that particular winning scenario and false if not. It is possible that a hand could qualifiy for several winning scenarios, i.e. a full house will also return true for 2 of a kind, and 3 of a kind. To account for this, I would progress through the functions in descending order (royal flush to 1pair).

Special case scenrios: if all players return false for all winning functions, then you will have to obtain a 'high card' from each hand in order to determine a winner. Furthermore, you may have use a 'high card' in order to break a tie between two winning hands (i.e. flush vs. flush). Lastly, you will have to handle the rare case of a tie, in which case the pot is split.

int winner(Card player_hand[5])
{
      //Exctract and save 'high card'
      //you can handle this here, or in another dedicated funtion for obtaining high card
      ::high_card = get_high_card(player_hand[]);

     //Make winning hand determinations:
     if(royal_flush(player_hand))
     {
          return 9;
     }
     else if(striaght_flush(player_hand))
     {
          return 8;
     }
     else if(4_kind(player_hand))
     { …
Clinton Portis 211 Practically a Posting Shark

Serious error on line #70. You are attempting to perform an assignment operation instead of a boolean comparison. Since you are using cstrings, I would recommend use of the strcmp() function from <cstring>

#include<cstring>

for (int x = 0; x < size; x++)
{
     if (strcmp(names[x], "Jim") == 0)
     {
          cout << "You Found Him";
     }
     else              
     {
           cout << "You Did Not Find Him";
     }
}
Clinton Portis 211 Practically a Posting Shark

You got a few things to take care of which may or may not directly be related to your problem:

1. Please in the future, when you post on the forum, wrap your code with code tags.
2. void main() is bad, for many reasons. instead, use int main() which will return 0 upon successful termination of your program.
3. How did you even make it this far without deriving objects from the standard namespace? Try adding using namespace std; on the 3rd line of your code.
4. Format your code nicely. Just like you indent between paragraphs when you write a paper, you should indent blocks of similar code.

Clinton Portis 211 Practically a Posting Shark

I'm guessing there's some polymorphism going on and Employee is the base class. If so, I think it's OK.

Good call. This is where things get a little fuzzy to me.. I think there are some special rules to consider when using pointers to a base class object vs. child class objects..

Hopefully one of our c++ guru's will step in and clear things up.. need a refresher on pointer use during polymorphism.

Clinton Portis 211 Practically a Posting Shark

I believe you might have a problem with line #2.

I just looked over the vector class constructors.. the only constructor with a single parameter takes a "reference to a vector" argument. In your code, you are attempting to pass an integer, which will probably throw a "could not convert int to const vector<T,Allocator>&" type of error during compile.

I would recommend declaring the vector using it's default constructor. Since you will have an empty vector, you'll have to push_back( ) new Employee objects, which you can populate using employee class constructors.

Also... I notice that you have a vector of Employee* pointers..... but then you are attempting to assign new objects of type Manager, EntryLevelEmployee, SalesRep, and SalesManager... Unless there is some sort of type casting that I am not aware of.. an Employee* pointer can only hold an address to an Employee object... therefore, you can only populate a vector<Employee*> with new Employee's.

Correct me if I'm wrong, but the two major flaws i've seen in your code are an incorrect use of a vector constructor, and a fundamental misuse of pointers.

Clinton Portis 211 Practically a Posting Shark

This is actually a clear-cut easy to follow assignment. Your instructor did all the thinking for you. There is nothing left up to your imagination; all you have to do is follow directions.

class Queue
{
     public:

     Queue();
     Queue(int& q);
     void add(int);
     void take();

     private:

     int array[100];
};

You will probably end up adding more stuff to this class, like menu() and display() functions for example.. what I outlined for you is the bear minimum required of your assignment.

Clinton Portis 211 Practically a Posting Shark

yes.. by all means, please use string class objects. they are so much easier to use than strings.

Clinton Portis 211 Practically a Posting Shark

The errors are self explanitory. Your sortArray() and binarySearch() functions are prototyped to take an array of cstrings, and you are trying to pass in a string object.

couple of things to try:

1) see if you can pass in your string whilst calling the c_str() member

sortArray(name.c_str(), name.size());

this might not work because i don't think you can modify a string object via it's cstring pointer. the c_str() member is mainly used for backwards compatibility with the older style cstring functions, as you will see below:

2) what will work though, is make a copy of the string to a cstring:

#include<cstring>

char* word = new char[name.size()+1];

strcpy(word, name.c_str());

sortArray(word, strlen(word));

Now you can pass cstrings arguments into your functions, which are prototyped to accept cstrings (not strings class objects).

Clinton Portis 211 Practically a Posting Shark

There might be a better way to do this, but this is just my idea:

1. determine sign of the result
2. make all cstring'd numbers positive
3. big number always goes on top
4. smaller number always goes on bottom
5. perform right-to-left subtraction just like you learned in 2nd grade.
6. apply proper sign to result

If anyone knows a better way to do this, let me know.

Clinton Portis 211 Practically a Posting Shark
for(int r=0, c=9; r<10; r++, c--)
{
     subtotal += array[r][c];
}

you should have got this on your own, or at least got really close.. or at least made an attempt.

Clinton Portis 211 Practically a Posting Shark

you could use a technique called 'pointer arithmetic' to access ye' array:

#include<cstring>

//dynamic array
char* array = new char[11];

//breaking into the ol' C library to handle the array cstring
strcpy(array, "I Love C++");

//display the array using pointer arithmetic method:
while(array != NULL)
{
     cout << *array;
     array++;
}

No subscript operator needed; you are using pointers and pointer arithmetic to move about in system memory.

Clinton Portis 211 Practically a Posting Shark

The first one is easy. Just make a function that strips off the ends of a string:

void stripper(string word)
{
     int size = word.size();     

     //Let the strippin' begin!
     if(size > 2)
     {
          cout << word[0] << word[size] << endl;
          word = word.substr(1, size-2);
          
          //enjoy recursive deliciousness
          stripper(word);
     }
     else if(size == 2)
     {
          cout << word[0] << word[1] << endl;
          return;
     }
     else 
     {
          cout << word[0] << endl;
          return;
     }
}

I'll leave ye' with this for now.. show us what ye' can do with problem #2.

Clinton Portis 211 Practically a Posting Shark

the sort( ) function by default performs a strict < comparison between elements. Unfortunately, you can't compare strings with the < operator. Luckily, there is an overloaded version of sort( ) that will accept a user-defined comparison function. You can design a function that will return true if string1 < string2:

//This function will be used to perform an "alphabetical order comparison" between strings

bool string_compare(string word1, string word2)
{   
     #include<cctype>
          
     int size = 0;

     //determine shortest word length
     if(word1.size() < word2.size())
     {
          size = word1.size();
     }
     else
     {
          size = word2.size();
     }
 
     //perform char-by-char comparison
     //return true if words are in alphabetical order
     for(int i=0; i<size; i++)
     {
          if(toupper(word1[i]) < toupper(word2[i]))
          {
               return true;
          }
          else if(toupper(word[i]) > toupper(word2[i]))
          {
               return false;
          }
     }

     //handle two matching words, different length
     //example:  work and working
     if(word1.size() < word2.size())
     {
          return true;
     }
     else
     {
          return false;
     }
}

Now you can use sort( ) to compare strings:

#include<algorithm>

//sort strings to alphebetical order
sort(sV.begin(), sV.end(), string_compare);

Be sure to read up on how to use sort: http://www.cplusplus.com/reference/algorithm/sort/

Clinton Portis 211 Practically a Posting Shark

teehee. that recursion joke never gets old. i am giggling on the inside.

Clinton Portis 211 Practically a Posting Shark

if you want to pass a vector into a function, just prototype the parameter list just like you would any other data type; 'type' then 'var name':

//Pass by reference in order to directly modify the vector
void bubble_sort(vector<string>& file)
{
     bool is_swap = true;
     
     for(int i=0, size=file.size(); i<size && is_swap; i++)
     {
          is_swap = false;         

          for(int j=0; j<size-1; j++)
          {
               if(file[j] > file[j+1])
               {
                     swap(file[j], file[j+1]);
                     bool is_swap = true;
               }     
          }
     }
}
Clinton Portis 211 Practically a Posting Shark

The erase() function requires an iterator type argument, but i was hoping to get away with an array referrence.

Just out of curiosity, see if this works:

sV.erase(&sV[j]);

If not, this will work:

sV.erase(sV.begin() + j);
Clinton Portis 211 Practically a Posting Shark

I just solved this problem like a couple weeks ago. Check out this thread. I'm sure it will answer pretty much all your questions.

Give this a shot on your own and see how you do. Then, if you have anymore questions let us know.

Clinton Portis 211 Practically a Posting Shark

click here for a good example on recursion.

jonsca commented: Well played. +5
Clinton Portis 211 Practically a Posting Shark

The easiest way to read diagonally through your array would be to take the procedural approach and just write out each element of the 2d array that makes up the diagonal you want to add.

But it seems like you are looking for a better method that would involve less code, using a loop. When it comes to something like this, look for a pattern. Once you've discovered the pattern, you have to make it part of your loop condition.

For example, in your [10][10] array, you start out at [0][0], then [1][1], then [2][2], then [3][3] and so on.. so we have identified a pattern; from [0][0], each subsequent element of the diagonal is [row+1][col+1]. So we can make this part of our loop condition:

int subtotal = 0;

//one diagonal
for(int i=0, i<10, i++)
{
     //increment diagonally through the array
     subtotal += array[i][i];
}

To get the other diagonal, you would start at [0][10], then go to [1][9], then [2][8], then [3][7].. etc. Initialize the element indicies to 0 and 10 and handle them appropriately in your loop condition in such a manner that will result in a diagonal summation.

Clinton Portis 211 Practically a Posting Shark

my bad.. i forgot that erase() changes the size of the vector. this is actually one time where continuous checking of size(), although not optimal, is actually a good thing:

for(int i=0; i<sV.size(); i++)
{
     for(int j=0; j<sV.size(); j++)
     {
          if(sV[i].compare(sV[j]) == 0)
          {
               sV.erase(sV[j]);
          }
     }
}
Clinton Portis 211 Practically a Posting Shark
#include <iostream>
#include <iomanip>
#include <string>
// User-defined headers
#include "MediaCollection.h"

using namespace std;

// Global function definitions
void DisplayMenu();

int main()
{
     // Create my media collection
     MediaCollection MediaCollection;

     // Declare and initialize the variable used for the menu
     int menuChoice = 0;

     do{

          cout << endl << "Welcome to the " << MediaCollection.getCollectionName()
               << " Media Collection Menu. Select from the following choices:" 
               << endl << endl;

          // Display the menu of choices
          DisplayMenu();

          menuChoice = MediaCollection.MenuSelection();

          switch (menuChoice)
          {
               case  1:  MediaCollection.addMedia();
               break;
               case 12:  cout << "Terminating program... Have a great day!" << endl;
               break;
               default:  cout << "Invalid choice. Please select again.";
               break;
          }
     }while(menuChoice != 12);

     // Wait for user input before terminating program
     cin.get();

     return 0;
}

void main() is bad. why? it's non-compliant with the ISO c++ standard which states a program that terminates successfully must return 0 (EXIT_SUCCESS) to the operating system. why? so the operating system knows your program exited successfully. why? so your operating system doesn't have to generate and send a microsoft system error report for analysis when the operating system is fine and it's just your junkie code causing the problem.

Clinton Portis 211 Practically a Posting Shark

try 1-800-admin