Clinton Portis 211 Practically a Posting Shark

int types do not support floating point decimal. try using double types, and if you desire a set precision, you can go even further and use float's.

with your calculator application, it is possible to prompt the user for a desired precision, in which case you would use float types.

http://www.cplusplus.com/reference/iostream/manipulators/setprecision/

Clinton Portis 211 Practically a Posting Shark

so can only search for the IDS_STRING part. How can I achieve that?

try a little somethin' like this:

vector<string> text_file;

//load the vector........

//find substrings in your vector:
for(int i=0, size=text_file.size(); i<size; i++)
{
     if(text_file[i].substr("IDS_STRING") != string::npos)
     { 
          //found
     }
     else
     {
          //not found
     }
}

I'm basically trying to go through the file and see if each IDS_STRING number exists, so from 1 to 4000, and if it doesn't I want to write a new line in the file with the IDS_STRING number.

This isn't too terribly difficult; however, the data structure ye' have chosen (vector) is a poor container when it comes to insertion that does not occur at the very end. A better choice for your application would be the <list> class... which is very similar to <vector> but offers an efficient insert() function (<list> offers the efficiency of a double-linked list data structure, unlike the array type of <vector>) So, what you would want to do is to populate your list and sort it using the sort() function from <algorithm>. You would want to (and have to) use the overloaded version of sort() that allows you to pass in your own custom compare function. This will allow you the flexibility to parse the list elements and perform a strict < less than comparison between elements. Sort the list, perform insertions as desired, open a file to write to. (if file already exists, open using the ios::trunc flag to overwrite the …

Clinton Portis 211 Practically a Posting Shark

it's pretty easy to do.. just compare the number you with every occupied element in the array. if it is not in the array, add to array. else, try again.

Clinton Portis 211 Practically a Posting Shark

in order to avoid repetition, you will have to push skills you have selected into an array, and compare the contents against future randomly generated skills. if skill has been used, try again.

Clinton Portis 211 Practically a Posting Shark

with limited knowledge of your problem combined with other generalities (such as your 'for loop') one guess i would make is that your loop condition in line #12 seems suspicious. since array elements are zero based, it is somewhat uncommon to see size-1 as a loop condition. this could cause you to examine one less element in your array and therefore return results less than what you would expect.

Clinton Portis 211 Practically a Posting Shark

one thing is ye' need to call srand() somewhere in order to seed ye' random number generator.

I would load your entire text file into a data structure, line by line, and then get lines out of the data structure at random.

vector<string> text_file;
int line_counter = 1;
string line;
srand(time(NULL));

//Load the text file
while(getline(infile, line))
{
     text_file.push_back(line);
     line_counter++;
}

//Get a random line
cout << text_file[rand()%line_counter];
Clinton Portis 211 Practically a Posting Shark

you need to make sure your header and function definitions are added to your project.. in dev c++ they will appear in the project window on the left side.

Clinton Portis 211 Practically a Posting Shark

try this.

#include<cstring>

char*  y = '\0';
char temp1[100];
char temp2[100];

for (int i=0; i<length-1; i++)
{
     Search(RecordArray,temp1,n,i);
     Search(RecordArray,temp2,n,i+1);  
     
     if(strcmp(temp1, temp2)> 0)
     {
          y = &RecordArray[i][254];
          RecordArray[i][254] = RecordArray[i+1];
          RecordArray[i+1][254] = *y;
     }
}

strcmp() will only give ye' the results you want if both arguments are of equal case (i.e. all upper or all lower case).

also, I am assuming you made RecordArray[ ][256], which means you lose one element for being zero based, and another element for null termination.

Clinton Portis 211 Practically a Posting Shark

you just can't arbitrarily assign a const char cstring to a buffer. it physically has to be copied element by element. the <cstring> library has everything you need to make cstring assignments:

#include <cstring>

char* lock = new char[501];

char ret[501] =  "this is a fine program";

strcpy(lock, ret);

the low-level power of c is cool, but when you take on the responsibility for allocating your own blocks of memory, you need some tools to help with handling character arrays (in this case, the tools are in the <cstring> library).

in contrast, a more object oriented approach would be to use the <string> class, which does offer an overloaded = assignment operator that would allow ye' to assign a string literal to a string class object.

Clinton Portis 211 Practically a Posting Shark

ye problem is right here:

if(x = i)

you seem to be an intelligent individual. take this opportunity to fix ye' error.

Clinton Portis 211 Practically a Posting Shark

I am having a tough time figuring out how to first approach this without confusing myself.

1. create an ifstream object
2. open your master file.
3. check for errors.
4. load ye' master file into a vector< > of structs.
5. close the ifstream object

6. create another ifstream object.
7. open your transaction file.
8. check for errors.
9. load ye' transaction file into a deque< > of structs.
10. close the ifstream object.

11. perform ye' calculations via comparison of the master file array and the transaction array. edit the transaction array as necessary.

12. create an ofstream object
13. open the transaction file (using the ios::trunc flag to over-write existing text)
14. write the newly edited contents of the transaction deque< > to the transaction file.
15. close the ofstream object.

ye' are done.

I will offer code for any 3 of the above given steps of your choice, free of charge. Choose wisely.

Clinton Portis 211 Practically a Posting Shark

I've dabbled in winsock a bit. So i'll throw in me' two cents:

I believe you should have a couple of struct attributes that include the 'type' of struct, and perhaps also the 'size' of the struct. These should probably be the first two things ye' test for as a client. Then ye' can identify and handle the type of data ye' are receiving.

there seems to be a common theme among win32 and winsock ADT's in that they usually contain a 'header' of some sort, which tells you everything you need to know about the struct, before you even begin to dig into of the contents of the struct. Try employing a similar strategy in the design your ADT's.

extra bonus
(also, i believe the size of many Winsock structs are 'padded' with useless attributes to the nearest power of 2 (128, 256, 512, 1024, etc) for sending and receiving efficiency. Win32 ADT's employ the same strategy for memory management efficiency.)

Clinton Portis 211 Practically a Posting Shark

Hola chica de newbiliciousness

after briefly lookin' at ye' code, try this:

//instead of
}while(choice != 'N');

//try
}while(choice != 'n' || choice != 'N');
//and
}while(choice != 'z' || choice != 'Z');

//cool alternatives
}while(toupper(choice) != 'N');
//or
}while(tolower(choice) != 'z');
Clinton Portis 211 Practically a Posting Shark

whenver ye' mix formatted and unformatted types o' input, ye' are acksin' for trouble. try cin.ignore() after line #5.

flush your buffer.

Clinton Portis 211 Practically a Posting Shark

try this:

int row = 0;
float data = 0.00;
vector<vector<float> > initMatrix(5, vector<float>(cols));

while(myfile)
{
     // --Handle 1D element allocation--
     //Resize every 5 loop iterations (why not? it's more efficient than re-sizing every time)
     //This is make sure you have a 1D element allocated in which to 'push_back()' to

     // **Make sure the initMatrix vector is initialized to at least 5 1D elements**
     if(!(row%5))
     {
          initMatrix.resize(row+5);
     }

     //since you have a set number of columns, we can make a known number of reads per line
     //this will assume three pieces of data per line (3 columns)
     for(int i=0; i<3; i++)
     {
          infile >> data;
          initMatrix[row].push_back(data);
     }

     row++;
}
Clinton Portis 211 Practically a Posting Shark

this is a c++ forum.

this is not a c forum.

Clinton Portis 211 Practically a Posting Shark

Now that i think about it, try

file >> *elements[i];
Clinton Portis 211 Practically a Posting Shark

what seems to be the problem senor

Clinton Portis 211 Practically a Posting Shark

should be:

//display
for(int i=0; i<number_of_elements; i++)
{
     cout << *element[i] << endl;
}
Clinton Portis 211 Practically a Posting Shark

if you ever take assembly language, you'll get a little insight on how arrrays and 2D arrays are really handled in memory. it does appear that line #31 looks like a 1d array; but i think it is ok. you could just as easily write it as: file[elements][0] but it will resolve to the same memory location as file[elements].

each element[] is just a pointer to the starting memory position of a 15 char buffer.


element[starting_memory_address][15_char_buffer]

so after your array is loaded, you could just easily do this and list all the elements of the periodic table:

for(int i=0; i<number_of_elements; i++)
{
     cout << element[i] << endl;
}

because you are outputting a cstring at it's starting memory position. output will stop at the null terminating character.

perhaps the problem is compiler dependent; try changing line #31 to element[0] and see if that helps.

Clinton Portis 211 Practically a Posting Shark

lines #20 and #21....

Elements = new char*[maxlines];//initialize pointer to pointer
symbol = new char*[maxlines];

also, you are not allowing space for a null terminating character when you declare symbol = new char[2] because periodic symbols are usually two characters.. leaving no space for a null terminator in your c-string. it should at minimum be symbol = new char[3].

Clinton Portis 211 Practically a Posting Shark

in line #21, are you reading in 'maxlines' every loop iteration? is this necessary? also, lines #22 and #23 you are re-allocating memory for 'element' and 'symbol' each loop iteration. Instead, try allocating the memory once, when the variable is declared. Then, every time you make a read operation, store the results into another container, perhaps a vector<char*>.... or even declare a 2D array:

//2D dynamic array of character arrays
//Element[number_of_elements][longest_word_length]

//Initialize a 'pointer to a pointer'
char** element = new char*[number_of_elements];

//allocate memory
for(int i=0; i<number_of_elements; i++)
{
     element[i] = new char[longest_word_length];
}

//read in from text file
int counter = 0;
while(infile)
{
     infile >> element[counter];
     counter++;
}

Make sure the second dimension of the array can accommodate the longest word + 1 null terminating character. you may even consider and extra amount of 'padding' just to ensure there are no problems exceeding the bounds of your arrays.

also, i would recommend using string class objects in order to avoid having to do all this memory allocation crap.

Clinton Portis 211 Practically a Posting Shark

i'm lost.

Clinton Portis 211 Practically a Posting Shark

Ye' never state what ye' problems is, but i'll guess it has something to do with float precision. there are a couple o' ways to set precision:

cout << setprecision(2);

//  or 

cout.setf(ios::fixed);
cout.setf(ios::showbase);
cout.precision(2);
Clinton Portis 211 Practically a Posting Shark

try this:

#include <windows.h>

//Declare a 'handle' type object
HANDLE console_handle;

//Get the 'handle' of the console window
console_handle = GetStdHandle(STD_OUTPUT_HANDLE);

//Change the title bar text
SetWindowText(console_handle, "I want to be the very best.");

Here is a reference for the SetWindowText() function.

Here is a reference for using basic windows functions in the DOS environment.

Clinton Portis 211 Practically a Posting Shark

String is a class object. It contains it's own member variables and functions. Some common (and very handy) string class include (but not limited to) size(), length(), empty(), clear(), compare() and c_str(). String class objects also have overloaded operators, such as [], (), += and = .

Cstrings, or 'character arrays' are basically remnants of the ol' days of C. They are not nearly used as often as string class objects; however, string class objects are still backwards compatible with cstrings via use of the c_str() member.

The <cstring> library also has a whole bunch of functions that can help you use character arrays more efficiently, but they will never be as robust as a <string> object.

Sometimes you are forced to use cstrings, when you'd really want to use strings. For example, if your program handles command-line arguments, you'd have to perform cstring operations... so it pays off to be equally good at using both cstrings and strings.

Here is a list of <string> class members. Here is a list of <cstring> functions.

Clinton Portis 211 Practically a Posting Shark

So what are ye' trying to do with this code? (why doesn't it work the way you think it should)

Clinton Portis 211 Practically a Posting Shark

if pseudorandom21 is correct, perhaps having random access to the file data would be an alternative to having to read in the entire file at once.

Clinton Portis 211 Practically a Posting Shark

the thing giving me trouble at the moment is how to read and store a floating point number within a struct, the data specifically for the mark is on 1 line of a text file seperated by white space.

Since ye' data is white space delimited, ye' can simply use the overloaded >> extraction operator provided by the <fstream> library:

ifstream infile;
int record = 0;
int index  = 0;

infile.open("text.txt");

if(!infile.is_open())
{
     cout << "\aError, file not found.";
     cout << "\nFile may have been moved, renamed, or deleted...";
     exit(1);
}

while(infile && record < MAX)
{
     //Get marks
     while(infile >> student[record].marks[index] && index < 12)
     {
          index++;
     }

     index = 0;

     //Get family name
     infile >> student[record].familyName;

     //Get given name
     infile >> student[record].givenName;

     //Get computer login
     infile >> student[record].computerLogin;

    //advance to the next student record
    record++;
}

infile.close();

This is all based on the assumption that your text file maintains a standard format that is shown in your example. This is most of the work done for you. See if you can handle and display the data on your own.

Clinton Portis 211 Practically a Posting Shark

this will create 12 "computer" objects and open 12 .txt files:

ifstream computers[12];

for(int i=1; i<13; i++)
{
     string file("comp");
     file += itoa(i);
     file += ".txt";

     computers[i].open(file.c_str());

     if(!computers[i].is_open())
     {
          cout << "\a\nError, file #" << i << " unable to be opened!";
          cout << "\nFile may be been moved, renamed, or deleted. \n\n";
          cout << "\nPress [Enter] to continue...";
          cin.get();
     }

     file.clear();
}

all you have to do is load each computer object with data.

Clinton Portis 211 Practically a Posting Shark

I believe <fstream.h> is either non-standard or an antiquated header for c++ (i think it's a C header); in which case, would make your instructor incorrect. Sometimes the real education occurs when you have to prove your instructor wrong.

According to wikipedia, the use of ".h" header files is depreciated: http://en.wikipedia.org/wiki/C%2B%2B_Standard_Library


It seems that you are trying to count the number of letters in a document:

if(letter_counter <= 'z' && letter_counter >= 'A') //|45|error: ISO C++ forbids comparison between pointer and integer|            {                letters++;            }//if

This is actually not a bad attempt at accomplishing what ye' wish to do. If your compiler doesn't like this comparison, you can actually use the ascii values for 'z' and 'A', which are 122 and 65, but if you look at the ascii table, there are other values in between 'z' and 'A', like: ':', ';', '<', '=', '>', etc...

So, one solution would involve the isalpha() function from cctype:

#include<cctype>

//char letter_counter[2] can only hold two characters..!  
//let's use a more flexible data type

string line_of_text;   // <--can hold an entire line of data


//These loops will read in the file, line by line
//and count the number of letters

int i=0, 
    size = 0;

while(getline(infile, line_of_text))
{
     size = line_of_text.size();

     do{
          if(isalpha(line_of_text[i]))
          {
               letter_counter++;
          }
    
          i++;

     }while(i < size);

     i=0;
}
Clinton Portis 211 Practically a Posting Shark

Incearca sa schimbe linia #39 din || la &&

Clinton Portis 211 Practically a Posting Shark

Moschops provides a good reason to hide data.. even if it is from yourself:

Even if I am the only user/coder, I know that I am not super-human. I forget things. I make mistakes. Sometimes I do things I know I shouldn't but mean to go back and fix later, and then never do. I need to hide data not only from other people, to protect them, but also from myself, to force myself to code responsibly and not screw myself over six months down the line.

Clinton Portis 211 Practically a Posting Shark

in my opinion, data hiding in today's IT can be best utilized in a few specialized scenarios:

if working on a classified/sensitive project, tasks can be delegated to specific departments, and assembled by one specific person/department. this ensures a 'balance of power' and a system of checks and balances among the programming team. For example, each department may contribute code for a nuclear inertial guidance system, but only a select few will have absolute knowledge of the entire system. This allows for greater control of the project; if information is leaked, it is easy for the company to point the finger at select individuals.

Although this may be an extreme example, 'data hiding' has more applications of a proprietary nature in order to maintain company secrets, especially if portions of the project are outsourced or sub-contracted.

Clinton Portis 211 Practically a Posting Shark

since you have varying possibilities of user input, i believe the better approach to this problem would be to read in the entire chunk of user input, test the input, and parse into individual substrings as desired.

Here is one possible method using the versitle <sstream> library (very useful for string parsing) based on this tutorial:

#include<string>
#include<sstream>

string full_name,
       first_name,
       last_name,
       middle_name;

stringstream temp;

cout << "What is your first and last and possibly middle name?\n";
cin >> full_name;

temp << full_name;

fist_name << temp;
last_name << temp;

if(temp)
{
     middle_name << temp;
}

cout << "Ye' first name is: " << first_name << endl;
cout << "Ye' last name is: "  << last_name  << endl;

if(!middle_name.empty())
{
     cout << "Yo' middle name be:  " << middle_name << endl;
}

If you know you are getting reliable & standard input from the user (which is quite a big assumption), then you'd be able to chain together your extraction operations.. but since you don't, I'd take what you get, figure out what you got, and handle it accordingly.

Clinton Portis 211 Practically a Posting Shark

whenever ye' are using a fstream object multiple times, you should clear the object before using it again.

try calling infile.clear() in the beginning of your function and see if that helps.

Clinton Portis 211 Practically a Posting Shark

beat.

Clinton Portis 211 Practically a Posting Shark

I'm no math wiz, but here is another approach to consider:

Based on your above example of 12.745, you could extract the decimal portion, and put it over its place value. In this case, the .745 has precision to the 1/1000 place (the last digit is in the 'thousanths' position) Therefore, you can just set the numerator as 745 over 1000. Then what you have is a mixed fraction "12 and 745 over 1000."

in c++
12.745 == 12 + (745 / 1000);

mixed form
12 745/1000

reduced
12 149/200

if you want to reduce your fraction you'd probably need a function to test for prime, and one to perform the reduction.

Clinton Portis 211 Practically a Posting Shark

it's good.

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

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 have a feeling you just did this guy's homework.. :(

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

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

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

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.