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

>>When and why we need to use fflush exactly?
Depends on the operating system. When writing to the screen (MS-Windows) I never use fflush() because MS-Windows seems to write immediately. I have seen the problem on *nix computers where it was necessary even after putting '\n' in the output stream.

When to do it? You won't have to put it after every printf() statement, you can delay callinf fflush() until after all printf() statements but before any input statements. Lets say you want to display a menu that has 10 printf() statements. Just put the fflush() after the last printf().

For disk file streams call fflush() after all writing has been done. Its not necessary to call fflush() before the file is closed because fclose() will do that anyway.

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

You have to write the code that iterates through all the files in the folder and sum up their sizes. boost library doesn't do that for you. I could easily write such a program without boost, but I doubt that is what you want. You need to write a function that iterates through all the files and directories, then when a directory is found call that function recursively to process all the files in the sub-directory. I posted a code snippet a couple years or so ago that does just that using win32 api functions.

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

Homework? You answer the questions and we will tell you whether you are right or wrong.

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

are you trying to print all the odd numbers between two values? Your program is doing much too much work! It only needs one loop, not two. Delete that second loop and just test if the loop counter d is even or odd.

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

>>player1score = add10 (player1score);

The first example doesn't work because it never changes the value of player1score variable. In the above code, when add10() returns it assigns the return value to player1score. That is not done in the first example. Passing a variable by reference does not do anything if the function does not change its value. Modify the first example like this and you will see the difference

int add10 (int &score) // function that adds 10 to player1score variable by reference
{
      score += 10;
      return score;
}
nats01282 commented: excellent answed my question first post +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Why are you multiplying nrows * sizeof(int*)? AnsiStriong is not an int pointer, so that doesn't make any sense.

>>AnsiString **array = new AnsiString*[nrows * sizeof (int*)];

^^^ Wrong AnsiString **array = new AnsiString*[nrows]; >>array= new AnsiString[ncolumns * sizeof(int)];

^^^ Wrong array[i]= new AnsiString[ncolumns];

Salem commented: Nice +20
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Freaky_Chris commented: Excellent resource +2
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You can not call malloc() to allocate c++ classes. It doesn't work. Use the c++ new operator. You have to call either delete or delete[] to destroy it.

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

The program depends.exe distributed with the compiler will tell you all the DLLs that are required by your program. Probably one or more of them are missing or the wrong version.

mitrmkar commented: A good suggestion. +5
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You have to iterate through all the files in the folder and sum them up. See the link I gave you earlier because boost has a function to do that.

[edit]^^^ beat me to it :)

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

The structure st is wrong and you don't need to malloc() anything. Actually you don't need that structure at all unless you intend to add some other members later on.

abc.txt -- is it a text file (you can red it with Notepad) or a binary file (Notepad just shows junk on the screen) ?

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

The way I understand the problem, the array does not have to have 10000 elements -- 10000 is just the maximum value that any one element may contain. In otherwords you have to generate an array that contains N elements (allocate with new operator), the value of each element is randomly selected between 0 and 10000.

After you find out the value of N, allocate an array of N integers then populate it with values between 0 and 10000. You can use array[i] = rand() % 10000; in a loop.

After that, you need another loop from 0 to Q. In that loop you need to generate another random number between 0 and 10000 then try to find that number in the array.

Of course, I could be all wrong about the problem too :)

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

Well there isn't much to it

int main()
{
   std::string filename;
   cout << "Enter a filename\n";
   getline(cin, filename);
   // now open the file
   ifstream in(filename.c_str());
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Note that the terms "IN", "OUT", and "INOUT" are just a description of what the parameters are used for. Only for human consumption and mean absolutely nothing to the actual program or function. When you learned C or C++ language you did not use those terms, but you did learn the same functionality. You can normally just ignore the terms IN, OUT and INOUT and just concentrate on what the function does, what parameters to pass, and what the return value(s) is(are).

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

>>int abc(void);

That is called a function prototype, which is used to forward declare the function so that the compiler knows what it is, it's return value, and its parameters.

Function prototypes are unnecessary if the function actually appears before it is used, as in the example I posted. You may code the prototype, but it is not required in that case.

The code you posted does not compile because you did not code the function prototype correctly. Change it to this: int abc(float *b); Now change the actual function like this:

int abc(float *x)
{
float p;
scanf ("%f",x); // NO & symbol here because x is already a pointer
// Here is another way to code it
// maybe it makes more sense to you
// what it is doing.
p = *x;
return p * p;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The variable b in main() is passed to function abc() by reference, which means that any changes made to it in abc() will also be made in main(). The parameter x in abc() is a pointer to the variable b in main().

So if you want to print the value of x that is in abc() just print the value of b that is in main(). Even though they have different names, since x is a pointer, printing the value os *x is the same as printing the value of b. Clear as mud?

Add a line of code in abc() to print the value of x and you will see what I mean: printf("x = %d\n", *x); Note the star is required because x is a pointer.

Xufyan commented: great :) +0
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>float main(void)

main() does NOT return float -- only returns int

float abc(float *x)
{
   float p;
   scanf ("%f",x);
   p=*x* *x;
   return(p);
}

int main()
{
   float a,b;
   a = abc(&b);
   printf("a = %f\nb(x) = %f\n", a, b);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

What are the primary differences between wireless voice and paging systems.

One has wires and the other doesn't ?

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

I would think it would be better to implement the queue as a linked list so that it can contain an (nearly) infinite number of items in the queue.

tux4life commented: Yes. +8
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>i have my submissions today n just want to know basics.

Are you a member of the Procrastinator's Club. Apparently so because now is a hell of a time to start the program.

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

You want to use win32 api EnumWindows

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

Look at line 30 of the code snippet you posted. See -- it has one parameter. Now look at line 23 ... there is no parameter. That's what the compiler is complaining about. You need to pass by reference the object you want to subtract.

You need to create another FancyDateClass object and pass that as the parameter. And delete line 31 -- that should be done when you create the FancyDateClass object in main. Better still you should get the current date and initialize FancyDateClass object with that instead of hard-coding some fictitious date.

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

curses is not related to conio.h. From wiki

The first curses library was written by Ken Arnold and originally released with BSD UNIX, where it was used for several games, most notably Rogue. [2] [3] [4]

The name "curses" is a pun on cursor optimization.[5] Sometimes it is incorrectly stated that curses was used by the vi editor. In fact the code in curses that optimizes moving the cursor from one place on the screen to another was borrowed from vi, which predated curses.[3]

curses was originally implemented using the termcap library.[citation needed] A few years later, Mark Horton, who had made improvements to the vi and termcap sources at Berkeley, went to AT&T and made a different version using terminfo, which became part of UNIX System III and UNIX System V. Due to licensing restrictions on the latter, the BSD and AT&T versions of the library were developed independently. In addition to the termcap/terminfo improvement, other improvements were made in the AT&T version:

<snip>
Notes
1. Thomas E. Dickey. "NCURSES - Frequently Asked Questions". http://invisible-island.net/ncurses/ncurses.faq.html.
2. Peter H. Salus (October 1994). "The history of Unix is as much about collaboration as it is about technology". Byte. http://www.byte.com/art/9410/sec8/art3.htm.
3. a b Arnold, K. C. R. C. (1977). Screen Updating and Cursor Movement Optimization: A Library Package.. University of California, Berkeley.

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

And it's not dependent on the operating system. It depends on the compiler.

Pick on AD day ??? It will actually depend both on OS and compiler. AFAIK *nix can't support the functions in conio.h, but I suppose someone could write a vt100 version or one that uses termcap.

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

>>How to fix the problem, please?
what problem?? CopyFile() parameters are const char*, not std::string. Call string's c_str() method to make the conversion.

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

call the funciton isprint()

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

What is the effect of the following code?
main()
{
int option=4;
switch(option/2)
{
case 1: printf("EAST");
case 2: printf("WEST");
case 3: printf("NORTH");
case 4: printf("SOUTH");
}
}

Hint: there are no break statements between the cases.

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

You are asking about how to flush and discard the input stream of all its contents. Narue wrote this thread some time ago that explains how to do that.

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

Number of iterations? Infinite. Why? because 1/2 = 0 (integer division) so the value of x will never change.

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

What the hell makes you think I'm going to do your work for you? I have my own to do.

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

case statements must be constants. You can not use variables there

switch( some_variable )
{
case 'a':
  // blabla
  break;
case 'b':
   // blabla
   break;
case 'c': case 'd': case 'e':
   // this for all three characters
   break;
<etc. etc for each letter you want to support>
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>void print_hello(int n, int *)
What is that second parameter for?

All that is doing is printing one line of * below the text. What about above the text and on each side?

****************
** Hello World ***
****************

This is how to do it

  1. Print the top line
  2. Print two *, then the text, then the last two stars, such as printf("**HelloWorld**\n");
  3. Print the last line

You can not do that all in one big loop.

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

- Hollywood gave them gloss and just HAD to set them in the US, totally ruining them in the process, *sigh*.

That's Hollywood for you :( They did the same thing with Dr. Who.I'm sure glad they didn't get ahold of the Godzilla movies. Oops! I think they did.

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

I found setjmp and longjmp quite useful in database programs to quickly return errors between deeply nested function calls. c++ exceptions are the alternative in c++ programs. goto is only useful within the same function. setjmp and longjmp are between functions, although could be used within the same function too.

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

There are several ways to solve that problem. IMHO this is the simplest.
sprintf() is your friend here

char filename[255]; // final file name
 char* namePt1 = "Labyrinth";
int num = 5;
sprintf(filename,"%s%d.txt", namePt1,num);

ofstream fileLabyrinth(filename);

Alternate method is pure c++

string filename;
string namePt1 = "Labyrinth";
int num = 5;
filename = namePt1;
stringstream str;
str << num;
filename += str.str();
filename += ".txt";

ofstream fileLabyrinth(filename.c_str());
DSalas91 commented: He help me very well! :) +0
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>> //NOPE! Doesn't add 1st digit... because it's stuck in 'ch' b/c of inFile.get();

call inFile.unget() to put the digit back onto the stream.

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

Please stop putting your questions in quote tags -- it just makes your posts confusing to read/understand.

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

Read this link.

In order for the compiler to produce meaningful references to line numbers in preprocessed source, the preprocessor inserts #line directives where necessary (for example, at the beginning and after the end of included text).

When the preprocessor parses your *.c program it will create a *.i file that expands all the #include and other preprocessor statements. In doing that all the include files are merged into one *.i file. When doing that it adds the #line directives so that it can keep track of the line numbers from the original file. Then when the compiler compiles the *.i file it will know the line numbers from the original files and use them to produce meaningful error messages. Without #line directives you would get line numbers from the *.i file, not the *.c or *.h file that you wrote.

I know of no reason why a programmer would want to insert #line directives in his program.

jephthah commented: thanks for parsing that. +7
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Don't do that even if you would. The macro is too large and will greatly increase the size of the final program. Just write a normal function that takes variable arguments.

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

Also asked, and unanswered, here

urbangeek commented: hehe....nice find... :) +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Maybe something like this:

struct person
{
    char name[40];
    struct person* next;
};

struct activity
{
   char activity_name[40];
   struct activitiy* next;
   struct person* head; // head of linked list
};

struct congress
{
    struct activity* type1; // head of type 1
    struct activity* type2; // head of type 2
};
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

What did you mean by 'he's a hack'?

The instructor is an idot who has no clue how to produce modern C or C++ programs with modern compilers. He probably learned how to teach on an old 20-year-old computer and MS-DOS 3.1 with Turbo C, which was one of the best at that time.

Universities should be teaching students the art of modern computer programming with modern compilers and modern operating systems. What is that instructor doing?? Answer: He is teaching students how to use 25-year old technology that no one in industry uses today. And that is really unfortunate for the students because it means they will have to teach themselves what the university should have, but failed, to teach them. How in the hell can someone from that university get a job writing programs for modern software houses?

Salem commented: Well said - students are being robbed by being taught fossil-ware +19
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I wish Dani would write a bot that would automatically delete any post that even mentions Turbo C or Turbo C++.

jonsca commented: If only I could give you more rep than this, I would. :) Check out the post in Area 51. We were going to try to throw a sticky together for it. +2
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

No. All is does is read the file. Where do you see use of cout in that code, e.g. cout << Car_array[i].make; ???

And delete lines 6 and 7 that you posted. After reading the file you don't want to re-initialize the class because the init() method will destroy all the information that was read from the file.

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

C, C++ and C# are all very similar languages, but also very different. Most of the concepts in C were used to create C++, and likewise most of the concepts of C++ were used to create C#. I agree with Myrtle that it may be better to lean one of the two languages first before diving into the other. But each person is different and learns in different ways. If you can learn both at the same time without confusing them then more power to you.

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

No one knows everything about everything. IMO you will know you are a good programmer when your peers tell you that you are one. And that takes a lot of time and practice. Employers do not expect entry-level people to know a lot about programming. You should be able to write simple programs, and probably make errors while doing it. You will not be expected to have the knowledge of someone with a Ph.D. or 10 year's experience. So don't be so hard on yourself. I had a prof who told the class once that graduating from college is not the end of your learning experience, but only the beginning. College just gives you the tools to learn.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
//Sets to default values
Car::Car() {
	make = " ";
    model = " ";
    color = " ";
    year = 0;
    mileage = 0;
}

What will I set the char's make, model, and color = to??

The constructor isn't quite right. To make an empty string all you have to do is set the first byte to 0.

Car::Car() {
    make[0] = '\0';
    model[0] = '\0';
    color[0] = '\0';
    year = 0;
    mileage = 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

@Dave: what are you rambling on about??? What does INT_MAX have to do with subtracting two character values?

>>You are the lord of this roost. I
Not because I am any smarter than anyone else -- I just liked to post and answer questions a lot. I never said I knew everything -- indeed I've screwed up lots of times.

>>especially since you always come along and tell people to write implementation-specific stuff

Not knowingly. If *s1 - *s2 is implementation specific then please tell us why instead of pussyfooting around it. If there is a case where that does not work, then please tell me because I'm all ears :)

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

Your options are NOT either Dev-C++ or BUY a commercial compiler. There are several very good free compilers. Both Code::Blocks and VC++ 2008 Express are free. Dev-C++ is no longer recommended because it's pretty much dead and is installed with an old version of the MinGW compiler.

If you are just starting to learn programming then spending your money on a commercial compiler is a waste of your $$$. If you have that much $$ to throw around then you can throw some in my PayPal account :)

Nick Evan commented: Good advice, but I also have paypal :) +12
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Beats me. You are writing the program, not me. Instead of using the struct record I posted, you can easily change your car record to hold char arrays instead of std::string. Then write the array of those classes like I did the structures. You may have to write them out one at a time if you did not make an array of those classes.