WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

2 things, please
#1) Format your code. We'd like to be able to read it and help you.
#2) English, please. This is a professional programming board, not a chat room.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Let's fully parenthesize the comparison, shall we? No sense going half way and this remove all ambiguity:

// will be executed atleast once
do
{
    // something
}
while (((first < second) && (third != fourth)) || (iterations < 5)) ;
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

It doesn't, but the smallest prime number is 2, so the largest prime number for n is n/2. Anything >n/2 and <n cannot be a factor of n, so why check above n/2

thnx. but wat beats me is .. what is the need for n/2 ?

why do we need to consider half of the no. to check if its a prime ?:-|

Sheesh! OK, you want to find out if 31 is prime. What's half of 31? 15, rounded down. The next value is 16. Can 16 possibly be a factor of 31? How about 17, 18, 19...? If it's impossible, why check them? Therefore you can stop checking at n/2

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

So I take it the only reason for you responding to this thread was to increase your post count? I'm looking for a tutorial for code caving because I receive multiple requests on my website; however I do not have time to write one. I was hoping by going out of my way and find a published tutorial to link to would suffice.

I would have suspected (must be due to ignorance) a moderator of this board would be more keen on helping someone, not flex his ego.

Considering only his post count, I would say JWenting is a more honoured member at 100 times your post count. Add to that you haven't been here for 1.5 years, you have no reason to snipe a one of our senior members simply because you feel his response was of no help to you. I for one felt it was justified as I found answers on my first Google attempt. Based on your response here, you have nothing to complain about...

Keep it respectful. Infractions are easy to receive.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

why does 'y' need to be < x/2 ??:-|

It doesn't, but the smallest prime number is 2, so the largest prime number for n is n/2. Anything >n/2 and <n cannot be a factor of n, so why check above n/2

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Not even going to try to read this code. Format it and repost, and use CODE tags.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

And when you strcpy(String, "abcdefghijklmnopqrstuvxyz") ; into a 26 character array like this you just blew past your array bounds because a 27th character (\0) is loaded too. Remember, a string in C always ends in \0 and that must be accounted for in the buffer.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You need 3 coin arrays:
A) The best coin solution so far
B) The last permutation of the coin solutions
C) The next (working) permutation being calculated.

1) First solution (A) is your initial run with 2 twonies.
2) Load that solution also into (B).

3) Load the next permutation based on (B) into (C) 
4) Load (B) with (C) for the next permutation. 
5) If (C) yields a better solution, replace (A). 
6) Generate next permutation at step #3
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Jeez! Stop shooting in the dark and think about the problem. First, format the code properly with braces and spaces so it can be read, then:

void populate(int array[][col], int row_size, int col_size)
{
    int x, y;
    for (int i=0; i <= row_size; i++)  // This loop just blew your array   
                                       //    boundaries. Take it back to <
    {
        for (int j=0; j <= col_size; j++)  // Same here
        {
            array[i][j] = 0;
        }
    }            
//  srand(time(NULL) * rand());  // don't multiply by rand -- does nothing
    srand(time(NULL));
    for (int i=0; i < max_total; i++) 
    {
        do              // start a loop
        {
            x= rand() % row_size;
            y = rand() % col_size;
        } while (array[x][y] != 0); // keep looping (get new coordinates) 
                                    //   if the location is already filled
        array[x][y] = id;
    }/*endfor*/
}

In the above code, I changed the for loops to have a "<=" that may fixed the problem. Keep the coutLoc for loop "<=" suggestion set along with this one, and let me know.

Looping 1 extra time is not a solution for data collisions. What if you had 2 collisions before the extra loop? You'll be missing one value. What if you had no collisions before the extra loop? You'd have too many values.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

The code you posted seems to work fine for me... I get

Please enter the amount of money you would like changed: 4.63
2 Twonie(s)
0 Jonnie(s)
0 Loonie(s)
2 Quarter(s)
1 Dimes
0 Nickel(s)
3 Penny(ies)

thats:
4.00  2 Twonie(s)
      0 Jonnie(s)
      0 Loonie(s)
 .50  2 Quarter(s)
 .10  1 Dimes
      0 Nickel(s)
 .03  3 Penny(ies)
-----
4.63
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Walt, when I'm using for loops, my C++ compiler allows me to declare and initialize a variable within a for loop.

In C++, this is allowed:

int main(void)
{
    int i;
    i = 23;
    int j;
    j = i * 3;
    for (int k=1; k < 10; k+=2)
    {
        int newval;
 . . .

In C the above is not and should be changed to

int main(void)
{
    int i;
    int j;
    int k;

    i = 23;
    j = i * 3;
    for (k=1; k < 10; k+=2)
    {
        int newval;
 . . .

The variable declarations must be made before any executable code. This includes function calls.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I got a more than a little errors most of them say something like

error C2143: syntax error : missing ';' before 'type'

I wonder if this is because The function is located in a separate C file

No, it's because your syntax is wrong.

Steps to correct:
#1) Look at the error
#2) Figure out what it's telling you
#3) Look at the line number it mentions
#4) Look at the actual line in the code, and a couple lines above it
#5) Figure out what your code says based on the error

And if you can't quite see the problem, post the exact error here and about 5 lines above and below the line in error -- with a comment on the line that's wrong at least mentioning the line number, or "error here".

The reason I mention exact is if you don't understand the error, you can't very well paraphrase the error

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You've basically got it. Did you try it? What happened when you did?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

LamaBot, please watch your formatting. We are trying to show by example. Poorly formatted examples tells the poster it's OK to be sloppy...

Your examples are good, we just need to model good practices.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

LamaBot, please watch your formatting. We are trying to show by example. Poorly formatted examples tells the poster it's OK to be sloppy...

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You know, LamaBot, you don't have to quote every post in their entirety if you aren't going to reference any part of it. And you can edit the quote down to only show the relevant portions you wish to comment on. That would help by not having a 200 line quote with a 3 line reply that has little to do with the quote. Just a thought... ;)

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

ok maybe I didn't explain this right the array has a fixed amount of rows and columns. I am supposed to randomly populate the array with 12 objects
I wanted to identify the objects with a number such as 1. the user is then prompted to guess the location of the hidden objects.

First define your array, kinda like you did: int multi-d[rows][columns] Then fill it with 0's with for loops.

Then loop from 1 to 12 and use LamaBot's idea with rand() to get a location in the array. Check if the location is zero. If so, load the loop value. If not, get another location.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

OK. I'll bet sourceforge.net has something you can use.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

(sorry word wrap on makes sloppy looking code it doesn't really go on to the next line like that...........

Then stop TABbing so deeply. Use only 4 spaces, not 4 TABs

class monster{
    
    public:
        monster(){}
        void initiate_enemy(int level,lvl_type ctype, int player_x, int player_y,char [50][50]);
        ~monster(void);
        void get_pos(int & xpos, int & ypos);
        void truepos(int & xpos, int & ypos);
        bool move(char [50][50]);
        void take_damage(int dam, int pierc);
        void cast_spell();
        void melee_attack(character player);
        void get_stats(int & health, int & mana, int & dam, int & defence, int & pierc);
        void show_stats();
        bool passable(char map[50][50],direction direction);

        direction facing;
        bool dead;

Like that...

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I like this part: tolower(i[q]); I've always wanted "to lower an IQ..." :mrgreen:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Im trying to write my own function that does this with array pointers so there is no point to use an existing function .

So? Looking at and understanding an existing function is the way to learn how it's done. Then you can write your own using a similar (not identical) technique.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Also, " while (!TextFile.eof()) " will cause your program to 'fail' at the end of file. Here's why [.eof() == feof()]

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

actually sir i don't hv c in my pc so m not able to check.yell me whether this one is right or not.

i hv some more queries regarding c++.
will u help me by highlighting my errors,when i'll send u the prgs???

Probably not, since you have completely ignored every posting rule and suggestion we've pointed at and still
1) won't use code tags
2) won't format your code
3) won't tell us what the problem is, leaving us to figure it out
4) asking us to finish your program for you
5) continually using 'leet speak'

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

can u send me the correct prog...
i want to see how it really works

No. Read this

sir i am still amaeture could u please helpme in this prg.

Read this too

this is the first time ..
can u telll me what is my mistake

if i write
for(i=1;i<=4;i++)

may be then it will work.
what do you say

Depends on what you want the program to do...

We are still waiting for an explanation of your problem. Rather than beg for help, explain what your program is doing wrong, and what it should do correctly.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Aia, what did you just post? No explanation, no formatting, improper programming practices ( void main() , getch() , clrscr() )
You should model good practices and proper techniques. You seem to have been here long enough to know this... ;)

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
switch (xactCode)
        {
        case 'D': case 'd':
            bal += xactAmt ;
            depAmt += xactAmt ;
            totDep++ ;
        case 'I': case 'i':
            bal += xactAmt ;
            intPaid += xactAmt ;

        case 'W': case 'w':
            bal -= xactAmt ;
            wdAmt += xactAmt ;
            totWd++ ;
            if ((bal < MIN_BAL) && (svcChgd = false))
            {
                bal -= SVC_CHG ;
                svcChgd = true ;
            }
        default:
            outfile << endl << "!! INVALID TRANSACTION CODE !!" << endl << endl ;
        }

Look up the requirements for a switch/case segment. You have a very important piece missing in each case .

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

If you're going to use char arrays, you might as well be using C. And no, I know it's not your fault, so I blame your teacher for that.

Seems to me the concept here is to learn to process c-strings, because they are necessary in C++.

earlyriser, stop using any and all functions that have a definition of string and use only char* functions. Help us save Joe's sanity. :)

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

... but it stop reading at the first line and not jump to second line. Could you help me at this point?
thank you!

Since the only code you posted above is ADragon's code, and his works fine, we have no idea what's wrong with yours.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

fscanf() does not differentiate between rows. So you will be better off not using that function at all. Instead, I would use fgets() to read one row at a time then parse the string that was read.

char line[80];
char *ptr = 0;
int i = 0;

while( fgets(line, sizeof(line), f) != NULL)
{
   // clear the array
   memset(v,0,sizeof(v));
   i = 0;
   // extract numbers from line string
   ptr = strtok(line," ");
   while( ptr != NULL)
   {
       // insert into array
       v[i] = atoi(ptr);
      ++i;
      // get next number from the string
      ptr = strtok(NULL, " ");
    }
    // now do something with these numbers

}

From a purely style issue, I would not use memset() nor strtok() . memset() is not necessary because you should be using only the array values you've loaded so you shouldn't have problems with uninitialized variables. As for strtok() I prefer to parse the values by hand with loops. For example:

do
{
    fgets(line, sizeof(line), f) != NULL)
    n = 0;
    // skip leading whitespace
    while (isspace(line[n])) n++;

    i = 0;
    do
    {
        // extract numbers from line string
        v[i] = atoi(&line[n]);

        // skip to next value
        while (!isspace(line[n])) n++;  // skip over this value
        while ( isspace(line[n])) 
        {
            if (line[n] == '\n')  break;  // isspace() sees \n as whitespace
            n++;  // skip over the spaces
        }
    } while( line[n] != '\n')

I feel I have more control over the input by doing essentially what strtok() does anyway. As I said, purely a …

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Do you know what arrays are?
Do you know the if statement?
Do you know the for statement?
Post your code after reading this and we can help.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

First of all, what did you do to get the code to print with colors like you did?

And has anyone explained code formatting? Indentation is necessary to understand code.

The problem is you don't seem to know how to test for prime numbers.
You actually have to test the number to see if it's prime from 2 up to the number (shorter is possible, but not yet). You can't just test if it's divisible by 2 and 3. There's also 5, 7, 11, etc.

And after you test the value you increment the number you input, so if you entered 22 you end up testing 22, 23, 24, 25, ..., infinity.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

OK. So use the information from my post and think about how you'd have to do it.

Or read the link from Dave. Two possible ways to handle this.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

But I don't know how to read file "data.inp" line by line. I think I should use fgets, but seem that fgets is used for char.

Yes, use fgets() to read the line, then you can use sscanf() to convert the line into numbers.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

hm i changed the code and it complies now, but when i try to inter a number, an error appears

You must give us details. "An error" can be any of 300+ things.

Explain fully
1) what is wrong
2) where the problem is
3) what it should do instead
4) cut and paste errors (don't paraphrase them)

That way our psychic powers don't have to be used. ;)

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

U tht r typng lk ths need 2 read this now! TY

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Yeah, what Ravalon said. You can check out this series for deeper understanding of why we say "stop using scanf() "

Also, there is absolutely nothing C++ about what you posted so why the using namespace std ?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

that might be easier to program, but it's not a desirable solution.
If the file gets larger it will inevitably be a LOT slower than modifying the existing file.
It could also easily lead to out of memory errors.

I see your point -- in a professional setting. But

I'm doing a homework for my programming class, ...

tells me your apprehension is unfounded and the OP may be using a technique beyond him at this time. I may be wrong, but it's worth pointing out.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

It would be easier to read youe header record, then read the number of students specified into a student array. After you add a new student to the array, recreate the file by outputting the header and all the students.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

how do you fill an object such as a rectangle with a colour using setfillstyle(); ?? I dont want to use flood fill as i am having more problems with that..

You don't. setfillstyle() simply sets the type of fill. You still need a function that actually performs the filling, like floodfill() or fillpoly() .

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Try outputting a character when you are in hidemouse() to be sure you are in fact executing it.

apurv, when you post something you should at least explain what it does... For all we know, that code could erase your disk. ;)

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Don't know. Have you tested the hidemouse() function outside of this program?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

C'mon people. C++ is C++ whether TurboC Ver 1 or MSVC.NET (for the most part).

There are a few things you have to keep in mind at high level programming but at basic learning it doesn't matter what compiler you use as long as it's solid. Yes the older compilers don't follow the current standards 100% (but neither do current compilers) but what they implemented was looked at when the standards were adopted.

When compiled, the program from TCver1 works the same as DevC++.

And I can guarantee the problem exists no matter what compiler he uses, so please let's concentrate in the OP's problem rather than an upgrade that produces a non-fix.

JMAO :twisted:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
cout << "i Am MALE"<<endl ;

:eek:

He was just hoping... :mrgreen:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

fscanf() will read just one word at a time, which will greatly simplify the solution to the problem.

True, but you learn nothing about how to parse a string, which I would assume is the lesson to be learned here.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I prefer the second form. I personally don't like prototypes in a function body. They get lost too easily.

You also don't need so many lines between code blocks. The farther they are away from each other the harder it is to compare.

SpS commented: Same here: SunnyPalSingh +3
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Is there a better way to compare a character array with a cpp string?

There are other ways, but not necessarily better. For just two characters yours way is efficient. If you had to test 4 or more, a loop might be better.

There are also the C-string str???() functions and C++ strings compare methods. Either of those you can look into.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Are there any suggestions for screwing his computer?

Yeah. Grow up. If you become mature, he won't find it so funny.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Its OK to use Single Command Button,
but not OK to Check for StartTime = 0 .
As StartTime =0 means 12:00:00 AM.
What If the user clicks Stop At exactly that time?

Good point. I'm used to C where the time returns time from a specific date. I forgot VB's time is 24hrs.

Good solution checking the caption... :)

Well.. I'am not trying to Argue with the moderator, but trying to tell one of the rare possibilities.

You can correct any moderator when they are wrong. We're just as human as you. Well some mods are. There are a couple I'm not so sure about :confused:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Or with only one button:

Dim StartTime As Date
'
Private Sub cmdStart_Click()
    if StartTime = 0 then
        StartTime = Now
        cmdStart.caption = "Stop Timer"
    else
        MsgBox DateDiff("s", StartTime, EndTime)
        StartTime = 0
        cmdStart.caption = "Start Timer"
    end if
End Sub

If course you can store the difference in a variable and use it elsewhere, too.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

how should i modify my IF statement in order for the query to execute properly based on the conditions provided?..

You are using the wrong process.
1) Load the query string into the variable (the sprintf() part)
2) Execute the query using some database function. This you didn't do.
3) Now check the return from the database call with the IF