Narue 5,707 Bad Cop Team Colleague

>So, I cannot use "End of File" function at all. Right?
You can, but it would be more along the lines of stopping the input loop prematurely instead of determining the array size:

for ( int i = 0; i < 1000; i++ ) { // Don't overrun the array
  infile>> P[i] >> V[i];

  if ( infile.eof() )
    break;

  // The records were successfully read
}

Which is quite pointless when you could do this instead:

for ( int i = 0; i < 1000 && infile>> P[i] >> V[i]; i++ ) {
  // The records were successfully read
}
Narue 5,707 Bad Cop Team Colleague

>How do we determine the value of n?
If you can't use dynamic memory (and I'm assuming any alternative that uses dynamic memory, like std::vector), your only option is to make the array large enough to handle any reasonable number of records:

int P[1000], T[1000], V[1000];

Of course, that size would pretty much be an arbitrary choice, and there are two immediate problems that arise from it:

  1. You potentially waste a *lot* of space.
  2. You have little choice but to ignore any records beyond N unless you're able to get clever and manage chunks of the file instead of the whole thing all at once.
Narue 5,707 Bad Cop Team Colleague

>i am new to this so i guess i am making stupid mistake
It doesn't seem that way. If you were using a C99 compiler, it would work due to the new variable length array feature. However, unless you're able to compile as C99 (the latest standard) or your compiler supports an extension that allows variable length function parameters, you're stuck with the old fashioned method, which I assume is what you were trying to do originally:

#include <stdio.h>
#include <stdlib.h>

void foo ( int **p, int m, int n )
{
    int i, j;

    /*
      -Use- p just like an array, even though
      it's incompatible with array types
    */
    for ( i = 0; i < m; i++ ) {
        int j;

        for ( j = 0; j < n; j++ )
            printf ( "%4d", p[i][j] );
        putchar ( '\n' );
    }
}

int main ( void )
{
    int m = 4, n = 6;
    int **p;
    int i;

    /*
      Simulate an array of arrays so you can
      use normal array indexing syntax. There
      are other ways to avoid m+1 malloc calls,
      but this naive method is straightforward
      and easy to understand
    */
    p = malloc ( m * sizeof *p );

    for ( i = 0; i < m; i++ ) {
        int j;

        p[i] = malloc ( n * sizeof *p[i] );

        for ( j = 0; j < n; j++ )
            p[i][j] = i + j;
    }

    foo ( p, m, n );
Narue 5,707 Bad Cop Team Colleague

Then replace "pay attention in class" with "learn the material in time to do the damn assignment".

Narue 5,707 Bad Cop Team Colleague

Hopefully a failing grade will teach you to pay attention in class.

Narue 5,707 Bad Cop Team Colleague

A two dimensional array is not the same type as a pointer to a pointer. If you want to pass a two dimensional array, change your function to accept the correct type. If you want to pass a pointer to a pointer then pass the correct type.

So what is it you really want to do? I can tell you that what you're doing is wrong all you want, or I can tell you how to accomplish your ultimate goal.

Narue 5,707 Bad Cop Team Colleague

I can forgive ignorance. We were all ignorant at one point. What I tend to be less tolerant of is a complete failure to learn. From all of your questions so far, I get the impression that you haven't learned jack and you're not really trying.

I'm honestly not interested in wasting my help on people who won't learn.

Narue 5,707 Bad Cop Team Colleague

Since this is extremely likely to be yet another attempt at starting a new thread with the same question, I know exactly what it is you want. But I'm still going to make you explain your question.

Narue 5,707 Bad Cop Team Colleague

You turned the conditions into string literals. Why? That's clearly not going to do what you intended anymore.

Narue 5,707 Bad Cop Team Colleague

Assuming you're working on Windows, learn about the PE (portable executable) file format and you can easily extract the information you want either manually or with any number of disassembly tools.

Narue 5,707 Bad Cop Team Colleague

static_cast is designed to painful, which encourages you to avoid casting when you might otherwise say "why not?". Another reason I've heard is that it's easier to search for a cast, while the (type) pattern is also seen in non-cast syntax. I have yet to see a case where I've wanted to search for a cast, and I work with C++ syntax parsers. ;)

Narue 5,707 Bad Cop Team Colleague

>none of the opponent ideas has changed my thoughts.
Do you want a cookie or something?

>i am not a cheater because i say cheating must be supported legally
Wait, let me get this straight. You support cheating as being the most ethical thing, but you turn around and say you're not a cheater with the implication that it's illegal? Please explain how your support of cheating can be viewed as anything more than a thinly veiled attempt to get other people in trouble.

Narue 5,707 Bad Cop Team Colleague

>Can you see what I'm trying to do?
You're trying to use the same function to properly handle two incompatible types. A three dimensional array is not the same as a pointer to a pointer to a pointer. A template is the ideal solution for this problem, assuming you can restrict yourself to only passing types that match the usage:

template <typename T>
void FillAndRead(T buffer, const unsigned int Width, const unsigned int Height)
{
	//fill
	for(unsigned int x = 0; x < Width; x++)
	{
		for(unsigned int y = 0; y < Height; y++)
		{
 			buffer[x][y][0] = 3;
 			buffer[x][y][1] = 4;
 			buffer[x][y][2] = 5;
		}
	}

	//read
	unsigned char r,g,b;
	for(unsigned int x = 0; x < Width; x++)
	{
		cout << endl;
		for(unsigned int y = 0; y < Height; y++)
		{
 			r = buffer[x][y][0];
 			g = buffer[x][y][1];
 			b = buffer[x][y][2];
 			cout << r << " " << g << " " << b << " | ";
		}
	}

	cout << endl;
}

If you can't use a template for some reason and you're willing to accept greatly broken code (that has a strong tendency to work) and vastly less convenient syntax, you can pun the incompatible types into a straight pointer and calculate the indices manually:

void FillAndRead(unsigned char* buffer, const unsigned int Width, const unsigned int Height)
{
	//fill
	for(unsigned int x = 0; x < Width; x++)
	{
		for(unsigned int y = 0; y < Height; y++)
		{
 			buffer[x * Width …
Narue 5,707 Bad Cop Team Colleague

There are two generally accepted ways to split a number into digits. First is the string conversion:

#include <stdio.h>

int main ( void )
{
    int value;

    printf ( "Enter an integer: " );
    fflush ( stdout );

    if ( scanf ( "%d", &value ) == 1 ) {
        char buffer[50]; /* Arbitrary "large enough" size */
        size_t i;

        /* Quick and easy, but relatively expensive */
        sprintf ( buffer, "%d", value );

        for ( i = 0; buffer[i] != '\0'; i++ )
            printf ( "%c\n", buffer[i] );
    }

    return 0;
}

Next is the mathematical solution, where you break the number down directly:

#include <stdio.h>

int main ( void )
{
    int value;

    printf ( "Enter an integer: " );
    fflush ( stdout );

    if ( scanf ( "%d", &value ) == 1 ) {
        if ( value == 0 ) {
            /* A typical breakdown loop doesn't handle 0 */
            printf ( "0\n" );
        }
        else {
            /* This technique works for any base */
            const int radix = 10;

            /*
              Note that the digits come in least 
              to most significant order 
            */
            while ( value != 0 ) {
                printf ( "%d\n", value % radix );
                value /= radix;
            }
        }
    }

    return 0;
}

Most solutions are variations of these two general concepts.

Narue 5,707 Bad Cop Team Colleague

>The consonants on the other hand should appear as is.
Then why are you printing "Code Error" for them? I'm having a hard time understanding why you can't grasp what the problem is even after I've pointed it out twice.

Seriously, just change printf("Code Error"); to putchar(c); . This will correctly handle the first character. You need a loop to handle all of them.

Narue 5,707 Bad Cop Team Colleague

substitution table is:
*-a
$-e
/-i
+-o
--u

Show me where 'm' is in that table.

m$$t m$ *t 9:00*m /n th$ p*rk

Show me where 'm' is in the string.

if(c=='*')
    printf("a");
  else if(c=='$')
    printf("e");
  else if(c=='/')
    printf("i");
  else if(c=='+')
    printf("o");
  else if(c=='-')
    printf("u");
  else
    printf("Code Error");

Now show me where 'm' is handled in this chain of if statements. If you show me all of the correct answers, you'll discover that 'm' is handled in the else clause, which prints "Code Error".

Narue 5,707 Bad Cop Team Colleague
if ( option == 'y' )
  // continue
else if ( option == 'n' )
  // exit
else
  // print error
Nick Evan commented: How do you come up with these things??I mean: Wow... ;) +14
Narue 5,707 Bad Cop Team Colleague

>How complicated should a program be for you to
>make plans for it (eg. flowcharts, drafts, etc)?
Anything more complicated than the hello world program.

>When do you just jump into coding with just a vague
>idea of how a program is supposed to run?
When I'm prototyping. However, for me prototyping is simply another way to make plans for a program. I use it to throw around ideas and get a rough idea of what kinds of problems I'll have during development.

Narue 5,707 Bad Cop Team Colleague

If you're sure the problem really has been solved, you can contact a moderator in that forum. We're able to set threads as solved if necessary.

Narue 5,707 Bad Cop Team Colleague

Are you implying that I've mellowed out, AD?

Narue 5,707 Bad Cop Team Colleague

You're not handling the default cause of a non-code. Since the first letter is 'm', and there's no substitution for 'm', your output is correct.

Narue 5,707 Bad Cop Team Colleague

>int *parr= new int[];
*sigh*

>so how would I insert the amount of my dynamic array inside the if statement
One option is to nitialize it to null and add an edge case to the options that require it to be non-null:

int *p = 0;

for ( ; ; ) {
  choice = menu();

  if ( choice == 1 ) {
    size = addamount();
    p = new int[size];
  }
  else if ( choice == 2 ) {
    if ( p != 0 ) {
      ...
    }
  }
  ...
}

delete[] p;
Narue 5,707 Bad Cop Team Colleague

>you're the first person I actually agree with in this thread. Well done.
I think you single-handedly destroyed my chances of getting on Daniweb's payroll. Dani will never trust me again now that the iamthwees of the world have started agreeing with me. :icon_rolleyes:

iamthwee commented: Don't worry, it's the once only kid. +18
jbennet commented: :) +36
Narue 5,707 Bad Cop Team Colleague

Yep, that's what I get for not being anal about terminology. I was typing "array" to mean "simulated array" and got caught up in my own web of lies. ;) Thanks for the correction.

Narue 5,707 Bad Cop Team Colleague

>int size=0;
>int *parr= new int;
For starters, size isn't a compile-time constant, which means you're relying on a compiler extension and your code is thus non-portable. Next, an array of zero size is unlikely to be what you want. The error you're getting generally means that you've written outside the bounds of the array (or in the case of a zero size, written to the array at all). You didn't show the definitions of the functions that are being called, so I can't say for sure what's happening.

>The const doesnt lock the array but doesnt give me an error.
All you're doing is redeclaring a second variable called parr in the nested scope (it hides the existing variable). When you leave that scope, the new variable is destroyed and you have the original variable, which is not const. If you want to "lock" the array, you need to reference it using a sentry object or a const pointer instead of through the original. There's no way to add constness permanently.

I'm somewhat confused about what you mean by "lock" and "unlock" the array. Perhaps you could describe the result you're looking for and then I can help you come up with a suitable way of achieving it. You seem to want something different from const.

Narue 5,707 Bad Cop Team Colleague

>might as well be the geek lounge where the rep points wouldn't affect me
Perhaps I should slap you with a Keep It Pleasant infraction. You're not invincible here either, slick. ;)

Narue 5,707 Bad Cop Team Colleague

Amazing. Visiting the Geeks' Lounge is like sitting in a room full of children.

Narue 5,707 Bad Cop Team Colleague

>Well, I think there should be a rule making helpers
>not come up with words only, but codes also.
Oddly enough, there's a rule that requires us not to do so until you show sufficient effort to deserve replies with code. You see, the problem here is that your code is so completely wrong, it's obvious that you don't understand the first thing about classes or even basic C++. It's not worth our time to give you "codes" because you won't understand it anyway.

My original advice still stands. Go read your book again. It's immediately obvious that you lack the fundamental knowledge required to write your program when I look at code like this:

class Block {
public:
  double weight, friction, force;
};

Block angle1;

// WTF!?
force.angle1==(weight-(weight*friction))/cos(angle1);
Narue 5,707 Bad Cop Team Colleague

>What do you mean, the "SQL end"? Or "the application end"?
>I don't understand those terms.
I think they're rather self-explanatory. Are you being intentionally obtuse again?

>Do I have to program those SQL files on the aplication end, or on the SQL end?
You program them wherever you want, but it's easier to test as you go if you write them from within SQL Server's management utility. Alternatively you can create the database manually during development and then script it into a sql file that can be run on the production database server. Note that these files are separate from your application and should be run as an installation step during deployment.

Narue 5,707 Bad Cop Team Colleague

I believe niek_e was suggesting you show us where the demand for a new forum is. If we added a specialized forum at the whim of every noob with a new hobby, Daniweb would become so diluted as to be useless.

Narue 5,707 Bad Cop Team Colleague

The map class uses an overloaded operator< for maintaining the relation between keys. Your Student class doesn't have this overloaded operator. That should give you a starting point for researching a solution.

Narue 5,707 Bad Cop Team Colleague

>it didn't make sense because I wasn't using any objects ...
What do you think cin and cout are?

Narue 5,707 Bad Cop Team Colleague

>but what's so wrong about it?
Destructors aren't called when you use exit. It's not a C++ friendly function.

Narue 5,707 Bad Cop Team Colleague

You'll need to read the chapter on classes and objects again. You clearly didn't understand it sufficiently to write any code. Once you figure out the relationship between objects and members, you'll be able to clear up the majority of those errors.

Narue 5,707 Bad Cop Team Colleague

That's what this forum is for. An added benefit is you have many people with different experience and perspectives who can help you and keep each other from making mistakes when helping you.

Narue 5,707 Bad Cop Team Colleague

It looks like you're confused about the arrow operator. This:

pointer_to_struct->member

is equivalent to this:

(*pointer_to_struct).member

It's just a convenient notation for merging a dereference and member access into one.

Narue 5,707 Bad Cop Team Colleague

You can't assign to arrays like that. In this case you're pretty much stuck with copying the contents of the string literal to the array manually or with a function like strcpy that does it manually:

#include <iostream>
#include <cstring>

struct StudentRecord
{
	char Name[20];     // (1)
	int  ID;
	float GPA;
};

int main()
{
	StudentRecord MyStudent;
	strcpy ( MyStudent.Name, "Test Value" );     // (2)  (3)
	MyStudent.ID	= 1234;
	MyStudent.GPA = 4.0;
	std::cin.get();
	return 0;
}

Note that I changed the <string> header to <cstring>. <string contains the std::string object and helpers while <cstring> contains the string handling functions (including strcpy) and types inherited from C.

Narue 5,707 Bad Cop Team Colleague

Cast it:

#include <iostream>

void f(int* x) { std::cout<<"int*\n"; }
void f(char* x) { std::cout<<"char*\n"; }

int main()
{
  f((char*)0);
  f((int*)0);
  //f(0);
}
Narue 5,707 Bad Cop Team Colleague

>When you compared your current knowledge to back then, do you see big changes?
Absolutely. If I can manage to go over four years without learning anything significant, it's time to retire.

Narue 5,707 Bad Cop Team Colleague

>where it is hard to be missed by posters who might accidently post onto such a thread
Even the posters who get to the thread via Google? Also keep in mind that we're talking about the same people who unfathomably manage to ignore the sticky threads that say "READ THIS FIRST!!!".

I applaud your noble intentions, but in reality people just don't give a damn. No amount of friendly reminders will change the behavior you want to change.

Narue 5,707 Bad Cop Team Colleague

>The point I want to make over here is, can't we have some
>auto-marking mechanism for threads which identifies the time the
>last post was made onto a thread and if this is more than some
>pre-specified value mark the thread as "solved".
What makes you think the new member in question will bother to check that the thread was solved? It's already clear that he didn't bother to look at the dates. I don't think your solution buys us anything.

>Should he be posting to the same thread or starting a new thread.
He should be starting a new thread. However, there's nothing we can do (short of closing threads after a certain age) to stop him from posting to the same thread. If he posts to the same thread and it's a suitably new question, feel free to report the post and a moderator can move it to a new thread.

Narue 5,707 Bad Cop Team Colleague

>Is there a way to get them all?
Do an advanced search and order by ascending. With that my first post is this one. My last search was with google, but this way is better. ;)

Narue 5,707 Bad Cop Team Colleague

When I joined there were a bunch of know-it-all wannabes running around giving bad advice in the C and C++ forums. I'm fairly confident that my first post was a correction to one of their posts.

The earliest post I can successfully find is on September 26, 2004 (clicky), which is pretty close to my join date, but still short ten days worth of posts.

Narue 5,707 Bad Cop Team Colleague

I'm not sure I understand the question. Are you trying to find our first posts on Daniweb, or are you asking why we didn't first post to the Geeks' Lounge?

Narue 5,707 Bad Cop Team Colleague

Conceptually, calloc works like this:

void *calloc ( size_t n, size_t size )
{
  void *mem = malloc ( n * size );

  memset ( mem, 0, n * size );

  return mem;
}
Narue 5,707 Bad Cop Team Colleague

>But it shouldn't see the end of file until after the 4th line is read, not the third, right?
I think you're confused about the meaning of end-of-file. You're thinking of the file itself, not the stringstream. The two objects are completely independent of each other and have no idea that the other exists.

The stringstream is only aware of the one string you used to initialize it. This is the line you use to initialize the stringstream:

512 512

As far as the stringstream object is concerned, that's the entirety of the file. After you extract the two integer values from the stringstream, the object goes into an end-of-file state.

daviddoria commented: Great explanation! +2
Narue 5,707 Bad Cop Team Colleague

>why would it have gone into an error state?
end-of-file counts as an error state in this case.

Narue 5,707 Bad Cop Team Colleague

...

Well, I'm glad you found your problem.

Narue 5,707 Bad Cop Team Colleague

>cout << *mp0_iter.first() << *mp0_iter.second() << '\n';
This won't work, for several reasons. To begin with, first and second are not member functions. Also, the member operator binds more tightly than the indirection operator, so you're trying to call the first and second member functions (which still don't exist) on the iterator itself rather than the value type. You need to wrap the indirection in parens:

cout << (*mp0_iter).first << (*mp0_iter).second << '\n';

In sufficiently conforming versions of the standard library, the -> operator is defined and you can use that instead:

cout << mp0_iter->first << mp0_iter->second << '\n';

>mp1.insert(1,13);
>mp1.insert(2,16);
>mp1.insert(3,17);
The map class doesn't have an overload of the insert member function that takes a key/value pair as two arguments. You'd have to create your own pair object first:

mp1.insert ( make_pair ( 1, 13 ) );
mp1.insert ( make_pair ( 2, 16 ) );
mp1.insert ( make_pair ( 3, 17 ) );

But that's effort you can avoid by taking advantage of the subscript operator overload:

mp1[1] = 13;
mp1[2] = 16;
mp1[3] = 17;
Narue 5,707 Bad Cop Team Colleague

>i post this thread to help cheaters and they will end up seeing all
>the ethical doctrines of yours about cheating, and maybe they will
>quit the thread without copying the answers huh?
Are you so bereft of logical sense that you can't see how ridiculous that sounds? It seems a lot like you're using my replies to somehow negate the unethical nature of your thread. Are you implying that if I didn't reply, it would be my fault that the "cheaters" who read this thread are going down the wrong path in life and you're absolved of any guilt?