jephthah 1,888 Posting Maven

for windows development, i use the Microsoft Visual C compiler. The "Express Edition" is completely free, and its the industry standard if you're developing PC apps. you can get previous versions (like 2005) if the 2008 doesnt work with your machine.

GCC would be the one for linux/unix

i mean, but hey, if you've got a project due, and you're allowed to use non-portable non-standard libraries like CONIO and BIOS... and you're used to workign with them, then dont change right now

just be aware, that these libraries will continue to haunt you and eat your soul until you exorcise them from your life.

as for my code, its working now just fine. and solves the Knight's Tour from any position on an 8x8 board in a few seconds .. would be a lot faster, but i display the board and moves to a terminal display, and that slows it down a lot.

but i can't just go and give it to you. that would defeat the point of you learning how to write your own code to solve problems.

all i did was use the old Warnsdorff's Algorithm. the algorithm itself is pretty simple, and has been around for some 200 years, to solve this exact problem.


.

Prabakar commented: It was very helpful +1
jephthah 1,888 Posting Maven

i wouldnt use bios.h either. it's just as bad.

why are you using such non-standard libraries, anyhow? is there a reason?

if you were maintaining mountains legacy code, say, on old crusty Win98 machines, i could see the reason behind sticking with it. (i had the misfortune of being in that situation a few years back...)

but if not, you should really drop these borland/windows archaic libraries like a bad habit.

....

Upon further review, i was able to extract your <conio.> dependent code and everything compiled okay.

ive since written an entirely new program from scratch, just to satisfy my curiosity. its not quite bug free, but i did it only using the STDIO and STRING libraries.

jephthah 1,888 Posting Maven

i want to help you, but youre using non-standard C that wont compile on either GCC or MS Visual C

you should never use Borland's CONIO functions.

so im either forced to port your code to something the rest of the world can use, or ignore it entirely and move along.

jephthah 1,888 Posting Maven

"help a little more"

how can we help you if you dont give us any thing to work with.

you cant plop down the description of a problem, and say "help me fix my program to work better" and then not even show us WTF you've been doing?

post your program here.

and use [code] tags. if you dont know what [code] tags are, find out before you post.

jephthah 1,888 Posting Maven

if you're using winXP, you should NOT be using some old crusty DOS compiler

MSVC is given away free by Microsoft.

get it. use it. love it.

jephthah 1,888 Posting Maven

another thing, "miles" needs to be initialized to zero before you start iteratively adding values into it.

just declaring it as a float does not initialize it.

Sh13 commented: Thanks for the help! +1
jephthah 1,888 Posting Maven

Yahooooo!

:P

:(

jephthah 1,888 Posting Maven

Narue, think about it.... really. its a vending machine problem. CSC 101.

do you think his class is going to be expected to understand strtol pointers and how to use the "errno" library?? He's here because he cant figure out how to cast a char * string into an int.

can you even remember learning the basics how to program? you're an expert among professionals, girlfriend.

these people just want their programs to "work". they cant see the larger picture yet. thats why they're here.

im not saying though, that you shouldnt be giving the complex answers.... it is good, as the OP just stated, to see more options. I think its important that you're here to show them (and me!) the "correct" way to bulletproof code.

im just offering the simple answer for simple questions. for clarity.

jephthah 1,888 Posting Maven

hey i agree. i rarely use atoi, favoring strtol instead. and i always error check user inputs in my own code.

i am merely answering his simple question: "why doesnt X work?" with a simple answer: "because you can't use X like that... you must use Y instead."

now if you want to diverge into more advanced topics, and give him rope to hang himself... thats fine too. because if he tries to pass that code off in an assignment, and then gets called on to explain it -- and can't -- well, that's probably karma working like it should.

i mean, maybe he'll successfully study and understand what and why you're doing what you're doing, and learn some really valuable lessons. but, i think, just in my brief experience here with other similar posters, that is unlikely.

whatever. its all good. you're right, and you've already won. I'm scared of your chainsaw, okay? I'm just offering the answer at the same level of the question as it was posed. that's all.

jephthah 1,888 Posting Maven

i'm totally down with everything you're saying. and, technically speaking, you're absolutely right.

i also see value in aia's approach.

MY point was that

(1) you can't do this: value=(int)argv[1]; (2) you must do this: value = atoi(argv[1]); (or similar, with strtol)

the poster obviously has a fundamental misunderstanding of how to get an int from command line argument.

now the place you're going with all your bulletproof errorchecking is just going to confuse the guy until he at least understands how to convert a char * to an int.

i dont think it's helpful to hand a beginner a fully fleshed out production-quality routine if they don't understand the basics.

i say let them send invalid args to the commandline and see what happens. otherwise they're just cutting and pasting your Beautiful Code without understanding what it really does.

jephthah 1,888 Posting Maven

never mind.

i dunno. too late to start

jephthah 1,888 Posting Maven

here's the simple answer:

int main (int argc, char *argv[])
{
    int value;

    value = atoi(argv[1]);

    ...

}

of course that doesn't even begin to address error checking. But that wasn't your question, so I'm leaving it that out in favor of the simple answer as to why your code didnt work (because you cant merely cast a string as an int)

now, lets the fight resume. i got 3:1 on narue. she wields a wicked chainsaw. i still have scars.


.

jephthah 1,888 Posting Maven

global file pointers in a multithreaded environment? no wonder you've got problems. To repeat what i've been saying... this underscores the reason why you shouldnt use global file pointers. you can't keep track of them worth a damn, and god help the person who has to maintain your code a year or two from now.

globals, sparingly used, do have their place ... but they should not be used as a relief for poorly specified functions.

try spec'ing your functions more thoroughly and pass your file pointers as arguments.

jephthah 1,888 Posting Maven

you have an interrupt service routine of some sort.

when you get an interrupt, process the row that had the keypress event by calling the row function according to the particular interrupt. or call all four row functions in sequence if the interrupt is just generic to the keyboard and not row-specific. the return value of the row function will be the character pressed, if one was pressed, or will be an 'x' if no character was pressed on that row.

otherwise you would expect the first character pressed to be a number -- keep a running total of the first value to be used by storing the value of this number.

recall: if a nine is pressed, the value is 9. but if a three is pressed immediately afterward, the value of the previous nine becomes 90, and the three is 3, and combine for the value of 93 ...

if (when) the character input represents a math operation (addition, subtraction, etc.), hold the final total value of the previous number(s) pressed, and store the "fact" that you will be performing the requested math operation after the next value is finalized.

repeat the process for the next set of numerics, to get the second value.

once "E" is returned from the keyboard interrupt, process the final result in this manner

if (operation == 'A')
    result = value1 + value2;
else if (operation == 'B')
    result = value 1 - values2;
else if …
jephthah 1,888 Posting Maven

are you using <TAB> in front of each command? i cant tell since youre not using code-tags.

dont "simplify" it until you get it to work in the first place. go back to the beginning and adhere strictly to your dependency graph

________   ________   ________
| ND.C |   | PD.C |   | MD.C |
========   ========   ========  
    |____  ____|          |
        |  |              |
      __V__V__        ____V___
      | PD.H |        | MD.H |
      ========        ========
                          |
                      ____V___
                      | RD.H |
                      ========
wsn commented: Very helpful gave me a very good hint where I was lost thank you +3
jephthah 1,888 Posting Maven

why are you trying to make your makefile?

the command should just be "make"... should it not?

jephthah 1,888 Posting Maven

yes. you're quite welcome.

jephthah 1,888 Posting Maven

okay.

we'll be looking forward to your return.

jephthah 1,888 Posting Maven

i concur with narue.

the code you are trying to fix is practically complete.

the change you want to make to it is almost trivial.

the code that you have already, is clear and well-written, and follows a good basic style.

if you can't figure out how to make this change to the code you already have... i can't help you.

i can GIVE you the answer, but that would be counter productive.

Ive already given you a concise answer to the PIG LATIN question in the other thread, yet you cant even plop the function call into a main( ) routine.

i feel like im just giving you rope with which you will wind up hanging yourself.

you need to take a refresher class in basic C programming before attempting these more "advanced" concepts.

jephthah 1,888 Posting Maven

this is a complete mess. study this and see why it can not work, then fix it.

do 
{   
   gets(romanString);
   printf("Invalid input\n");
   printf("Please re-enter Roman number:");     

} while(romanString[j]!="I,V,X,L,C,D,M");
        
arabicValue=convert(romanString);
jephthah 1,888 Posting Maven

okay, where did you get the code from?

because I don't believe you wrote it.

jephthah 1,888 Posting Maven

yeah, dude... you can't just take a function like: int convert (char *) and turn it into your main routine

"convert( )" is a function that must be called INSIDE main, like this:

int main()
{
   blah   // setup
   blah
   blah
   blah

   scanf("%s",romanString);

   blah  // error-checking
   blah 
   blah 
   blah

   arabicValue = convert(romanString);

   printf("\nRoman  : %s\n",romanString);
   printf("\nArabic : %d\n",arabicValue);

   return 0;
}
jephthah 1,888 Posting Maven

you cant change my "convert" function to main.

convert must be called BY main.

in main, you must get the string input from the user, do you error checking, and then display results.

jephthah 1,888 Posting Maven

first function depends on second. caller is responsible for error-checking input into "convert( )"

example : value = convert("mcmlxix");
returns: value = 1969

int convert(char *roman)
{
    int arabic = 0, tmp;
    char *str_p  = roman;
    
    while(*str_p)
    {
        tmp = letterValue(*str_p);
        if (letterValue(*++str_p) > tmp)
            arabic -= tmp;
        else
            arabic += tmp;
    }
    return arabic;
}
    
int letterValue(char letter)
{
    if (toupper(letter) == 'I') return 1;
    if (toupper(letter) == 'V') return 5;
    if (toupper(letter) == 'X') return 10;
    if (toupper(letter) == 'L') return 50;
    if (toupper(letter) == 'C') return 100;
    if (toupper(letter) == 'D') return 500;
    if (toupper(letter) == 'M') return 1000;
    return 0;
}
jephthah 1,888 Posting Maven

^ nice try. but you should have your opening tag be like this:

and you need to have all your INDENTATIONS (ie, "tabs") copied into the text box.... otherwise it still looks like shiite and we still can't read it.

but anyhow ... i opened your code, read it, and executed it... and im not sure why you're having a problem.

your code essentially works.

-------------------------------------

welcome to the Piglatin GeneratorType 'End' when finished

hello how are you wont you tell me your name

elloha owha reaa ouya ontwa ouya ellta ema ourya amena

------------------------------------

i mean, you're like 99% there. your code is very clear and straightforward.

are you telling me that you've done all this work, wrote all this very nice code and come this far, and [b]now[/b] you're [b]stuck[/b]?

what exactly are you having problems with? what function are you confused on? what are you trying to do that isn't working?[code=c]
and you need to have all your INDENTATIONS (ie, "tabs") copied into the text box.... otherwise it still looks like shiite and we still can't read it.


but anyhow ... i opened your code, read it, and executed it... and im not sure why you're having a problem.

your code essentially works.

-------------------------------------

welcome to the Piglatin GeneratorType 'End' when finished

hello how are you wont you tell me your name

elloha owha reaa ouya ontwa ouya ellta ema ourya amena

------------------------------------

jephthah 1,888 Posting Maven

i'll look at it...

but it sure would be a helluva lot easier on everyone if you were to just post your indented code between the code tags

you'd be a lot more likely to get people to help you if they can just read your code on the webpage without having to go through a lot of contortions.

jephthah 1,888 Posting Maven

insert your properly-indented code between code tags

[ code=c ]
insert code here
[ /code ]


(except, do not have any spaces within tag brackets... i just put those there so it wouldn't actually turn it into "code" ...)

jephthah 1,888 Posting Maven

heres some more hints:

(1) do not attempt to look for "return" statements. at all.

(2) only use the open/close brace counting method.

(3) you need to change your regex method. merely looking for a brace at the beginning or end of the line WILL NOT cut it -- as you have already experienced.

(4) instead, look for ANY AND ALL braces, with the EXCEPTION of braces that are found within quotations (ie, braces to be printed.

this means that you can not just merely look for and count braces.... you have to look for and keep track of quotation marks ("), but disregard escaped quotation marks that are themselves being printed (\")

jephthah 1,888 Posting Maven

(1) do not just blindly replace = with == , there was only one place where this was an issue if($openCount==$closeCount) (2) SOME functions can have NO "return" statements (void) ... but ANY function can have MANY returns (even void). you can NOT rely on the existence of "return" statements. if you try to, you will inevitably fail.

3) that is the very problem ive been telling you about. the method behind your choice of regex is flawed. this is the root of your problem. you need to think about another pattern by which you can find open and close braces without relying the "Beginning of Line" and "End of Line" operators (^ and $)

4) you probably have some whitespace (one or more spaces and/or tabs) after the brace. this is the same problem that is plaguing you in 3)

5) i dont understand what you mean

jephthah 1,888 Posting Maven

Hulk angry

:icon_mrgreen:

jephthah 1,888 Posting Maven

i know what you're saying, dragon, but it doesn't usually work that way.

components reach end of life, instruments and machines are no longer supported, systems become obsolete, and drivers cease to be made available for their replacements. you just can not physically maintain an assembly line running dos.

last month i just bought a truckload of new industrial computers running XP to replace our ancient win98 machines. we couldn't keep those '98 lines going even if we wanted to, and believe me, management wanted to. 98 is long since end of life.

and we're not manufacturing cutting-edge devices. all our instrumentation automation is via GPIB for chrissakes. we'd run DOS if we could get away with it, for sure.


and... FTR... DOS is not an RTOS. Linux has RTOS capability, and Windows CE is an RTOS. but you dont need an RTOS to run the assembly line you described. its only useful for embedded development.

jephthah 1,888 Posting Maven

Oh, yeah, I'll be the first to admit, Turbo C is preferable. but it costs $$.

whereas the one i posted is a FREE alternative that's usually suitable for average user.

i mean think about it... DOS 5.0? whoever is hanging on to that crap is not likely to shell out much cash for new program development on what is probably non-critical, lowest priority legacy code.

as it is, it appears this is just a school exercise, anyhow

My program is simple so an example on hello world program would do the trick

I say Digital Mars will be entirely sufficient.

jephthah 1,888 Posting Maven

in case it's not clear...

your parsing method will count the opening brace in line 6, but not the closing brace in line 10

jephthah 1,888 Posting Maven

http://www.digitalmars.com/

you have my sympathy.

jephthah 1,888 Posting Maven

glad you got it :)

jephthah 1,888 Posting Maven

what about lithp?

jephthah 1,888 Posting Maven

just because you include the header file doesn't mean you're getting the code. the errors you described earlier sound like you're not linking properly.

the solution depends on your compiler, and how their source code is provided... whether it's in a .dll or a .lib or whatever

jephthah 1,888 Posting Maven

1) i noticed in your earlier code you were using = when you should have been using ==

2) you also had a do statement without a corresponding while condition. avoid do/while's for this and other reasons. you should stick with "while" loops unless you have compelling reason to use "do/while"

3) you should not be "return"-ing in the "else" statement.

4) you should not attempt to define the end of a function by the appearance of "return"... besides the fact that void types do not return a value, a function can have any number of return statements.

otherwise, this sort of works in a basic sense:

use strict;

my $openCount;
my $closeCount;
my $foundReturn;
my $myCfile;

die "usage: perl $0 <filename>\n" 
    if (!($ARGV[0]));

die "file: <",$ARGV[0],"> does not exist\n" 
    if (! -e ($myCfile = $ARGV[0]));

open(fp,$myCfile);
open(writeme,">output.txt");

print "reading <",$myCfile,"> ...\n";

$openCount = $closeCount = $foundReturn = 0;
while(<fp>)
{
    if ($_ =~  /return/)  {
        print "hey am inside return if\n";
        $foundReturn=1;
    }
    elsif(($_ =~ /^\{/) || ($_ =~ /\{$/)) {
        $openCount++;
        print "openCount1=", $openCount ,"\n";
    }
    elsif(($_ =~ /^\}/) || ($_=~ /\}$/ )) {
        $closeCount++;
        print "closeCount=", $closeCount,"\n";
    }
    if($openCount==$closeCount) {
        print "count is equal\n";
    }
}

you STILL have the problem that if a brace is both preceded and followed by any combination of whitespace, comments, or additional code -- which is a VERY likely prospect -- your counting code will become immediately and irretreivably lost.

for instance:

int main …
jephthah 1,888 Posting Maven

for one thing, you cant put the begin/end of line operators (^ and $) inside parentheses. they must be immediately adjacent to the match operator like this

/^  ...regex...  $/

for another thing, you can't put the LOGICAL OR inside a regex. you need to break it down into separate expressions like this

if (($var =~ /^  ...  $/) || ($var =~ /^ ... $/))
{
    do stuff
}

also, can you ever think of a function that will never match the regex

/return/

perhaps a function like this:

void myStupidFunction (void) 
{  
     printf("hi world!\n");
     foo = bar;
}

finally, you can't guarantee that the open or close braces are ALWAYS going to be either at the beginning or end of a line. that would then fail to find any indented brace that had spurious whitespace afterwards, much less a brace that was preceded and followed by more code. like this:

for (a=1; a<10; a++) {     // only call 9 times!
     myFunction(a); 
}

your method (as I interpret it from the apparent intent of your code) will fail to find the opening brace, and only count the closing brace. thereby getting your counting algorithm hopelessly lost.

jephthah 1,888 Posting Maven

lol i can't read hebrew

jephthah 1,888 Posting Maven

it isn't "necessarily" always at the first spot.

but the regex should be correct.

post more code. use tags, please[code=c] tags, please

jephthah 1,888 Posting Maven

strcpy(msg,"hello")

:)

jephthah 1,888 Posting Maven

Try setting a very large new buffer.

yah, that, or maybe just call a flea flicker and have the running back throw a hail mary to the kicker in the end zone.

jephthah 1,888 Posting Maven

i see now whats wrong you missed a couple points.

*msg needs to point /somewhere/... either declared as array[ ] or allocated.

you have to send the "sizeof" your message buffer to the "recv" function... not the length of the string (strlen)

i modified this to work with a public server. adjust your code accordingly

#define EXIT_SUCCESS 0
#define PORT 666            //BOFH Excuse Server
#define IP "193.202.115.241"  

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>

char     msg[2000];
int      sockfd, connresult, recvresult, sendresult;
struct   sockaddr_in server_addr;
bool     Connected;

int main(int argc, char** argv) 
{
    server_addr.sin_family = AF_INET; 
    server_addr.sin_addr.s_addr = inet_addr(IP);
    server_addr.sin_port = htons(PORT);
        
    //Create the Socket
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    printf("sockfd = %d\n",sockfd);
    if (sockfd < 0) 
    {
        printf("errno %d: %s\n",errno,strerror(errno));
        exit(1);
    };
        
    //Connect
    errno=0;
    connresult = connect(sockfd,(struct sockaddr *)&server_addr, 
					  sizeof(server_addr));
    printf("connresult = %d\n",connresult);
    if (connresult < 0)
    {
        printf("errno %d: %s\n",errno,strerror(errno));
        exit(2);
    }    
    printf("Connected to host\n");
      
    // receive
    Connected = true;      
    while(Connected) 
    {
        errno = 0; 
        recvresult = recv(sockfd,msg,sizeof(msg), 0);
        //printf("recvresult = %d\n",recvresult);
        if (recvresult < 0)
        {
            printf("errno %d: %s\n",errno,strerror(errno));
            Connected=false;   
           
        }
        //Handle received messages here
        else if (recvresult > 0)
            printf("%s", msg);
        else
            Connected=false;   
    }
    printf("\n");

    close(sockfd);
    return (EXIT_SUCCESS);
}
jephthah 1,888 Posting Maven

change it to this:

server_addr.sin_family = AF_INET; 
	server_addr.sin_addr.s_addr = inet_aton(IP);
	server_addr.sin_port = htons(PORT);
jephthah 1,888 Posting Maven

wow.

now THAT was one helluva gift, serverdude.

you don't even know.

jephthah 1,888 Posting Maven

as for checking whether or not it is a function... each subroutine in C starts with one or more return type:

int, long, short, double, float, void, char, unsigned ...

then it has a single word for the function name (simply: \w+ )

then it will have an open parenthesis '(' followed eventually by a closed parenthesis ')' ....

and then there will be the opening bracket '{'

thats all you have to parse on to find that it is a function... of course be sure to ignore function "prototypes"... they will be distinguished by a semicolon ';' after the closing parenthesis

the problem for you will be that a function declaration can spread across multiple lines... very often that first opening bracket is on a line by itself. like so:

void myExample_Function (int argument, char * value)
{
    blah;
    blah;
    blah;
}
jephthah 1,888 Posting Maven

easiest way is to

(1) do a dir (ls) listing of directory contents, parse each one for a .c extention. start with the first .c file...

(2) open the .c file for READ, to a file handle.

(3) open a new .c file to WRITE to a different file handle and make it's filename similar to the original one being read from in step 2 (like using same name, but adding a "-mod" after the filename)

(4) read each line of original file.

(5) search for each function, and keep strict count of number of opening brackets, and number of closing brackets ( '{' and '}' )

(6) write each of the the lines read from the original file to the new file, exactly as read.

(7) write an additional "extra line 1" only after the first opening bracket '{', and write teh additional "extra line 2" right before the last closing bracket '}'

(8) repeat steps 5 - 7 until all functions are completed and end of file is reached

(9) close both the read file and write file, and then move to the next .c file in your directory.

(10) repeat steps #2 - #10 for the next .c file, until all .c files are completed.


.

jephthah 1,888 Posting Maven

the problem doesnt seem to be in your code. the problem seems to be that your ip address is refusing the bind.

at least that's the error i get. your's may be different, so try this code and see what error code is returned.

errno=0;
handle = bind(localsock,(struct sockaddr*)&local,sizeof(local));
printf( "bind -- Error #%3d: %s\n", errno, strerror( errno ) );
if (handle < 0) 
{
    fprintf(stderr,"bind command failed\n", errno);
    exit(1);
}

because your code, as written (with the assumption that you included all appropriate headers and gave a meaningful port number) ... the code works. i dont see anything wrong with it.

here was additional debug results from my run:

[jezebel@localhost cprogs]$ ./ipv6test

ipv6 = 2001:0067:0000:0000:0000:0000:0000:0002
inet_pton -- Error #  0: Success

localsock = 4
socket open -- Error #  0: Success

bind -- Error # 13: Permission denied
bind command failed

[jezebel@localhost cprogs]$
jephthah 1,888 Posting Maven

your code is not compiling without a lot of effort on my end, and hence i havent been able to put much time into this while at work.

is this for a server i assume? do you have any more code along with this?