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

>> while ((fgets( line, BUFSIZE, file )!=EOF))//why shoudn't I use EOF is there something better?

You really should learn to read the documentation about the functions that you use. If you had, you would have found out that fgets() returns a char pointer or NULL when end-of-file is reached. Don't just toss functions at a program without knowning what they do. you can google for most fiunctions, such as "man fgets" will return the man page for that function.

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

does the last line of the file contain '\n' ? If not, then your program will not count it. fgets() will fix that problem because it reads all lines. Getting the length of the longest line is trivel too -- just use strlen() to get the length of the line read by fgets().

The size of the file can not be determined by either your program or using fgets() becuase in MS-Windows os reading text files converts "\r\n" into just "\n". It will be easier to get file length by calling fstat() or stat().

file=fopen("summaryData.txt","w");
			 fputs("avgchar",file);

you forgot to close the file after writing to it. This will cause your program to eventually run out of file handles. And the fputs() statement is probably incorrect -- all it is doing is printing the same hard-coded text "avgchar" in the file each time. Use something like this:

fprintf(file,"%d\n", avgchar);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>but the stats are incorrect
how do you know that? What are you comparing them to?

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

open the log file for append mode, not write mode, if you want just one log file that contains info for all files
fopen(filename,"a");

or create a unique log file name if you want results in separate log files.

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

given the example in this article and and the example you posted, where "->" represents a linked list, you would have a 2d linked list that looks like below ?

0->2->4
1->3->4
2->5
3->6
6->0

The first number in the pair is the row number, and the second number is a node in the list represented by the row number.

Is that how you define your problem?

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

Here is an example that will illustrate the difference between using EOF and 0. In this example, checking for EOF causes infinite loop because end-of-file is never reached due to conversion error. If you uncomment the while statement that check for 0 then the program will stop when conversion error occurs.

When you wrote your programs for school you probably wrote sample data files that did not contain errors. That will not always be the case in real-world.

#include <stdlib.h>
#include <stdio.h>
 
int main(int ac, char** av) 
{
	int a;
	int counter = 0;
	FILE* fp = fopen("myfile.txt","r");
	if(fp != NULL)
	{
		while( fscanf(fp,"%d",&a) != EOF)
//		while( fscanf(fp,"%d",&a) > 0)
		{
			printf("%d\n",counter++);
		}
		fclose(fp);

	}
	return 0;
}

This is sample data file

1
2
40
abc
5a
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you can't actually delete it. you can set it to 0 if you wish, but it will still be there. What you can do is decement the variable that contains the number of elements in the array. Just pretend the array only has 9 elements insead of 10.

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

>> return a[count];
the above is inside the loop, so the loop only runs once.

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

>>while (fscanf(c,"%d",&a)!=EOF){

fscanf() does NOT return EOF one eod-of-file. EOF is returned only if there was a conversion error. What you want is this:

while (fscanf(c,"%d",&a) > 0){
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you could google for your question. Here is one way to do it. And another here.

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

like I mentioned in my previous post, printf() family of functions, which includes ssprintf(), will convert int to hex using "%X" . Example:

char buf[20];
int i = 16;
sprintf(buf,"%x",i);

If you use that in dek_hex() you will have to change the return value from char to char* because as you can see from the above code snippet the hex value is an array of characters, not a single character value.

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

you can use fscanf("%d", ...) to read them into an int memory, and fprintf("%X" ...) to write them back out as hex.

Sin-da-cat commented: Way too nonchalant and broad. +0
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

C or C++?

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

I took out the push's and pop's because they really didn't not have any effect on the code.

I got into the habbit of pushing all registers that I want to save before calling any interrups or functions because (1) general purpose registers ax, bx, cx and dx are subject to change without notice and (2) you don't know if the function or interrupt called will change them. Better safe then sorry.

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

xor ax,ax
is the same as this
mov ax,0
except that xor is a tiny bit faster

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

I think it should be something like this -- I didn't assemble or test it.

;
mov  bx,1 ; row counter
loop_start::
xor cx,cx ; column counter
l1:
 mov al,'*'
mov ah,0eh ;prints to screen
int 10h 
inc cx
cmp cx,bx ; have we printed enough stars ?
jne l1  ; no, then do it again
:print CR/.LF
;
; save our two loop counters
push bx
push cx
mov al,10 ;line feed
mov ah,0eh
int 10h 
mov al,13 ; CR
mov ah,0eh
int 10h
; restore our two loop counters
pop cx
pop bx
; process row counter
inc bx
cmp bx,5
jne loop_start
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you need two counters -- the first counter counts the number of lines to be printed, the second counter counts the number of stars that are printed on the current line. Your code is putting a CR/LF after every star. This should happen only after the stars have been printed on the current line. You need two loops and two counters to accomplish that.

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

>>Since inheritance is not supported in C
neither is the new operator and classes.:rolleyes:

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

we might be seeing undefined behavior in those examples. I made the program a little bit more complex by adding a base class and VC++ 2005 produced different results than before

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;
class base
{
public:
    base() {m_xx = 1;}
    int getXX() {return m_xx;}
private:
    int m_xx;
};

class My : public base
{ 

   public:
      int a ;
} ;
 
int main( )
{
    My* first = new My ;
    My* second = new My() ;
   
    // this will print out junk i.e. uninitialized value
    cout << "value of var init without round brackets : " << first->a << " " << first->getXX() << "\n";
   
    // this will print out 0 i.e. initialized value
    cout << "value of var init with round brackets : " << second->a << " " << second->getXX() << "\n" ;
}

results

value of var init without round brackets : -842150451 1
value of var init with round brackets : -842150451 1
Press any key to continue . . .

Don't count on () version initializing POD data because it might not. If you need them to be initialized then write a constructor that will explicitly do it.

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

Below is the output of the three compilers I use.

This is Dev-C++

value of var init without round brackets : 0
value of var init with round brackets : 0

M$ VC++ 6.0

value of var init without round brackets : -842150451
value of var init with round brackets : -842150451
Press any key to continue

VC++ 2005 Pro

value of var init without round brackets : -842150451
value of var init with round brackets : 0
Press any key to continue . . .
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you example produces the same output when compiled with my compiler.

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

they are the same -- the first form is normally used to pass parameters to the class constructor. The second form when there are no parameters to pass.

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

in the button's event handler call GetSaveFileName()

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
while(m_iCount <= 5)
	{
         }

How many ways can you say infinite loop ? and it is consuming nearly all CPU time?

why are you making it so difficult ? If you goal is for the program to pause for 5 second, just call Sleep() function. No need for that timer.

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

He wanted me to initialize the array to 255, and I did. It runs 255 times with wrong output.
I'll post the code

No I did not tell you to run that loop up to 255 -- re-read my example code again, the loop stops when size number of items has been counted (variable size is the number of characters entered from the keyboard).

Do you understand why you want an array of 255 integers for counting purposes ? You could do it with fewer, but 255 is the fastest way. If you do not understand it I'll try to explain it again.

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

It doesn't work because you have not allocated any memory for the objects. array_persons is just an array of 10 pointers that point to some random memory locations. You need to allocate memory using new operator.

array_persons[i] = new Person;

and don't forget to free up the memory before the program terminates using delete

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

>> status = fgets(list, 80, inp);

fgets() does not return an integer -- it returns char*. And your loop is incorrect

while(fgets(list, 80, inp) != NULL);
     {
          // blabla
   };

>> fscanf(inp, "%d", &seconds);
fscanf() is the wrong function because it redads from a file not from memory. Since the digits have already been read using fgets(), just use atol() to confert to int

seconds = atol(inp);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
int counter[] = {0};
for (int i = 0; i < 255; i++)
{
counter[letters[i] - 48]++;
cout << "LETTER - " << letters[i] << " APPEARS " << counter[i] << "TIMES\n";
}

array counter is not declared correctly. You have to tell it how many integers are in the array by putting a number between those two square bracket thingies.

int counter[255] = {0}

the loop counter should not go up to 255 because the loop counter is used as an index into character array letters and that array has at most 96 letters (could be fewer). So the loop counter must count from 0 to the number of characters in letters array.

<snip>
else
{
letters[k] = letters[k];
}

Above from function uppercase(). That is totally unnecessary, it is a do-nothing statement. You should just delete it (your compiler probably will delete it anyway).

for (k = 0; k < size; k++)
{
if (letters[k] >= 'A' && letters[k] <= 'Y')

{
answer[k] = letters[k] + 1;
}
else if (letters[k] == 'Z')
{
answer[k] = letters[k] - 25;
}
else if (letters[k] < 'A' || letters[k] > 'Z')
{
answer[k] = letters[k];
}

What is all that? First, letters array has already been converted to all upper-case, so there is no need to test for it here. Second, what is the purpose of array answer. If that was just for your own debugging you should have deleted it before posting -- and you …

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

I just want to ask how to reach the position which I want

Just repeat the code Bench posted until you get to the desired line. You will have to use a loop and a counter integer to keep track of the line number that was just read. If you are confused about loops -- which is understandable -- you should read about them in your text book.

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

Reading from a file works the same way as reading from any other istream, such as cin:

//read up to newline
std::string str;
std::getline(cin, str);
//read up to whitespace (after a word or character)
std::string str;
cin >> str;

With files, replace cin with the name of your ifstream

and just repeat that three times.

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

I didnt ask you to write this program, I wrote it.

Yes you did and no one is trying to do it for you. I was attempting to make some constructive criticism to show better way of coding something. If you plan to write computer programs you might as well get used to that because you will encounter criticisms your entire career. Its called "peer review" and required by most companies that you will work for. you might as well get over being so defensive about other more experience programmers evaluating your code.

.And i thought i used the code tag because i highlighted the text and hit the code text button, which is what i was told to do.

I didn't say anything about code tags -- what you saw is in my signture and appears in every post I make. Yes you did use code tags correctly. :)

.the loop is needed so it will print all those titles again if someone wanted to enter another year again after the first time. otherwise it just prints the dates. I was just wondering if anybody new what could be used to loop printtitle with the main

you don't have to do anything. When printtitle() function returns the loop in main will take care of everything. The do loop in printtitle() is not needed.

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

what is the purpose of that do loop? Why not just remove it ? And you don't need that switch statement if you put the month names in an array of strings.

char *months[] = { "", "January","February", ... "December"}
cout<< months[monthnum] <<setw(20)<<year<<endl;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The problem is somewhere else. You should post the rest of your program. You should have declare array count like this:

int count[255] = {0}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Please read my signature and follow the link

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

>>And, I can simply call this from within the program when needed, say from the area it originally was located?

As long as it only gets called ONCE. Every time it is called it will reseed the random number generator

mattyd commented: ty +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The number of braces in printShipPos() looks ok to me.

srand((unsigned)time(0));   //RNG
    float shipPos; 
    for(int index=0; index<1; index++){

two comments: (1) srand() should only be called once throught the lifetime of the program. Best place to put it is near the beginning of main() function.

(2) I hope you realize that loop will be executed only once. If you do then there really is no point to the loop.

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

Paradox: didn't you read this thread that you started a couple days ago ?? The question was answered there, complete with examples.

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

Here's a working example of how to reverse a string using a function. Hope this helps, good luck!

#include <iostream>

using namespace std;

void ReverseString(char string1[], char reversed[]);

int main()
{
    const int MAX = 80;
    char string1[MAX];
    char reversed[MAX];
    cout << "Enter your string: ";
    cin.get(string1,MAX);

    ReverseString(string1, reversed);

    return 0;
}

void ReverseString(char string1[], char reversed[])
{
    int length = strlen(string1);

    for (int i=length-1, j=0; i>=0; i--, j++)
        reversed[j] = string1[i];

    for (i=0; i<length; i++)
        cout << reversed[i];

    cout << endl;
}

You really should compile and test your code before posting. It contains several bugs that may just confuse the OP.

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

you have already coded all the required overload operators. In main() just use them like this

int main()
{

   Complex x;
   Complex y( 4.3, 8.2 );
   Complex z( 3.3, 1.1 );
   cout << "x: " << x;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Does anyone know how to writea program to print

Yup. Next question :)

create two loops -- outer loops counts backwards from 10 to 1, the inner loop counts forwards from 1 to the value of the outer loop counter. Then print the value of the inner loop counter.

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

Where's the global instance of charge ? I can't seem to find it...

I thought it was just above main(), but I don't see it now. So maybe I was seeing things?

Sorry Joe, I accidentally edited your post instead of creating a new one. But fixed it now.

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

variable charge is declared both as a global and as a local variable inside main(). Delete the variable in main() because it hides the global.

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

What operating system are you using? That is a linux function and not supported in MS-Windows. But if you are using linux then I don't know the answer because I don't have a linux computer available to me where I am at.

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

here is how I would code it.

cout << "The string reversed: ";
for (i = strlen(reversedUserInput)-1; i >= 0; i--)
{
// print the [b]ith[/b] character
    cout << reversedUserInput[i]; 
}
cout << "\n";
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

strcpy() function does not take 3 parameters --

strcpy(reversedUserInput, userInput);

>>for (reversedUserInput; i >= 0; i--)
This is incorrect. you have to initialize variable i with the length of reversedUserInput string minus 1 (because of null terminator)

for (i = strlen(reversedUserInput)-1; i >= 0; i--)

>>cout << "The string reversed:\n" << reversedUserInput << endl
This is incorrect too. It will display the entire string in normal order (not reverse order). It should print just one character on each loop iteration.

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

please post code so we don't have to guess what you are doing. But basically its like this

char string[] = "Hello World";
int len = strlen(string)-1;
while(len >= 0)
  printf("%c",string[len--]);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you need to add the parentheses so that the compiler will know it is calling a function.

randomno = randgen(); // Generate random number

>>it has a variable double register random
do you mean you declared it like this ??:

register double  random;

All of the compilers I have used over the years only allow register keyword for integers (short, int and long). And today most compiler will ignore the register keyword altogether. There is no requirement for a compiler to honor that keyword.

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

you can read it liike this

while (theFile >> temp >> windspeed)
       {
           // blabla
      }
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There is lots of on-line help for the pow function -- such as this one.

while (theFile >> number)
{
   cout << number << "\n";
}

that will read every number in the file, assuming the file contains no other strings. The program will store evey number read into the same variable, so you will have to save it somewhere else if you want to use it for something else.

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

What he posted does compile ok after fixing minor posting errors. But the problem lies when attempting to pass a double to the push method instead of a pointer, such as

weight.push(student.weight); // error