jephthah 1,888 Posting Maven

CodeBlocks is an IDE that can be used with most any compiler. But it is not a compiler, itself. its just a shell. so you need to use CodeBlocks in conjunction with a compiler, and preferrably one that, unlike Turbo C, is tightly bound to the ANSI standard.

I think your problem is that you may be using CodeBlocks without having a compiler associated with it, and it's attempting to default to a GCC compiler.

Since you dont have a GCC compiler (i assume you're on Windows), your code is not being recognized.

here's the quickest way to get started:

Since (I assume) you're on a Windows machine, get the MS VisualC++ Express Edition free compiler (download here) and use it as the preferred compiler in CodeBlocks. you can select which compiler is being used from the toolbar "SETTINGS -> Compiler and Debugger" menu

then quit using any function that requires libraries -- like the dreaded "conio.h", "dos.h", or even "windows.h" -- and constrain yourself to writing code using only standard ANSI C. short-term difficulty, yes, but long-term rewards.

even then, you'll probably still have some problems porting code you've written against the MSVC compiler to a real ANSI compiler like GCC, but it the problems will be a few minor details rather than a ton of major code rewrites.

now the only problem with this setup I've described is that I dont think the free (express) version of MSVC has …

jephthah 1,888 Posting Maven

global variables are evil.

most beginners don't realize this, because in short little school programming assignments , no one cares.

it becomes a major problem is "the real world" when people start relying on global variables as "duct tape" for poor program specification and implementation.

because in the real world you have tens- or hundreds of thousands of lines of code that are often written by several people, and have to be maintained by many more people and very soon, no one knows WTF all those global variables are for.

jephthah 1,888 Posting Maven

hey prabkar

i posted my attempt at solving the Knights Tour. code and Windows-based executable are here

http://www.daniweb.com/forums/post609519.html#post609519

jephthah 1,888 Posting Maven

focus on "findAvailMoves" and "bestWarnsdorff" subroutines.

that's where the action is.

jephthah 1,888 Posting Maven

Hey Buddha.

here's an implementation of the Knight's Tour i wrote in C. the algorithm is a simple one (Warnsdorff). it's a couple hundred years old, so it's not plagiarism to use it, i dont think..

at any rate, it solves any position for "open" and "closed" solutions. not the best, but it will give you an answer in a second or two. i have additional delay incurred from repeatedly printing to console.

check it out and see how you might apply it to your own C++ problem.

and to the experts: maybe someone can tell me why this algorithm occasionally fails? i thought it was pretty bulletproof, but every now and then it will paint itself into a corner.

here's the program along with with a ZIP of the MSVC-compiled executable and C source

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

enum Direction {
    FwdRight,
    RightFwd,
    RightBack,
    BackRight,
    BackLeft,
    LeftBack,
    LeftFwd,
    FwdLeft
};

void printBoard(char * marks, int turn);
int initialize(char *board);
int convertIndexToRowCol(int index, int *row, int *col);
int convertRowColToIndex(int row, int col, int *index);
int convertNameToIndex(char *name, int *index);
char * convertIndexToName(int index);
int findAvailMoves(int position, char *board, int *moves);
int bestWarnsdorff(int position, char *board, int * movesAvail);
int promptAndReply(char * message, char * response, int responseLen);
void stringToUpper (char *buffer);
void getCommandLineArgs(int argc, char **argv);


int ARG_Debug = 0;  // if command line arg "ARG_Debug" is given, each step
                    // of the move selection process will be displayed

main(int …
jephthah 1,888 Posting Maven

i just realized i forgot the pointer dereferencers. i should know better than to just type code off the top of my head without testing it.

sorry for any confusion it caused anyone who tried to use it.

here's the correct version:

void stringToUpper (char *buffer)
// converts any lowercase characters in a string to uppercase
// leaves non-lowercase and non-alpha character as they were
//
// input:  pointer to buffer containing original string
// output: pointer to modified buffer, original string is overwritten

{
    char * buffer_ptr = buffer;
    size_t buffer_len = strlen(buffer);

    while(buffer_len--)
        *buffer_ptr=toupper(*buffer_ptr++);
}

.

jephthah 1,888 Posting Maven

it worked in Turbo C, because Turbo C is riddled with undefined behavior and non-ANSI libraries in standard distribution. It's the AOL of C compilers. In other words, it's a joke. Do yourself a huge favor and quit using it.

anyhow.... if you're intent on keeping "header" declared as pointer, then do exactly what Dragon said. Personally, I don't see why you need it to be a pointer since it's local to main. You could more easily declare "header" directly, and write it without all the pointer mechanisms and memory allocations (which has its own additional dangers).

but either way

.

jephthah 1,888 Posting Maven

then you're probably the smartest one here

:)

jephthah 1,888 Posting Maven

[edit] this doesn't belong on this board :) [/edit]

and neither do those $99.00 links :)

.

jephthah 1,888 Posting Maven

what, does everyone here have an ACM membership?

am i the only one who doesnt?

because i was interested in your links, so i went to check it out (the one about Hamilton Paths)

and well, guess what? I need to pay $99.00 in order to access your article.

so, instead of you getting all clenched up at the slightest bit of justified criticism, maybe you ought to consider your audience when you post links.

I mean, yeah, I could go and post all sorts of links to IEEE peer-reviewed journal articles... but i dont. ... for obvious reasons.

and by the way, great way to introduce yourself. welcome to the community.

.

jephthah 1,888 Posting Maven

too bad most people on here don't have membership to ACM.

hacker9801 commented: gtfo troll +0
jephthah 1,888 Posting Maven

This is my project but i cant do it . Pls help me

how did you get this far in life?

i ask in all seriousness, because surely this is not the first computer programming assignment you've been thrown. you must have done something prior to this.

which then begs the question, how many previous assignments have you just cut-and-pasted from the internet? and what you think you're going to do at a job interview?

it's posts like these just make me be sad ... and then i be all like :(

but you know, you can still make me be glad ... just post your program that you've got so far, and ask a semi-coherent question as to why _________ doesn't work like you think it should.

and then i be all like :)


now don't let me down, mmkay?


.

jephthah 1,888 Posting Maven

everything is urgent, it seems. people need to start posting their questions with subject headings like:

"Help Help!! Life or Death Problem Here!!!"

or maybe

"OMG MY ASS IS ON FIRE AND MY HEAD IS A-CATCHIN', SUMBODY PLS ANSWER MY Q!!!"

otherwise, im gonna get bogged down trying to answer all these merely "urgent" questions.

.

jephthah 1,888 Posting Maven

yes, you're absolutely right Prabakar.

funny thing is, i looked at it and didnt even notice. I was thinking how such a conversion could be applied incorrectly -- like what happens when you try and make an EOF or CR or LF or NULL character "uppercase"

I suspect Nick was thinking of converting decimal values to ASCII, and had a late-night brainfart.

that's what i claim when i do stuff like that, anyhow.


.

Nick Evan commented: You're right, my brain misfired +6
jephthah 1,888 Posting Maven

y'all have to excuse me, mmkay? i been off my meds for a couple months now.


.

jephthah 1,888 Posting Maven

oh, and by the way.

welcome to Daniweb.

:P

jephthah 1,888 Posting Maven

You know, I was going to help you, because this is an easy problem...

But then i get here and see not a single code block or conditional statement is indented. I mean, look at your code, look at all those opening and closing braces lined up like little tiny firing squad victims against a wall.

Does this look good to you? Does this look readable? And you're not the only one, by any stretch ... this happens all the time. What is wrong with you people?

More importantly, what is wrong with your instructors? What kind of professional lets you get away with writing code like this?

Or am i just going insane? Can you hear the sound of my head banging against particle board?

.

jephthah 1,888 Posting Maven

meh. nm.

jephthah 1,888 Posting Maven

i dunno, dude. it sure sounds suspiciously like a google bomb to me... not that google hasn't already dealt with about a million such attempts already.

but okay, let's say it is for the legit task you describe.

my First recommendation to you -- as a self-described beginner -- would be to consider using Perl for this sort of work. theres stuff already written in Perl to do exactly what you describe.

my Second recommendation to you is (no matter which language you choose) to start coding, get stuck, then come back for specific help

people here generally will not write your code for you. sometimes you get lucky, but not on this kind of project.

so, get started.

.

jephthah 1,888 Posting Maven

your first comment

"If you want your code to be maximally portable, it's better not to use extensions."

i believe is the most correct.

at work i use National Instruments' C compiler (LabWindows/CVI), and they have a ton of useful libraries


but they're a double-edged sword. they make life easier for the moment, but most of which will never port to either GCC or MSVC, and i become dependent upon them at my own peril. God forbid i have to develop a GUI front end outside of CVI.

jephthah 1,888 Posting Maven

Also about fflush(stdin), i've been tought to use to to flush any 'extra' info that maybe accidentally added to any variable inorder to get a correct input, but since that is bad practice, i will have to learn to handle them correctly like you said :)

its not just bad practice, its totally wrong. it's not me who's saying it either, it's in the ANSI C standard.

look, you can flush an output buffer; it sends the buffered data to the stdout device (terminal) and so those bytes are "processed" or "handled" correctly. the only change is in the timing: you manually forced them out of the buffer before it would have naturally gone on it's own.

there's no analogue to flushing an input buffer. Where will you flush it to? Back out through the stdin (keyboard) device? Will the keys somehow untype themselves? Of course not, but the other option is even worse: instead of processing these bytes correctly (such as fflush(stdout) does), it just deletes them?? Whatever you want to say about these bytes, they are still part of the user input. Who's to say if they are valid or invalid, when you don't even know what they are? the bytes need to be handled correctly, not blindly ignored.

ANSI C specifically states that you can not fflush a stdin buffer, and that such an event is undefined and not intended to be performed with the fflush command.

If your compiler supports such a …

jephthah 1,888 Posting Maven

this is a job for regular expressions.

try using the Regex++ library for C++

http://ourworld.compuserve.com/homepages/john_maddock/regexpp.htm

jephthah 1,888 Posting Maven

yeah ... i'm just not seeing where the "Urgency" is here

i thought someone was bleeding on the floor.

Salem commented: Indeed. +16
jephthah 1,888 Posting Maven

here's your problem...

if (rooms[i]==NULL )
		  	  {
		  	  	rooms[i] = &p;
		  	  	callDoctor(tree,i);
		  	  	[b]i = 5;[/b]
		  	  }

you just set index i=5... your array only has 5 elements, 0-4.

so when you try to access room[5], it craps out on you.


.

jephthah 1,888 Posting Maven

um, trying to exploit Google page rankings is a significant problem even for experts.. as a "beginner", you'll be better served trying to program a Sudoku game or something...

but, if you're really intent on trying to hack some websites, most SkriptKiddi3Z prefer Perl for this sort of task...


.

jephthah 1,888 Posting Maven

nick's first suggestion is good for a single character, but you'd have to write a function to convert an entire string.

something like:

void changeBufferToUpper (char *buffer)
{
    char * buffer_ptr = buffer;
    size_t buffer_len = strlen(buffer);

    while(buffer_len--)
        buffer_ptr=toupper(buffer_ptr++);
}

as for nick's second suggestion, buffer += '0'; /* add 48 to the char */ ... that will work for the case described, but will likely cause your program to blow up.

i mean, im sure nick only meant to suggest this for the case where the character being modified is always and forever a lowercase letter... but that's not necessarily true. To do it this way, you'd have to employ additional and significant checking on the char beforehand. which is what "toupper" does for you.

jephthah 1,888 Posting Maven

umm yes?? What does that mena?

it means i looked through your entire post and couldn't find a single question.

jephthah 1,888 Posting Maven

"fgets" is essentially a simplified version of "fread".

"fgets" is good for (and should only be used for) reading character strings from an input stream, be it a file or the stdin device.

"fread" is suited for any data type, such as binary (hex) data. it gives you more rope to hang yourself with, so if you're just wanting to read ascii text, stick with "fgets"

http://www.cplusplus.com/reference/clibrary/cstdio/fread.html
http://www.cplusplus.com/reference/clibrary/cstdio/fgets.html

jephthah 1,888 Posting Maven

>> 4. What will be printed if the user enters “Wingding”?

well .. what happened?? if you used scanf, i'll bet it went to crap real quick. which would not be an example of "robustness".


>> 7. Give a set of values that will test the normal operation of this program segment.

I'm pretty sure they don't want you to say "all values from 0 to 100 are valid" -- i mean, that's obvious; it's in the program specification. And you cant merely go pull values out of the air that "look good"... there needs to be a rationale as to why you pick some but not others.


>> 8. Defend your choices.

so yeah, the point here is NOT to just select all possible values... the point of software testing is that in most every real world application, you just can't reasonably test every possible combination of inputs... you'll likely have the time and resources to only test a small fraction of all possible inputs, so you have to intelligently direct the test cases to exercise some inputs but not others.

at the very least you need to select all the boundary cases, and the numbers on either side of those boundaries. because these are the cases at which typical programming errors occur.


>> 9. Give a set of test values that will cause each of the branches to be executed.

you have …

jephthah 1,888 Posting Maven

also, before i forget:

NEVER use "fflush(stdin)"

it's wrong. in fact, it's meaningless. if it "works" for you right now, then your compiler is wrong, and if you start relying on it in your programming, you will have your ass handed to you.

learn instead to handle your input stream correctly in the first place an you never will need the concept of FFLUSH STDIN... whatever the hell such a concept even means. i truly don't know.

.

jephthah 1,888 Posting Maven

first off, start indenting your code blocks.

you're setting yourself up for failure if you cant easily differentiate where nested conditional blocks start and stop.

it's also annoying as hell to try and read someone else's code who doesn't indent.

void add()
{
   clrscr();
   int item_code = 0;
   char item_name[80];
   int qihand;
   int item_quantity;
   float item_cost;
   
   printf("Addition Page Test");
   FILE *quantity;
   
   if((quantity = fopen("C:\\TEST\\XYZ\\quantity.dat","a+"))==NULL)
      item_code=1;
   else
   {
      do
      {
         fscanf(quantity,"\n %i %[^/]%*c %i %f", &item_code, &item_name, &qihand, &item_cost);
      } 
      while (!feof(quantity));
      item_code+=1;
   }
   
   printf("\nItem Code : %i", item_code);
   printf("\nEnter Item Name :");
   gets(item_name);
   printf("\nEnter The Quantity :");
   scanf ("%i",&qihand);
   printf("\nEnter The Price Per Unit :");
   scanf("%f",&item_cost);
   printf("\nRecord Added To Database");
   fprintf(quantity, "\n %i %s %i %.2f", item_code, item_name, qihand, item_cost);
   fflush(stdin);
   getch();
   
   fclose(quantity);
   
   menu();
}

now off the bat, line 20 looks suspicious. i can't be certain because i have no idea what your text file looks like, but putting the '\n' in front may cause trouble. its unusual style at least. but maybe it works okay. i cant run it since i dont have your text file to test against.

anyhow, as to your method ... i think a better (as in simpler, and more consistent) way would be to open the file for "r" read permission only, use a conditional while loop to "fgets" read each line and actually look at the "item code" field ... keeping track of the highest value read, and possibly allow some checking to handle irregular sequences found.

jephthah 1,888 Posting Maven

well, it is good for that sort of work.

it's also my preferred tool, if only because (1) i already know the language and am familiar with it (2) it's just so easy to get something slapped together and actually working...

if i were just starting out, i might look into Python as an alternative, but I can't deny that Perl is a really powerful and easy-to-learn tool for exactly that kind of work.

jephthah 1,888 Posting Maven
#include <stdio.h>
#include <time.h>

int main ()
{
    time_t unixTime;
    FILE * fp;
    char stamp[32];

    unixTime = time (NULL);
    fprintf (stamp,"timestamp: %ld\n", unixTime);
    if (fp = fopen("myfile.txt","a")) 
    {
        fputs(stamp,fp);
        fclose(fp);
    }
    return 0;
}

f you want to know what "unix time" is and how you can make it more meaningful to the average person... look up "time.h"


.

jephthah 1,888 Posting Maven

aha

you just signed up to this site in order to post that

religious discussions, indeed :P

jephthah 1,888 Posting Maven

um... yes?

jephthah 1,888 Posting Maven

FOR or WHILE

as in,

for (count=1; count<= MAX; count++)
{
     <stuff>
}

...

while(count <= MAX)
{
     <stuff>
}

the FOR loop will perform <stuff> MAX number of times before continuing with the rest of the program. note that the variable "count" is incremented once each time the loop is executed, until it exceeds the value MAX. "MAX" doesnt have to be a variable. it could simply be hardcoded as 10 or 20 or 1000 or whatever; i just put it as a variable for illustration.

WHILE works similarly, but in this case the variable "count" is modified somewhere in the block of the code (<stuff>)... make sure that "count" actually can become greater than MAX, or you will be stuck in the while loop forever. infinite loops are a common programming bug.

WHILE loops are often used with various conditions such as

while(reverseCount > 0)

or

while(flag != 1)

etc.


.

jephthah 1,888 Posting Maven

on the one hand i probably complain too much. on the other hand, after a long day at work, i find this coding style with no whitespace, no indentions, no syntax, no nothing, to be unreadable in the most annoying way.

anyhow, sockaddr and sockaddr_in are two different structure types. try this: if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) .

jephthah 1,888 Posting Maven

if you'd put CODE tags so we could READ it... oh i dunno, maybe we could see what LINE 41 is.

and "bzero"? holy early-1990's, batman.... do you not like "memset" for some reason?

.

jephthah 1,888 Posting Maven

the point was we did skip those things -- multiple times -- before finally deciding that we really didn't care after all.

you however, were smart enough to keep on clicking away and finally get that message that, unfortunately, you just weren't quite smart enough to understand.

so i guess it averages out that you're fairly mediocre?

:P


.

jephthah 1,888 Posting Maven

i'm glad it works for you, but those aren't good arguments.

okay, YOU may be (or think you may be) faster, but may the gods help the poor slobs who have to go behind you after you leave your job. what typically happens in these circumstances, is maintainers hack at your original code for a while, then eventually it's thrown away and rewritten from scratch in a language the rest of the world understands.

two other observations I'd suggest may be applicable:

(1) IMO, the reason why you "struggle" so much with concepts in C is exactly related to the fact that you learned Assembly first, and now have issues (emotional, mostly) in trying to unlearn those habits that lend themselves so poorly to higher-level languages.

(2) I'd also say your perception of relative speed is either invalid or is an isolated case. All other things being equal, C programmers are necessarily faster than Assembly programmers... so I'd suggest that one or more might be the case here: (a) your friends kind of suck (b) you're something of a demigod among men (c) the applications you're writing are out there in the ~1% periphery where Assembly really might be a better choice ....


.

jephthah 1,888 Posting Maven

thats one of the most godawful implementations of a stack ive ever seen.

a stack is supposed to be simple:

PUSH and POP.

that's it.


.

jephthah 1,888 Posting Maven

since he's happy with his own "solution", i think it's safe to say he's not looking to improve/fix it -- hence, my earlier shotgun metaphor.

theres another metaphor that applies here, it starts with "you can lead a horse to water..."


.

jephthah 1,888 Posting Maven

indeed, they are.

jephthah 1,888 Posting Maven

well, yeah, i get your point, but ...

Fortran programs are still out there chugging away, but that doesn't mean Fortran isn't dead, or that it would be worthwhile for the average person to bother learning it.

i'm just saying Perl appears to be heading to the graveyard.

what does Perl do that Python doesn't do at least as well or better?

jephthah 1,888 Posting Maven

CORRECTION:

perl 5.10 has been out for a while.

but still. version 5 was released in 1994.

theyve been talkign about releasing version 6 "any day now" for the past 8 years.

jephthah 1,888 Posting Maven

so... whatchyallthink?

i notice that this forum gets a tiny burst of activity about once a week. before posting this thread, the last couple posts here were made 9 days ago. then the next few were 17 days ago. and this pattern seems to be the same with other multi-language programming forums, that i've seen.

besides this anecdotal observation ... 5.8 has been around now for how long? 8 years? after the last announcement (that i recall) in 2005, that 6.0 was "about to be released"... it's hard to believe anything that anyone involved in this project says anymore. nobody seems to know what they're even doing.

I think it's safe to say that Python has for some time now been a complete system to replace Perl ... and probably better.

the only point in using Perl anymore seems to be that of personal preference or, more accurately, "emotional attachment"

What's it going to take for the tech community to finally let Perl go?

.

jephthah 1,888 Posting Maven

And rep points don't count here in the coffee house.

so, if i get positive or negative points on a post here in the geeks lounge, it wont add or subtract from my overall "reputation points"... is that right?

jephthah 1,888 Posting Maven

HAY GUISE

there's a CHICK

with a NICE PUSSY

THIS HAS NEVER BEEN DONE BEFORE

HA HA, I'M ON TEH INTERWEBS!!!!1

majestic0110 commented: haha! good point! +2
twomers commented: This is a serious issue and must be taken seriously! Hai to you too. +5
jephthah 1,888 Posting Maven

could you explain how to create a delay of exactly 20us in Keil C for the 8051, if my two timers are already used?

you cut-and-paste a teeny-tiny little delay macro that already exists in a million places on various 8051 boards, tweaking it if necessary.

as for the remaining 99.9% of your embedded code, you continue programming in C.

that's how you do it.

what you DON'T do, is become an Assembly Guru and learn every deprecated assembly programming technique from the early 1990's.

jephthah 1,888 Posting Maven

no leet please

um, that's not anything remotely related to "leet", FYI...

it is merely an acknowledgment of the absurdity of a thread that is itself a spoof on a most noobtastic accident

:P


.