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

Oh I see. When you first open the program read the output file into studentAvgQuiz array so that array looks just like it when when the program was shut down. Then when you write it out DO NOT open with the flags I mentioned previously -- if the file exists then you want to overwrite any exting data in it.

1. on program startup open the output file for reading and read all data into the array.
2. close the file in #1 above and reopen for output with the ios::trunc flag to delete all data in the file.
3. write the array to the file like you already posted

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

The open function does not take std::string object but a char*, so call its c_str() to get it

inFile.open(RES_FILENAME.c_str());

[edit]what Dave said ^^^^[/edit]

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

I assume you want the file to look like this after entering the 4s

0 0 0 0 5 0
0 0 0 0 5 0
0 0 0 0 5 0
0 0 0 0 4 0
0 0 0 0 4 0
0 0 0 0 4 0

As I said before you have to open the output file with ios::ate or ios::app flag otherwise you will get the behavior you are experiencing. Or, if the file is never closed after calling enterQuiz() then at the beginning of that function call seekp() to move the file pointer to end-of-file before writing to it.

quizFile.seekp(ios_base::end);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

when you open the output file add the ios::ate flag so that the open function will move the file pointer to the end of the exting file data after it opens the file. Normally the file pointer is set to the beginning of the file and if you don't use seek() functions to set it to the end then your program will just overwrite whatever is there. The ios::app flag will always move the pointer to the end of the file for each write and the ios::ate does it only when the file is first opened.

quizFile.open("quiz.dat", ios::out | ios::ate);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 64: That does not call the function but declares a prototype and my compiler VC++ 2005 Express says it is different than that included in windows.h (winuser.h). If your intent was to actually call that function then code it like this:

HMENU hMenu = CreateMenu();
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I didn't want to fill up a thread with a bunch of code, but I couldn't find a way to attach a file to an e-mail using this site's "send a message" system. I would have inserted the .CPP file.

Hit the Go Advanced button then scroll down the page and you will see an option to attach a file. You do NOT send it to DaniWeb via email, but upload it using the options for the Manage Attachments button.

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

all you did was comress it which to me makes it harder to read. .

Nope -- we didn't do that. In the future please hit the Preview button before posting so that you can see what it looks like.

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

I have seen that behavior with VC++ 6.0 on XP os with Norton Antivirus running. Antivirus program prevented the IDE from saving the file successfully.

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

instead of getline() you could use the >> extract operator

string name;
int number;
int line_count = 0;
while( myfile >> name >> number && line_count < x)
{
    ++line_count
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The formatting is simply awful! If you reformat in more readible style and repost people may help you faster. Also do not use color tags -- use code tags and DaniWeb will add correct colors.

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

line 1 is wrong -- eof() doesn't work the way you think it does. Here's the correction (combin line 1 and 3)

while ( getline(potter, text) ){

The getline() function does not put the '\n' in the input buffer, so the test on line 30 will never ever be true. The solution is to just display '\n' after that loop terminated.

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

can't help you without the code because I can't see your monitor from my chair.

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

Unbelievable. 75 posts and you still don't know how to format your code ! I certainly hope you do not plan to turn in that to your instructor.

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

Well, I compiled with VC++ 2005 Express and get one warning

'deleteCell' : recursive on all control paths, function will cause runtime stack overflow

You need to fix that because if that function is called it will crash your program.

[edit]
move gets set to true at line 118, which means the test at line 126 is never true.

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

Another suggestion: don't hard-code the array dimensions all over the place like that. If you ever have to change it chances are pretty good that you will make a mistake somewhere and it will require a considerable amount of debugging.

The solution to that problem is to define const integers at the top of the program then use those constants whenever needed. That way there is only one place in the program to change the array dimensions.

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

for starters, move lines 27-33 down after everything has been calculated. You can't print the values of the variables before the calculations have been completed.

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

>>That doesn't work either
why do you say it doesn't work??? Looks ok to me.

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

The total variables have to be declared outside the loop so that they don't get re-initialized on every loop iteration.

>>It doesn't work
Then you didn't do it right. In the example I posted, grossPay is the gross pay for the current line in the file. Gross pay is not included in the file -- you have to calculate it based on other information in the file

Gross = hours worked times hourly rate plus overtime hours time hourly rate times 1 ½

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

you are supposed to be summing up the total amounts, not counting how many there are. For example Total Gross Pay is:

grossPay = ???
totalgross += grossPay;

>>and that would go inside the while loop?
Yes because those calculations have to be done for each line in the file

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

>>How can i get the following:
Looks simple enough -- just add more variables to hold the those totals and add them up while reading the file.

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

chooseNumber() only permits the player to enter a value between 1 and 9. Yet the matrix is 15x12. Why the difference ? 1 and 9 are the values in the cells, not the matrix size.

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

NASA predicts that the Sun will also reverse its own magnetic poles during 2012 as result of reaching the end of the current 11-year sunspot cycle.[12]

Above from Wikipedia.

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

rename the parameter to something else other than Average. There is no point to that parameter anyway, just make it a local variable and get rid of that parameter.

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

In 2038 or 2106 (depending on signing) most UNIX/Linux and modern Windows NT based systems will fail due to a date bug. This could very well signal the end of the world, or just anothe fun time for y2k paranoids

Not true any more with modern compilers because that problem has been fixed for another hundred years or so. Old compilers like the original Turbo C and all programs produced with them will be trashed.

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

Depends on the operating system. MS-Windows/MS-DOS (and probably *nix) if you don't specify otherwise the file will go in the current working directory where the program was started. If you use MS-Windows use Windows Explorer to search your program's source code directory and you will probably find it there. Or use the search feature to locate it.

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

>> am a newbie to the C++ arena
attempting to write a windows program is NOT a good way to learn c++ language because it is too advanced. Start with something a lot simpler and work your way up from there.

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

you have to rewind the file back to the beginning. Before doing the while loop search call dict.seekg(0); And move that line that says "word not found" outside that loop because if the program goes there it means that the word was found.

iamthwee commented: Nope, you don't have to use dict.seekg(0) to read the file again. -2
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>The compiler builds it and the output is ok
Yes the compiler will build it, but its not ok. What you have is called buffer overrun. Look at what you posted and THINK about it for a couple minutes. Why do I say buffer overrun -- because array element at indices matrix[NO_OF_ROWS][NO_OF_COLUMNS] does not exist. Your program is scribbling all over memory outside he boundries of the array.

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

The code in lines 16-20 is all wrong. you need nested loops to do that, and array indices are the values of the loop counters, like this:

array[row][col] = 0;

Or even easier -- you don't have to do the above at all if you initialize the array when it is declared

int matrix[NO_OF_ROWS][NO_OF_COLUMNS] = {0};
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I can't read your mind -- post most recent code that includes the changes we have already discussed. I'm not going to repeat myself, so re-read all previous posts to this thread if you need to. If what we have suggested doesn't work then post the code you tried. Don't guess -- do copy/paste.

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

>>mathid[10] the array starts with size[5].when an array wants more memory it must renew it self and ask more space.

You can't dynamically expand the array size when it is statically allocated like you posted in the class. You will have to make mathid a pointer if you want to change its size at runtime.

Or better yet, since this is a c++ program using std::string instead of character arrays. The string class will do all the messary memory allocations for you, simplifying your whole program.

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

I don't know what you mean. the replace method does not have the correct number of parameters. See these examples, code snipped below

// replace characters of tooth string with num (here length) 
  // characters from teeth string, beginning at index 21
  string tooth = "He ate it all with a toothpick";
  cout << tooth << endl;
  string teeth = "teethpick";
  tooth.replace( 21, teeth.length(), teeth );
  cout << tooth << endl << endl;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Look at my code below and tell me what i'm doing wrong.
the code you posted contains some syntax errors and missing semicolons.

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

well, when you get around to posting something that includes the fix that I last mentioned I might get around to answering you. In any event -- to late for me tonight, going to sleep. Good night. :)

AD

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

you obviously ignored my previous post so I'll ignore yours.

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

that while loop is close to being ok, but not quite. If the word is found you need to jump out of that loop if you don't then the if statement that is after the loop will not work correctly.

That loop is also still incorrect because the return value of find could be somewhere in the middle of the string, which is not what you want. For example, lets say the word you want to find is "Mary" and the sentense is "Hello my name is Mary.". In this example the find method will return a value of 17 because "Mary" starts in position 17 of the string. But you want a string with the word "Mary" at the beginning of the string and ignore other places. This means that if statement has to be changed to check if found is 0, not npos.

while(getline(dict,line)){
    if ( (found = line.find (word, 0)) == 0 )
    {                                      
          cout<<"\n"<<line<<endl;	
          break;
     }
}

to convert to lower-case you need to call tolower() in a loop and call it for every letter in the word. There is another way, assuming word is std::string object and not a character array.

translate(word.begin(), word.end(), word.begin(), tolower);

You will also have to do the same thing with each line read from the file, just to insure that they are all the same case.

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

re-post your most recent code. What you posted earlier was not right, as I mentioned before. Pay close attention to the placement of the braces. compilers do not accept sloppy coding.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	string line;
	char* word="";
	size_t i;
	fstream dict("c://example.txt",ios::in);

	if (!dict.is_open()){ 
		cout << "Unable to open file \n";
		exit(1);
	}
	cout<<"Enter word to search \n";
	cin>>word;
	{
	while(getline(dict,line))   //read line by line
	i = line.find(word);    //try to find the var word in the string line an assign it to i
	
	if (i!=string::npos)      // dont exactly know what it does
  cout<<i<<" not found";      
	else 
		line.compare(word) != 0;  //compare the string line to the var word 
    cout << "result " <<line << endl;
	exit (1);
  
}

	return 0;
}

I know youre trying to make me understand but am a little lost here really !!
i put comments around the lines to see if i understand what each does correctly , please correct me if am wrong..

thanks a lot

you put the braces in the wrong place. This is how it should look:

while(getline(dict,line))   //read line by line
{ // start of while

    // your code goes here

} // end of while
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

read the INSTALL file and it will tell you that they used VC++ 6 and NASM assembler.

2.a/ Requirements.
------------------

- MS VisualDev 6 Processor Pack 5 or MS VisualDev 7
- nasm installed as 'nasm' in the msvc binary search paths.

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

A little off. First you need to put braces around that entire while statement. Next, the find mentiond returns string::npos if the word is not found. You need to add a check to see if the find method worked or not.

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

expand the while loop to extract the first word (hint: use string's find() method to locate the first space), then compare that with the string that you entered from the command line. If they are the same then you can stop that loop.

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

yes -- read each line sequentiall from star to finish until you get the line you want. use fstream's getline() to read an entire line then check only the first word to see if the line is the one you want.

Don't attempt to code all of that at one time. First get your program to open the file for reading. After that is code and compiles without error, add a loop using getline() to read each line. Get that working then code the next part.

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

>you can't write a sentence that makes any sense.
Sorry for not being a scholar in English. >_>

We don't expect you to be. Just write sentences that people can understand -- grammer doesn't have to be perfect, just understandable. I still have not figured it out

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

First, I'm not understanding why are you all being so weird here. There totally no of TOTAL Just refer to your code.

Look who's calling the kettle black -- you can't write a sentence that makes any sense. :)

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

>>am i wrong??
Yes, you are wrong. You ALWAYS have to declare variables before they can be used. And on line 24 total is no different. pass by reference means that the receiving function ( display_output in your program) will have direct access to the referenced variables which must be declared somewhere in your program. So if display_output changes the value of total then that change will be visible when display_output returns to its caller -- in your program main().

And in case you have not figured it out yourself by now, line 45 is incorrect. You forgot the last parameter -- total.

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

Those errors mean that you prototyped the functions using the reference operator, but the actual functions get the paramets by value, not by reference. The functions, even though they have the same name, are not the same. Fix the problem as already pointed out a couple times before.

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

>> I'm receiving 4 errors
please post them.

my guess about the errors: The parameters on line 45 must match exactly line 6. Same with line 5 and 40.

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

you are close -- delete lines 36 and 46. Then delete the +1 from line 37 and the -1 from line 45. and line 45 should be min = computerGuess Finally, line 47 calculation is incorrect.

computerGuess = ((max - min)/2 ) + min;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

show us the code you have written so far. Do you know how to use vector class? That is the easiest array to use. You can make a vector of string objects

std::vector<std::string> theArray;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The flush function forces data to be written to a disk data file (or some other output stream).

There is a couple ways I can think of to solve your problem:
1. write each value to a file then read the values back and display them when the user is done.

2. Save the values in a vector or some other kind of array/container.