Clinton Portis 211 Practically a Posting Shark

As soon as you said this:

Could anyone tell me why this code read an extra line?

I immediately looked for this:

while (! in_stream.eof());

This is a very common problem when testing eof() for an end of file condition. Instead try this:

//Test the return condition of the file extraction, much better than eof()
while(in_stream.get(next_symbol))
{
     //do stuff
}

//Read the file safely without ever going out of bounds
while(getline(in_stream, data))
{
     //do amazing things
}

This might require some minor restructuring of your code, but this is the safe and preferred method for reading a file without having to test eof().

Clinton Portis 211 Practically a Posting Shark

perhaps you could floor() the variable prior to testing for zero.

Clinton Portis 211 Practically a Posting Shark

The recommended scheme for File I/O is to create an fstream object, attempt to open a file, perform error checking, and load the file.

The code in question is performing error checking to see if the file was actually opened. It is quite possible that the file may have been moved, deleted, renamed.. or was just never created:

#include<fstream>

//create an fstream object
ifstream infile;

//attempt to open a file
infile.open("C:\\Users\\Dave\\Documents\\test.txt");

//perform error checking
if(!infile.is_open())
{
     cout << "a\Error!  File could not be opened!";
     exit(1);
}

A slight variation involves the use of the ifstream constructor:

ifstream infile("C:\\Users\\Dave\\Documents\\test.txt");

if(!infile)
{
     cout << "a\Error!  File could not be opened!";
     exit(1);
}
Clinton Portis 211 Practically a Posting Shark
pToMax = &arr[0];

//should be
pToMax = &arr;
Clinton Portis 211 Practically a Posting Shark

winapi is good.

Clinton Portis 211 Practically a Posting Shark
//attempting to initialize with an uninitialized value out of bounds of the array
int numOfStudents[100] = students;

//function name spelled wrong
acceptScores (numOfStudents);

//Here you try to pass an array pointer to a function prototyped to accept an int
acceptedScores (numOfStudents);

//You should dereference the pointer to an int using an array subscript
acceptedScores (numOfStudents[1]);

//The function definition:  the function name is spelled wrong/does not match it's prototype.  Additionally, you lack a starting { curley brace to start the block of code.  
void acceptScores(int numOfStudents)

//All these 'for' statements should be 'if'
//also, you can't have a ; semicolon after an if() condition
//also, you create a variable 'scores' but never assign anything to it.
for (scores >= 80);

//You cannot 'return' anything in a void function()
//Even if you could, you can only return one item 
return A, B, C, D, E;

//missing semicolon in printedscores() function
int students

//variable not declared inside of printedscores()
scores

//incorrect for loop condition
//should be something like for(int grade=0; grade<=students; grade++)
for (grade = 0; grade)
Clinton Portis 211 Practically a Posting Shark

I would make 'name' a char array (cstring):

//line #7
char name[80];

Now you can actually accept a name from the user:

//Line #32
cout << "Enter ye' name: ";
cin >> name;

Since you are using cstrings (character arrays) you should become very familiar with the <cstring> library. One function you may be interested in is called, strcpy():

#include<cstring>

//Line #41 
//Be careful here with names[11][11], your 'name' must be less than 11 characters (leaving room for a null terminating character)
strcpy(names[9][0], name);

Other suggestions:

  • Always make sure your character arrays are null terminated.
  • Your use of goto is frowned upon by the modern OOP programming community (it leads to a phenomenon called "spaghetti code"). Instead, try making function calls:
switch(next)
{
     case 1:   loopcontinue();
     break;
     case 2:   loophighscores();
     break;
     case 3:   exit(0);
     break;
     default:  display_error();
}
Clinton Portis 211 Practically a Posting Shark

Here is working code that compiles, runs, and tests good:

#include<iostream>
using namespace std;

int main()
{
    unsigned char cstring[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
    bool binary[5][8];

    for(int i=0; i<5; i++)
    {
        cout << endl << cstring[i] << '\t' << (int)cstring[i] << '\t';

        for(int j=0; j<8; j++)
        {
            binary[i][j] = cstring[i] & 1;
            cstring[i] >>= 1;
            cout << binary[i][j];
        }
    }

    return 0;
}
Clinton Portis 211 Practically a Posting Shark

Check this out:
http://www.daniweb.com/forums/thread240216.html

Please help me in this program.My problem is that we are given that total budget of a game is 50000 but when the total expenses increases what should we do .....

You calculate a percentage of 50,000. Then it either falls into one of five categories:

1. If the expenses greater than 80% show as” Very Expensive”.
2. If the expenses are greater than 60% and less than 80% than show “Expensive ”
3. If the expenses are greater than 50% and less than 60% than show “Less Expensive ”
4. If the expenses are greater than 40% and less than 50% than show “Not Costly”.
5. If the expenses are less than 40% than show “Best”.

Display output based on percentage. Even if you have an expense that is greater than 50,000, it will still qualify as case #1 in the above assignment criteria.

Clinton Portis 211 Practically a Posting Shark

After briefly looking at ye' code.. I would guess a displayPayment () would probably take place of everything going on between line #47 and #52.

Clinton Portis 211 Practically a Posting Shark

Try this and see if it works:

//Line #6
void getAge(int&);

//Line #24
void getAge(int& years)
Clinton Portis 211 Practically a Posting Shark

A lot of people have questions about how to load a text file using file I/O. There seems to be 2 popular methods to loading a text file:

Use fstream's >> extraction operator. Simple enough, load the file directly into individual containers, word at a time, but you lose your delimiting white spaces (assuming you need them). So some file integrity is lost, unless you make the effort re-construct the file by re-inserting all the white spaces.

So instead, you decide to use getline(). You preserve all white spaces and load the file line by line... but now you have to parse a line of data into individual substrings; either by using <string> member functions, performing string[] array operations, or using strtok().

One alternative I would like to suggest: why not do both? It is possible to read a file in it's entirety and read in text 'word at a time' into individual containers.. without having to do any string parsing:

#include<string>

//Be sure to open your file in binary mode
infile.open("C:\\Users\\Dave\\Documents\\test.txt", ifstream::binary);

//Here you can load the file in it's entirety    
while(getline(infile, lines[i]))
{
     //Go back to the start of the line
     infile.seekg(begin, ios::beg);

     //Now you can load the same data into individual containers
     for(int j=0; j<4; j++)
     {
          infile >> words[word_count];
          word_count++;
     }

     //Discard any extra characters left behind
     infile.ignore(100, '\n');

     //Save current position, so you can go back to the beginning of next line
     begin = infile.tellg();

     i++;
 }

So now you have …

Clinton Portis 211 Practically a Posting Shark

I'm not sure what you are asking for, but I think you seem to be interested in preserving your white spaces. Try using getline():

string line[50];
int pos = 0;
int i=0;

while(getline(datoteka, line[i]))
{
     //Go back to the start of the line so you can extract data based on white spaces
     curr_pos = datoteka.tellg();
     char_count = gcount();
     datoteka.seekg(curr_pos - char_count);

     //Now you don't have to parse line[], just read the same line in again
     datoteka >> studenti[i].naziv_student >> studenti[i].visina;
     
    i++;
}

So now you have 'string line[]' which contains all the lines of the file with white spaces preserved.

This code is untested, uncompiled, and may contain small easy to fix errors (specifically, I am concerned about 'off by one' errors associated with my seekg() 'get pointer' placement.)

Clinton Portis 211 Practically a Posting Shark

You want to return the min and max gpa from your binary search tree.

Q. Is your tree sorted by gpa? If so, (if ascending order), your lowest gpa would be on the lowest far-left node. Conversely, your high gpa would be on the lowest far-right node.

If BST is not sorted by GPA, I am not sure you can traverse the tree starting from the root node and guarantee that ->right nodes will be greater GPA's, and ->left nodes will be lesser GPA's.

If this is the case, you can provide a function that will sort your BST based on the GPA attribute. One quick and dirty method would be to push your BST into an array, sort the array based on GPA. At this point you might as well get the min and max GPA... In this scenario, you lose the speedy quickness of using a BST, but you only need 1 BST to exist in memory.

Another possibility would be to create copies of the BST that are sorted by attribute. In this case, have 3 trees.. one sorted by ID, one sorted by GPA, one sorted by age. Then just search the appropriate BST as needed to return speedy results for the given attribute.

Another possibility is to sift through every node of the BST, keeping track of min and max GPA along the way (why even use a BST if you do this.. )

There are probably better methods …

Clinton Portis 211 Practically a Posting Shark

One way I would approach this, is not to worry about templating the function at all until you get a function that does what you want it to do. Once you got that down, you can easily go back and do all the necessary templating.

With that said, just concentrate on writing a basic array sorting function. I would google for c++ bubblesort.

Btw, you have failed to ask an intelligent question in virtually every aspect, so I suggest you don't come back until such time as you show some sort of effort on your part.

Clinton Portis 211 Practically a Posting Shark

Check out this cool DOS tutorial http://www.adrianxw.dk/index.html

I believe you might specifically be interested in part 5 which talks about keyboard and mouse events.

Clinton Portis 211 Practically a Posting Shark

I guess this should be:

unsigned char cstring[6] = {'H', 'E', 'L', 'L', 'O', '\0'};
Clinton Portis 211 Practically a Posting Shark

From what I understand, an 'unsigned char' datatype will give you straight-up binary properties.. 8 bits, 1 byte, 0 to 255.

My main task is to find a way of generating a boolean array of each character

I think a pseudo-code for the task at hand could be this:

1. create an unsigned char c-string of desired characters

unsigned char cstring[5] = {'H', 'E', 'L', 'L', 'O', '\0'};

2. create a bool array that will equal (n*8) the size of your unsigned c-string

//5 characters, 8 'bits' per character (no need to account for null terminating character)
bool binary[5][8];

3. perform bit shifting and extraction on individual c-string elements in order to populate 8 elements of the bool array

//haven't done bit shifting/extraction in awhile, hope this is right
for(int i=0; i<5; i++)
{
     for(int j=0; j<8; j++)
     {
          //extract right-most bit
          cstring[i] & 1 = binary[i][j];
          //shift all bits right one place
          cstring[i] >> 1;
     }
}

4. you now have the bool array populated with the equivalent binary representation of ascii characters, feel free to make any additional bitwise operations you need.

Clinton Portis 211 Practically a Posting Shark

Since ye' have a singly-linked list, I believe it is important to 'look ahead' to the next node as much as possible.. because you don't have a pointer to go backwards which can make it hard to affect the previous node.. when you delete a node.. so that it points to the 'next' node..

confused? so am I.

//Try this..    we can now look at the next node in the list and make important decisions
if(Temp->link->Ch == CharToDelete)
{
     NodeToBeDeleted = Temp->Link;
     Temp->Link = NodeToBeDeleted -> Link;
     delete NodeToBeDeleted;

     CharFound = 1;
}

Before when you just had

if(Temp->Ch == CharToDelete)

we attained a pointer to the current node which needed to be deleted.. but had to way to refer back to the previous node, change it's 'link' so that it points past the deleted node.. to the next node in line. With the new and improved if() test condition, we can look one node into the future.

Clinton Portis 211 Practically a Posting Shark

so, should i change the equal sign to ==?

Have you ever seen an assignment operation take place inside of if()..??

More importantly, were you let down by NIN's release of year zero, in which Trent Reznor claimed to be his greatest artistic effort?

Clinton Portis 211 Practically a Posting Shark

pretty good at basic, qbasic, intel x86 assembly, and c++.

i know a little c, and a little java.

i've been studying c++ for years that's like the only thing I really know.. and i'm still learning, so I'm kinda biased.

Clinton Portis 211 Practically a Posting Shark

This is all my opinion, but I think pointers were big in C due to its heavy use of c-strings (character arrays). Also in C, it requires the use of pointers to allocate memory for certain data types.

I believe in c++ it is possible to do most of these things either by passing values by reference, or use of the keyword 'new' to allocate memory.

So, in modern day c++ one would not expect to see a whole bunch of pointers, but in my opinion they are often taught in c++ classes just so you know how to use them.. if you needed to use them.

Clinton Portis 211 Practically a Posting Shark

c++

you can do a lot of lower level stuff.. like handle pointers and memory addresses.

c#

you can make a lot of gui controls quite easily.. contains a lot of the same general constructs you would see in c++ (loops and if/else logic for example)


if you are an algorithm and data structure type of hombre, then I would suggest c++. if ye' would like to make them fancy user friendly gui programs, then I would suggest c pound.

Clinton Portis 211 Practically a Posting Shark

If you like programming like I do.. why not learn both..?

Clinton Portis 211 Practically a Posting Shark

Anyone please feel free to chime in if you see anything wrong here:

//line #11
if(Temp -> Ch [B]=[/B] CharToDelete)
Clinton Portis 211 Practically a Posting Shark

on my code I use the same on line 21 and 26. What is wrong???

Starting at line #65 you are local inside of the constraints of the function.. you are no longer inside of the main() function. Additionally, in the function parameter list (line #65) you named the variable 'array'.. but then on line #85 you try to pass an argument named 'array1' into the showarray1() function.

There is no variable named 'array1' located inside of the scope of bubblesortarray1().

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

Another good ol' database program. This calls for 'an array of structs'

create your data type:

struct record
{
     string description;
     int product_number;
     double unit;
     int sold;
};

Now that you have designed your object, you need to create a bunch of them:

record database[100];

One of your menu options should provide a function to populate your objects... whenever the user wishes to add to the database:

do{

     cout << "Enter product description: ";
     cin >> database[i].description;

     cout << "Enter product number:  ";
     cin >> database[i].product_number;
     ...
     ...
     etc. etc.

     i++;

}while(another == 'y' || another == 'Y');

You also have the ability to search your array of structs:

//this will return the element position of the found record in database (if not found, return -1)
int find_by_sales(record database[], int sold_target, int entries_made)
{
     int i=0;

     while(database[i].sold != sold_target && i <= entries_made)
     {
          i++;
     }

     if(i == entries_made)

          return -1;

     else

          return i;
}

You can have similar functions to perform sorting as well, where you can sort the database[] array based on a single attribute.

Here is an easy way to sort the database[] array based on a single attribute (in this case, we'll use sales) using the sort() function from <algorithm>

#include<algorithm>

bool sales_test(record& i,  record& j){return (i.sold < j.sold);}

sort(&database[0], &database[entries], sales_test);

You can also have a menu option that will display the entire database contents:

void display_database(record database[], int entries_made)
{
     for(int i=0; i<entries_made; i++) …
Clinton Portis 211 Practically a Posting Shark

line #85, the name of one of your arguments is incorrect:

showArray1(array1, SIZE);

Also, just out of curiosity, why do you have two seperate functions to display an array.. it seems to me that one showArray() function could work for any array.

Clinton Portis 211 Practically a Posting Shark

I will assume your problem is here. You test for a space just fine.. but what about the rest of the time when there is stuff to be copied..?

//Try this:

for( i = 0; orig_string[i] != NULL; i++ )
{
     //Check for space
     if( orig_string[i] == ' ' )
     {          
          count++; 
     }
     //Else, add characters to your word
     else
     {
          strcat(words[count] , orig_string[i]);
     }
}

There is probably a better way to do this using strtok().

Clinton Portis 211 Practically a Posting Shark
//line #24 you might have to do this
string = &name;
Clinton Portis 211 Practically a Posting Shark

This might be another way to read in a file without testing for eof()

do{
    
     getline(infile, file_string[i]);
     char_counter += gcount();
     i++;

while(gcount());

cout << i << " lines contained in file. ";
cout << char_counter << " characters read from file. ";
Clinton Portis 211 Practically a Posting Shark

it will say "scout arrived at 100,200"

After reviewing your loop, this seems to be the correct output in order to establish the terminating condition of your loop.. where toX == currentX AND toY == currentY... which will occur when toX becomes 100 (which is the initialized value of currentX) and toY becomes 200 (which is the initialized value of currentY)

Clinton Portis 211 Practically a Posting Shark

Look closely.. something isn't right here.

//line #66, parameter list
float calculateGrossPay ( char ps [ ], int input [ ], [B]float [ ][/B] , int size)
Clinton Portis 211 Practically a Posting Shark

One thing I notice is that on line #48 you make this assignment:

b = a;

and then on line #30 you totally undo the previous operation with each loop iteration:

b = start +i;

So I'm not sure what you are trying to do, but something here doesn't make sense.

Clinton Portis 211 Practically a Posting Shark

good catch..

i need to get a compiler on me' old laptop.

They have spell check on here.. why don't they have code check ;)

Clinton Portis 211 Practically a Posting Shark

I'll put down my crack pipe now and state the arrays can only be passed by reference.. what was I thinking...

Clinton Portis 211 Practically a Posting Shark

The 'curl' api as provided by Jonsca might be more related to what you want to do.. I've just been browsing activex tutorials myself and mainly see ways to create web based controls (buttons, edit boxes etc) and have yet to see anything about affecting web pages (uploading/downloading script etc.)

Clinton Portis 211 Practically a Posting Shark

May I know use of Method returning a pointer in C++, please?

What you are saying resembles english and seems quite insightful, so I will offer you this:

#include<iostream>

int* get_address(int&);

int main()
{
     int number = 0;

     std::cout << "Enter a number: ";
     std::cin >> number;

     std::cout << "The memory address for the number is: " << get_address(number);

return 0;
}

get_address(int& num_addr)
{
     return &num_addr;
}
Clinton Portis 211 Practically a Posting Shark

I believe that when it comes to bridging the gap between programming and the internet, people seem to recommend the use of activex

ActiveX™ is a technology built on the Component Object Model (COM) that allows the developer to create objects or controls that can be used to "activate" content on the World Wide Web.

The article also goes on to specificially identify the pros and cons of using c++ with activex.

Clinton Portis 211 Practically a Posting Shark

What you are tasked to do is not really that difficult.. what part of the assignment is giving you the greatest difficulty?

Clinton Portis 211 Practically a Posting Shark

I believe that this may be related to your problem..

You are passing in objects to your functions 'by value' which necessitates the need to save a return value from your functions.

//you create this stuff
int stoodunce[4]; 
char stoodunce1[4];
cfloat stoodunce3[4];

//and pass it into these functions 'by value' hence, cannot affect them in memory..  you'd have to save a return value from the functions
getEmployeeData(stoodunce, stoodunce1 , stoodunce2, 4);
calculateGrossPay(stoodunce1, stoodunce2, stoodunce3, 4);
displayEmployeeInfo(stoodunce, stoodunce1, stoodunce3, 4);

The current sheme you are trying to use (which I like because it is more efficient than passing stuff around by value) is to have your functions to affect variables in memory; thus eliminating the need to save function return values.

So, to turn your code into an efficient 'pass by reference' program, be sure to prototype your functions to accept memory addresses rather than having to make copies of its arguments. Luckily, the answer is very simple:

//Function Prototypes
void getEmployeeData(int*&, char*&, int*&, int*&);
void displayEmployeeInfo (int*&, char*&, float*&, int*&);
float calculateGrossPay (char*&, int*&, float*&, int*&);

To pass variables into your functions by reference (using the & 'address' operator) you give them the ability to directly affect the variables in memory.

Suggestion:

I see that you are down with commenting out pieces of code, which is a very usefull tool. To make life simplier, you can place a /* at line #103 and a */ at line #107 and comment out several …

Clinton Portis 211 Practically a Posting Shark

In that case, you either have to do this:

//line #18
int numVowels;

Or this:

//line #114
return &numVowels;

But not both, just do one or the other.

Clinton Portis 211 Practically a Posting Shark

Try the following:

case 'A':	 cPtr = getStringFromUser();
          numVowels = vowels(cPtr);
          break;
Clinton Portis 211 Practically a Posting Shark

What part of the assignment is giving you difficulty.. ?

After breifly looking at your attachment, it seems that the assignment is already well outlined for you.. all you have to do is create a simple child class with one additional attribute that will identify the primary work on the cd. Even the main() driver has been written for you. It seems like all you have to do is to create a simple child class, and write the function definitions.. which shouldn't be too difficult.

One thing to consider though, there are some rules about passing pointers to child classes, although I can't remember specificially but I know that there are some issues that can arise.

Also, if I am not mistaken, parent class functions that will be used in child classes need to be prototyped as 'virtual.'

Clinton Portis 211 Practically a Posting Shark

I agree with twomers, using cin seems to be a 'safer' method than using getline(), in that cin will (unless something wack happens) will read in the entire input buffer while use of getline() will extract everything except the '\n'.. leaving it behind to thwart your subsequent attempts of reading the input stream:

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );
Get line from stream

Extracts characters from the input sequence and stores them as a c-string into the array beginning at s.

Characters are extracted until either (n - 1) characters have been extracted or the delimiting character is found (which is delim if this parameter is specified, or '\n' otherwise). The extraction also stops if the end of file is reached in the input sequence or if an error occurs during the input operation.

If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it. If you don't want this character to be extracted, you can use member get instead.

The ending null character that signals the end of a c-string is automatically appended to s after the data extracted.

The number of characters read by this function can be obtained by calling to the member function gcount.

A global function with the same name exists in header <string>. This global function provides a similar behavior, but with standard C++ string objects instead …

Clinton Portis 211 Practically a Posting Shark

In my opinion, I think it's possible to decompile an .exe into a somewhat hackish assembly code.... where if a person has a lot of time on their hands, could do a translation from assembly to a higher level lanaguage. (which of course, would probably not even be close to the original code... even if the person knew which language it was coded in.)

Clinton Portis 211 Practically a Posting Shark

Do you like long walks on the beach

Clinton Portis 211 Practically a Posting Shark

1. Create an ifstream object.
2. Create an ofstream object.
3. Attempt to open Expenses.txt using your ifstream object.
4. Perform error checking to see if Expenses.txt actually exists.
5. If file open successful, load file into a storage container of your choice (string, array of strings, or a vector of strings seem to be the most popular choices.)
6. Close your ifstream object in efforts to save valuable system resources
7. Display the loaded file to the screen.
8. Use your ofstream object to open/create Expenses2.txt
9. Perform error checking to see if file opening/creation was successful
10. If successful, write to the Expenses2.txt from your storage data type.
11. close your ofstream object.
12. Start a loop
13. Prompt the user for an expense
14. Save user input into an array of doubles
15. Prompt if wish to enter another expense
16. If yes, Loop
17. Else calculate expense average.
18. Display average to screen
19. use if/else logic to test expense average and display desired ouput as per your assignement requirement.
20. return 0;

The is probably the best help you'll ever get without any effort or code from your part. I suggest you don't come back until you have something to show us.

Clinton Portis 211 Practically a Posting Shark

You are right on brother.

One suggestion: whenever you see a self re-assignment, you should have "compound operator" runnin' through your noggin'

y = y + 1;

//is the same thing as

y += 1;  //The += operator signifies "accumulation"

It's such a common task to re-assign a value to the original variable, that c++ made these compound operators available to you:

y = y / x;     y /= x;
y = y * x;     y *= x;
y = y - x;     y -= x;
y = y + x;     y += x;
y = y % x;     y %= x;

for (i = 0; i<=6;  i+=3)
for (j = 0; j<=15; j+=5)
kvprajapati commented: helpful! +6