Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

[edit]The line numbers below refer to your post #33

line 30 is showing the value of total before that value has been calculated. Why don't you move that line down to line 110. And it will show the sum of all values for not only the current sentence (or word actually) but all the words read before it. If you want to show the total for just the word that is being displayed then you will need to use a different variable. Since total is already accumulating the value in all those case statements then maybe create another int and name it something like grandTotal then sum it on line 111 and reset total on line 112. Insert the lines below at line 110.

line 111:  cout<<"Words"<<setw(30)<<"Value"<<endl<<endl;
line 112: grandTotal += total;
line 113: total = 0;

You will also have to change line 115 to use grandTotal instead of total

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

depends on how variable crap was defined. If its a character array char crap[255]; then you are right. But if its a std::string then jamthwee is right. And jamthwee shows the prefered c++ method.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If a guy marries his granny does that make him his own grandpa?

Actually I think such a marriage would be legal in USA because the woman is probably beyone the child bearing age.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Here is one way to do it -- first read the entire line into a std::string object then parse it

int _tmain(int argc, _TCHAR* argv[])
{
    vector<string> sList;
    vector<float> fList;
    string name;
    float f;
    string line = "X = {1.0, 2.0, 3.0}";

    // extract the name which 
    size_t pos = line.find(' ');
    name = line.substr(0,pos);
    line = line.substr(pos+1);
    pos = line.find('{');
    line = line.substr(pos+1);
    while( (pos = line.find(',')) != string::npos)
    {
        string n = line.substr(0,pos);
        f = (float)atof(n.c_str());
        fList.push_back(f);
        line = line.substr(pos+1);
    }
    // now do the last float in the line
    f = (float)atof(line.c_str());
    fList.push_back(f);
	return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Use >> operator rather.

CFile doesn't support that, which is one reason I hate that class.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You would have to write a TSR (terminate-and-stay-resident), and I'm not sure that cmd.exe supports that like it did with MS-DOS Version 6.X and earlier.

I thought you might use one of the win32 hooks, but probably not from what I read.

Possibly a device driver or a windows service program

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The problem is in Read_File, you have to pass listTop pointer by reference so that Read_File() can change the value of the pointer that was created in main().

void Read_File(resident *& listTop, int &residentCount)

Next problem with that function -- delete the declaration of listTop at line 47 because it is a duplicate definition of the parameter and your compiler should have given you an error about that.


The loop at line 67 is constructed wrong -- it should be a while loop, not a do loop (which means you can delete the if statement on line 71)

while( inFile >> oneSsn >> oneFirstInitial >> oneLastName >> onePhoneNum >> temp )
{
   // code here
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you wrote it wrong -- what you wrote is the c++ class object, not the string contents. If the file is a normal text file where '\n' is line terminator then you need to tack on the '\n' to the end of the string then write only the string to the file.

FileName += "\n";
f.write(FileName.GetBuffer(), FileName.GetLength());

or

FileName += "\n";
f.write((LPCTSTR)FileName, FileName.GetLength());

Then I would use a C character array to read it back into memory and assign the CString object to it

char buffer;
while( f.read(&buffer, 1) == 1 && buffer != '\n')
   FileName += buffer;

Personally I hate the CFile class -- it is really crude and difficult to work with text files. A better solution is to use either standard c++ fstream if it is available with your compiler (and some compilers do not support it) or use CArchive in conjunction with CFile. CArchive will make the above code quite a bit easier and cleaner.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Have no idea what you want. Care to explain in more detail.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

It is true that the average calculation will work inside the loop, but its a waste of processing time to leave it there. If a teacher wants to find the average grade of all students for a given test she/he does not recalculate the average every time a student's grade is added to the total. No because that's such a waste of her time. Instead she will wait until all student's grades are summed up then divide that total by the number of students.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I would gander a guess that your American Government book is wrong. How can anarchy be a dictatorship. Anarchy has no governing person or body, so how could it be a dictatorship? A dictatorship is where a person or party has absolute rule, so how in gods name can an anarchy be a dictatorship. You should mention to your teacher that the book he's making you use is crap.

Of course another explaination is that Christina mis-read or mis-interpreted what her book said. I sometimes do that -- read into something that is not there.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

consider moving lines 10 thru 12 to the top of the program, above line 1. Traditionally include files go at the top of the programs and everything else follows. Sometime its a lot more than just tradition but a requirement :).

Without knowing more about what your program is supposed to do, and what it does not do that it should, can not answer any more.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Microsoft Visual Studio 6.0
That's your first mistake. Unless you are required to use that compiler by your school you should scrap that compiler because of its age and known NON-compilance with c++ standards. You can get a pretty good free VC++ 2005 Express compiler that will let you code most everything you will learn in school and text books.

Why in the world are you using all those variables ? I thought you are supposed to be learning matrices, such as one declared like this ? That will greatly simplify your code because you only have to deal with ONE variable, not 16.

int matrix[4][4];
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

All you have to do inside the loop that starts on line 23 is (1) total up the value of all the numbers read from the file, which line 30 does ok, and (2) count how many numbers are read from the file, which line 27 does. Then after the file reading is finished you need to calculate the average, which line 33 does do, but its in the wrong place in your program (move it down below line 34 so that its outside the loop). That means line 26 is completly useless, and in fact destroys the value of totalNumber accumulator, so delete line 26.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you probably didn't initialize totalNumber when declaring it

int totalNumber = 0;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I still can't get numbers on the side? I tried using code and inline code to no avail. Sorry, I will keep looking. Thanks!

The trick is to use the languate option like this (but remove the spaces)
[ code=cplusplus]
// your code here
[ /code]

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Yes I agree it is pretty good, and I don't normally like anything written before about 1950 :)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

delete line 35, it has no useful purpose there.

variable sentlength on line 107 is declared in the wrong place -- declare it after line 29 and initialize it to 0 so that it can be used inside the loop. Then on line 35 sum up the number of characters in all sentences with the += operator.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you need a good explaination of those interrupt functions. Here is just one of them you can find. Function 1, which you posted, only tests to see if a key has been pressed. You have to call other functions to actually get the value, such as function 10. But it too only returns the value of ONE key, so you have to create a loop and keep calling those two functions until function 1 says no-more-keys.

what you do with those keys is up to you -- normally you will want to put then into a buffer so that they can be easily used whenever you want.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

cout doesn't destroy the value of the string -- your program can do lots of things with it. You could put a cout statement right after line 34 if all you want to do is display the value of the string.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>. The program only shows the last word from the file(why doesn't it show all the individual words and their respective individual letter)?

because you didn't tell it to do that. Recall that the >> operator deletes the previous value stored in the string them reads the next word from the file and saves it there. If you want to display all the lines then you have to call cout just after the word is read.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

This is how to get the line numbers
[ code=cplusplus]

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

because the length of the last word read == Retatteration -- has 12 letters and the program read 58 words, so 12/58 = 0 (integer arithmetic drops the fractional part).

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I do not like the line numbers. It makes it impossible to test the code someone posts without extensive editing.

Just click the "Toggle Plain Text" and you have it without line numbers, and it also makes it easy to copy into the clipboard so that you can past it into your program. No need to drag and highlight, just right-click and select "Copy" from the popup menu near the cursor. That is a really great feature for large amount of code that span more than one page.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If you want reading and evaulation to be two different functions then you need to read the file into an array of strings (probably a vector) so that the evaluation function can see them.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you have to also correct line 106 as I mentioned previously. sentence is only the value of the last word read from the file so its length is not appropriate for that calculation.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you have mismatched braces in function checkPrime() that is causing the problem

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

As for next year's election in USA, I'm neaning to either Hillory or Obama. I don't see any Republican candidates I want to vote for. And being mayor or a major city (Rudy Giuliano) does not qualify someone to be President of US.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

we voted for margret thatcher and she was terrible (

So that is why you all re-elected her for 3 or more terms ?

Thatcher's tenure as Prime Minister was the longest since that of Lord Salisbury and was the longest continuous period in office since the tenure of Lord Liverpool who was prime minister in the early 19th century. She was the first woman to lead a major political party in the UK, and the first of only three women to have held any of the four great offices of state

That sounds like quite an accomplishment for anyone, let alone a woman.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Thanks for your reply. So, there is not a readstring command?

Nope -- in assembly language you have to roll your own. You can use C library functions, but that would defeat the purpose of you writing assembly language.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Depends on what operating system you want to use. If MS-DOS version 6.X or earlier then you need to make it a TSR. There is quite a bit about that on the net, just google for it. MS-Windows doesn't use memory resident programs like that. It uses DLLs and device drivers, neither of which you would want to write in pure assembly. C or C++ are recommended languages for that. And if you do that, the code you posted simply can not be used at all because the os will not permit it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>But i use NASM as my compiler

Its not a compiler, but an assembler. :)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Actually, that loop is also wrong. Your program is supposed to be counting words, not sentences. So you need something like this

string word;

while( infile >> word )
{

}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>numWords+=numWords;
wrong syntax. should be this: ++numWords; . And move that line outside the switch statement, between lines 33 and 34. Then delete all those other likes where that occurs. You only want to count the number of words, not the number of characters.


>>It's saying that total & numChars are undeclared.
you have them declared in the wrong place. Move lines 32 and 33 outside that loop at line 27.

>>sentence.size()/numWords<<endl;
wrong. you need to keep track of the total length of all sentences read. What you have here is just the length of the last sentence. You will need another int variable to do that.

line 30 is also wrong. It only checks for eof but never actually reads the file. Correct it like this:

while ( getline(infine, sentence) )
{
   // your code here
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

initialize the data in the constructor then. Or add another const method to do that

class vector
{
...
const void initialize() const;
};

int main()
{
   const vector v(5);
   v.initialize();

}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

dont't declare it with const keyword. A class object declared like that is pretty useless.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

That's probably true for serious scientific computing. If that's the kind of program you are writing then you are correct not to use it. But for most other kinds of programming its as good as any other.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If you want to calculate the average word length then you will need another counter to sum up the length of all words read. After finished reading just divide total length by total number of words read.

>>average of the evaluation
After finished reading the file divide total by numChars

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>but using the random number generators rand(), which I understand is not a very good idea
why not ?

>>but I have NO idea how define it in my class
declare it as static and there will only be one instance of it for all instances of your class.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you need to repost your program because your original code will not produce that output.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

... keeping in mind that you have a problem if you want to add strings too. Think very carefully if you need them.

Not a problem -- just add the string to that. It might require some special handling. Microsoft has implemented identical thing in its VARIANT structure which is used all the time in COM programming, VB and VC++ compilers.

>>after "genStack<dtype> gs;"
like : i want to push an intger, do i just write:
"gs.push(7);" ???
then why it said "can not convert int to dtype"and "type mismatch in parameter 'el' in call to genStack<dtype>::push(const dtype &)"?

sorry for stupid qestions.

No you can't just do that. You have to first create a dtype object then push that object onto the stack

const int sType = 1; // short integer
const int usType = 2; // unsigned short
const int iType = 3; // integer
// etc. etc
genStack<dtype> lst;
...
...
dtype d;
d.type = iType; // we want an integer
d.iVal = 7; // set the integer value
lst.push(d); // put on stack
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you had it all right except the open statement, and Duoas gave you the correct way to do that.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I thought you are supposed to be writing a c++ program? If so, then why are you resorting to C language syntax and C functions?


>> freq[filecounter]lineineachfile[filecounter]

I think you are missing square brackets in that statement.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Ok thank you, I usually boot from a cd when I do programming. So i use backtrack which is Linux.

You mean you actually boot linux from a CD :-O You must be awfully patient to wait all that time for your computer to boot up.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Also what assembler because some assemblers have macros that do all (or most) of the grunt work of calling system functions.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

since infile is of type ifstream it is not necessary to specify ios::in because that is the default

after closing the stream you have to also clear the error bits because if you don't the file will not open the second and succeeding loop iterations.

infile.close();
infile.clear();
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If you want to store mixed data than I'd use a structure that has two members: (1) a union between all the data types you want it to support, and (2) an int that indicates what kind of data is stored, such as 0 no valid data, 1 is an short, 2 is an unsigned short, 3 is a int, 4 is an unsigned int, etc. etc.

struct dtype
{
   int type;
   union data
   {
        short sVal;
        unsigned short usVal;
        int iVal;
        unsigned int uiVal;
        long lVal;
        unsigned long ulVal;
        // etc for all other data types
   };
};

now just use that structure (or class if you like)

genStack<dtype> lst;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You need to complete that switch statement and add the cases for all the other letters. It didn't work because that sentence doesn't have an 'A'.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>1. Is my declaration of each letter correct?
No. They should be declared as floats, not char, because you will need the decimal places, and make them const because their values will never change.

const float A = (1.0F * 2.0F);//value for letter A

2. use a loop to look at each letter in the string, then a switch statement to calculate the value. (probably also answers your other questions) Example:

std::string sentence = "How Now Brown Cow.";
float total = 0.0F;
int numChars = 0;
for(int i = 0; i < sentence.size(); i++)
{
   switch(sentence[i])
   {
      case 'A': 
              total += A; 
              ++numChars; 
              break;
      // blabla
    }
}