Solved

Outout prog 1 as input prog 2 & preform operations.

Posted on 2004-04-10
46
331 Views
Last Modified: 2010-04-15
I have one code below. Im trying to send the putput of this code as the input of a second program. Prog 2 should read this prog 1, and ive the answer.

Like, 1st line in Prog 1: 10+0. Prog 2 will output 10 [It reads 10+0 from Prog 1 and send it on the command line]. I have only been able to code prog 1 and the beg 3 lines of prg 2. Any ideas..suggestions..help?

******************************
//PROG 1

#include <stdio.h>
void main()
{
      for (int i=0; i<=9; i++)
      {
            switch(i%4)
            {
                  case 0:
                   printf("%d + %d \n", i,i+10);
                  break;

                  case 1:
                   printf("%d - %d \n",i,i+10);
                  break;      

                  case 2:
                   printf("%d / %d \n",i,i+10);
                  break;

                  case 3:
                   printf("%d * %d \n",i,i+10);
                  break;
            }
      }
}
*****************************************8

//PROG 2

#include <stdio.h>
#define SIZE 100


int main (void)
{
      float line[SIZE];
      while (fgets(line, SIZE, stdin) != NULL)      
      {
            float x;
}
****************************************8

0
Comment
Question by:newguy2004
  • 13
  • 13
  • 12
  • +3
46 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 10796255
from what i gather prog 2  will get a string which can look like 10+0

so your second program would need to parse this string of

10+0 into a integer followed by a '+' and another integer etc


you can connect the two programs using "pipes"

but parsing  the string  you have to use something else.

/abhijit/




0
 
LVL 11

Expert Comment

by:avizit
ID: 10796279
if you just want to connect the two programs such that the standard output of first is connected to the standard input of second   you can just use "pipe" and for pipes read the first response in

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20948442.html


/abhijit/
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10796294

An example

// FIrst Program

#include <stdio.h>

void main () {
   int i = 10 ;
   printf ( "%d", i ) ;
}

//Second Program

#include <stdio.h>

void main ( int argc, char *argv[] ) {
    int i ;
    for (  i = 0 ; i < argc ; i ++ ) {
       printf ( "\n%s", argv[i] ) ;
    }
}


run on command line this way

d:\first || second

and see the output

Amit
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10796299
Another solution is write the output of prog1 to file and read the file from prog2.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10796309
Hi Amit,

On executing that on the command line in DOS,i get syntax error.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10796314
Hi newguy2004,

You cannot do a fgets(line, SIZE, stdin) and read what exists on screen
That would ask you for more input.

A messy way to do this is directly read from VDU memory by reading memory locations from
0xb8.

void main()
{
char far * scr = (char far *) 0xB8000000; /* Casting the address is important */
clrscr();
printf("1");
//*scr='1' here.To read next value,do scr+=2;
}
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10796320
Hi ankuratvb,

It works perfectly fine for me
Here is my dos prompt

d:\>
Below is what i execute
first || second

Below shown is the output
10
D:\SECOND.EXE


Amit

0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10796332
I'd still recommend writing to a file from prog1 and reading it from prog2.
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10796339
If he is working on Unix, then he could use the various IPC's OR named pipes (the simplest) to achieve the same without storing th output into a file & then reading it back

Amit
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10796341
d:\>
Below is what i execute
first || second

Below shown is the output
10
D:\SECOND.EXE

I guess what the OP wants is that the value 10 should be accessible in prog2.

In ur output above,the 10 is printed from the output of prog1
The next line,the d:\second.exe is the argv[0] value of prog2 but the 10 is not passed as a argument to prog2 so what have we achieved?
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10796343
The same solution with the same command execution as posted above works for Unix as well

Amit
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10796345
I should say the files suggestion would be a good portable solution to this(will work across all platforms)
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10796369
Hi ankuratvb,

Thnks for your comment
I really didn't realise that 10 is printed from first program & it is not available in second program

Anyway, the files solution would work on all systems
The Named Pipes solution works on Windows and Unix/Linux (AFAIK)

Amit
0
 

Author Comment

by:newguy2004
ID: 10796410
Well, im using MS VS 6.0 somehow i cant the command line feature there. Anyways, ill have to use another software for that then.
As for the prog 2,  have to implement using these features:
a) It reads lines from stdin with problems on them and solves them.
b) It only has to compute ordinary arithmetic operations.
c) It will compute them from left-to-right without worrying about traditional operator precedence.

Example of Prog 2 outout is really like this:
Line 1's value is 10.
Line 2's value is -10.
Line 3's value is 24.
.....

Im not sure about pipes, never used them before, so have to research on them a little so i know what im doing. The easy part is that I have a string tokenizer object already coded.  Thats why i was trying to use the fgets(). But was lost from there on.
Here is the string tokenizer():

********************************
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

const int   MAX_LINE_LEN            = 256;
#define          DEFAULT_IGNORE_CHARS      " \t\n"

class      string_tokenizer_class
    {
    char*      string_beginning;
    char*      string;            
    const char*      chars_to_ignore;
    char      local_space[MAX_LINE_LEN];
 public :
    string_tokenizer_class  (const char* new_string,
                       const char* new_chars_to_ignore = DEFAULT_IGNORE_CHARS
                      ) :
                      chars_to_ignore(new_chars_to_ignore)
      {
      string_beginning = string = strdup(new_string);
      }

    ~string_tokenizer_class ()
      {
      free(string_beginning);
      }
    const char*   get ();
    };


const char*      string_tokenizer_class :: get ()
    {
    while  ( strchr(chars_to_ignore,*string) != NULL )
      {

      if  ( (*string == '\n')  ||  (*string == '\0') )
          return(NULL);
      string++;
      }
    if  (*string == '\0')
      return(NULL);
    int      index = 0;
    if  ( isdigit(*string) )
      {
      bool      have_seen_decimal_pt = false;
      do  {
          local_space[index++] = *string++;
          if  (*string == '.')
            {
            if  (have_seen_decimal_pt)
                break;
            have_seen_decimal_pt = true;
            }
          }
      while  ( (isdigit(*string) || (*string == '.') )  &&
             (index < (MAX_LINE_LEN-1))
             );
      }
    else if  ( isalpha(*string) )
      {
      do  local_space[index++] = *string++;
      while  ( (isalnum(*string) || (*string == '_') )  &&
             (index < (MAX_LINE_LEN-1))
             );
      }
      else
      {
      local_space[index++] = *string++;
      }
    local_space[index] = '\0';
    return(local_space);
    }
******************************
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10796452
>Well, im using MS VS 6.0 somehow i cant the command line feature there. Anyways, ill >have to use another software for that then.

I dont think that will work.

>As for the prog 2,  have to implement using these features:
>a) It reads lines from stdin with problems on them and solves them.
>b) It only has to compute ordinary arithmetic operations.
>c) It will compute them from left-to-right without worrying about traditional operator >precedence.

If from 'it reads lines from stdin',you mean the output from prog1,then
you cant read whats on screen by reading from stdin.
If you give a fgets(line,SIZE,stdin),it will work like gets and ask you enter a line of text.

What you can do(since i have no idea about pipes) is write the output of prog1 to file as well.Then in prog2,read from the file.
You can use fgets to read from file.
e.g.

//PROG 1

#include <stdio.h>
void main()
{
  FILE *f;
  f=fopen("file.txt","w");
    for (int i=0; i<=9; i++)
    {
          switch(i%4)
         {
              case 0:
               printf("%d + %d \n", i,i+10);
               fprintf(f,"%d + %d \n", i,i+10);

              break;

              case 1:
               printf("%d - %d \n",i,i+10);
               fprintf(f,"%d - %d \n",i,i+10);

              break;      

              case 2:
               printf("%d / %d \n",i,i+10);
               fprintf(f,"%d / %d \n",i,i+10);

              break;

              case 3:
               printf("%d * %d \n",i,i+10);
               fprintf(f,"%d * %d \n",i,i+10);

              break;
         }
    }
 fclose(f);
}
*****************************************8

//PROG 2

#include <stdio.h>
#define SIZE 100


int main (void)
{
    float line[SIZE];
    FILE *f;
    f=fopen("file.txt","r");
    while (fgets(line, SIZE, f) != EOF)    
     {
       float x;
       //read from line[] and process
     }
 fclose(f);
}
****************************************
0
 

Author Comment

by:newguy2004
ID: 10797780
Ok, what if, in Prog 1, i print with printf to stdout. Then i can read lines from stdin? I guess then ur concept of pipes may come in?
0
 

Author Comment

by:newguy2004
ID: 10797785
Also, im suprised, a Microsoft product doesnt have command line feature?
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 10797803
>> run on command line this way
>>       d:\first || second
>> and see the output
>
> Hi Amit,
> On executing that on the command line in DOS,i get syntax error.


Amit's syntax is slightly off.  When piping the output of one program into
the input of another, you use the single vertical bar:

      first | second

0
 
LVL 4

Expert Comment

by:booki
ID: 10797805
newguy2004,

You have several options for what you are proposing.

1. Prog1 outputs to to stdout, Prog2 reads from stdin.  Prog1 stout is redirected to Prog2 stdin.
  - I beleive this is the more flexible approach and is the route taken by many dos/unix/linux/command line utilities.
  - You can also use an intermediate temporary file as in method 2 but the advantage being it is not hardcoded in.

Usage:
    C:\PathOfExes\>  Prog1 | Prog2
              OR
    C:\PathOfExes\>  Prog1 > tempfile.txt
    C:\PathOfExes\>  Prog2 < tempfile.txt

Example:
/////////PROG 1//////////////
#include <stdio.h>
main() {
    printf("10 + 0 \n");
    printf("10 * 1 \n");
}
////////////////////////////////

/////////PROG 2//////////////
main () {
    char line[SIZE];
    while (getline(line, SIZE, stdin) != NULL) {
        printf("Read: %s\n",line);
        // Parse & process line
    }
}
////////////////////////////////
 
2. Writing to a temporary file.  (ankuratvb's post)

3. From Prog2, redirect Prog1 stout to Prog2 stdin, then call Prog1. (a little more complicated than what you're looking for i think, let me know if you want an example tho.)

4. From Prog1, call Prog2, passing the expression on the commandl line.

Usage:
    C:\PathOfExes\>  Prog1

Example:
/////////PROG 1//////////////
#include <stdio.h>
main() {
    char cmd[255] = "cmd.exe /c C:\PathOfExes\Prog2";
    char exp1 =  " \" 10 + 0 \"";
    char exp2 =  " \" 10 * 1 \"";
    strcat(cmd,exp1);
    strcat(cmd,exp2);
    system(cmd);
}
////////////////////////////////

/////////PROG 2//////////////
main (int argc, char *argv[]) {
    char line[SIZE];
    int i;
    for (i=1;i<argc-1;i++) {
        printf("Read: %s\n",argv[i]);
        // Parse & process line.
    }
}
////////////////////////////////
   
Sys_Prog,

>> d:\first || second

This is simply the logical OR of the return values of the two exe's.  Since first returns a false value (void), second is also executed.  If first returned a true value, second would not be executed at all.

Try:
dir || echo dir call failed

dir && echo dir call succeeded

To pipe the output of one program to another use a single |

Example:
 - get a sorted (descending order) directory listing
dir /b | sort /r

b.
0
 
LVL 4

Expert Comment

by:booki
ID: 10797862
newguy2004,

>> Also, im suprised, a Microsoft product doesnt have command line feature?

Click on the "Start" button.
Select "Run"

Type in:
 Win9x
command.exe

 WinNT/2K/XP
cmd.exe

Enter or click OK.

To get help on available commands enter:

help

b.
0
 

Author Comment

by:newguy2004
ID: 10797919
Ok, what would be an example for step 3? Well, too many ways here, have to read through everything and figure out whats the most time efficient way to approach this.
0
 
LVL 4

Expert Comment

by:booki
ID: 10797949
newguy2004,

Use the the first method. Of the 4 separate methods that i have listed, it is by far the easiest to implement.

An example of the third method can be found here (goto the accepted answer):
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20942301.html

b.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10799718
>C:\PathOfExes\>  Prog1 | Prog2

This didnt work.No output.

>      OR
>   C:\PathOfExes\>  Prog1 > tempfile.txt
>   C:\PathOfExes\>  Prog2 < tempfile.txt

This did.But this is pretty much the same as writing to file.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:newguy2004
ID: 10799779
Well, i have to try it out on monday, as the code is not working correctly on my  home machine, MS VC++ 6.0
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10799782
Hi booki,

Your option 1:

/////////PROG 2//////////////
main () {
   char line[SIZE];
   while (getline(line, SIZE, stdin) != NULL) {
       printf("Read: %s\n",line);
       // Parse & process line
   }
}
////////////////////////////////

Shouldnt the getline be fgets instead.
getline takes only 2 parameters,char * and size.unless its a user defined function.
BTW,what output do you get?
I am not getting the correct o/p on my DOS machine.

I just get the value 10 printed from prog1.Thats it.
This is what i did:

// FIrst Program

#include <stdio.h>

void main () {
  int i = 10 ;
  printf ( "%d", i ) ;
}

/////////PROG 2//////////////
main (int argc, char *argv[]) {
   char line[SIZE];
   int i;
   for (i=1;i<argc-1;i++) {
       printf("Read: %s\n",argv[i]);
       // Parse & process line.
   }
}
////////////////////////////////

c:\>prog1 | prog2
10
c:\>
0
 
LVL 4

Expert Comment

by:booki
ID: 10800042
newguy2004,

Sorry here's an update on method 1.  As ankuratvb pointed out use fgets instead of getline.  Otherwise it should work.  I tested under WinXP, Win98, Linux.

Usage:
    C:\PathOfExes\>  Prog1 | Prog2
              OR
    C:\PathOfExes\>  Prog1 > tempfile.txt
    C:\PathOfExes\>  Prog2 < tempfile.txt

Output:
Read: 10

Read: 11

Read: 12


/////////PROG 1//////////////
int main(int argc, char* argv[]) {
      printf("%d\n",10);            
      printf("%d\n",11);            
      printf("%d\n",12);            
      return 0;
}
////////////////////////////////

/////////PROG 2//////////////
#define SIZE 256
int main(int argc, char* argv[]) {
      char buf[SIZE];
      while(fgets(buf,SIZE,stdin)) {
            printf("Read: %s\n",buf);
      }
      return 0;
}
////////////////////////////////


ankuratvb,

The Prog2 you're using is from method 4 and the Prog1 you're using is from method 1.  The Prog2 from method 4 does not expect nor attempt to read anything from stdin.  It expects input on the command line.  So when you enter:

c:\>prog1 | prog2

prog1 puts "10" onto stdout, which is piped to stdin of prog2, and exits.  prog2 with no arguments on the command line does nothing as argc == 1 and the for loop is also never entered.  specifically prog2 never reads from stdin so "10" is left on the buffer.  prog2 exits.  buffer is flushed to console giving output:

10

If you want to test piping stdout of one prog to the stdin of another prog use the example in this post.  Alternatively try this command:

c:\>dir /b | sort /r
   Same As w/o the tempfile
c:\dir /b > tempfile.txt
c:\sort /r < tempfile.txt

"dir /b" gives a brief directory listing.  "sort /r" simply sorts in descending order either the file specified on the command line (in this case none) or whatever is on stdin.

b.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10800094
Hi booki,

Thx for pointing out.
Finally got it working.

BTW,the argc and the argv part of the declaration are never used so we can skip them and have int main()
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10800101
Also,just a small typo in method 4:
>char cmd[255] = "cmd.exe /c C:\PathOfExes\Prog2";

should be:
char cmd[255] = "cmd.exe /c C:\\PathOfExes\\Prog2";

to avoid the escape sequence error.
0
 
LVL 4

Expert Comment

by:booki
ID: 10800105
ankuratvb,
thx.
b.
0
 

Author Comment

by:newguy2004
ID: 10801675
Are you guys using MS VC++? Coz how do use like P1 | P2..or commands like that on VC++ 6.0, or .NET?
0
 
LVL 4

Expert Comment

by:booki
ID: 10801729
newguy2004

>> Are you guys using MS VC++? Coz how do use like P1 | P2..or commands like that on VC++ 6.0, or .NET?

1. Find and copy both exe's to the root directory of C:\

2. Open up a "Dos window".  For insttructions see my second  post... "Click on the Start Button. Select Run....."

3. Enter at the prompt:

    To change the current directory to the root:

    cd \

    To run your programs:

    P1 | P2


b.




0
 

Author Comment

by:newguy2004
ID: 10802510
Cook, thanks. I got the pipe thing to work correctly. Prog 1 is perfect. Only in prog 2, i have a question. Prog 2 only reads the lines from prog 1. If i want P2 to preform operations on Prog 1? I know you guys gave something like this for Prog 1:

printf("%d\n",10);          
printf("%d\n",11);          
printf("%d\n",12);          

But, if i want to keep Prog 1 as:

switch(i%4)
          {
               case 0:
                printf("%d + %d \n", i,i+10);
               break;

               case 1:
                printf("%d - %d \n",i,i+10);
               break;      

               case 2:
                printf("%d / %d \n",i,i+10);
               break;

               case 3:
                printf("%d * %d \n",i,i+10);
               break;
          }

And, be able to get Prog 2 to say:
Line 1 = 10
Line 2 = 11
Line 3 = 12
...

Thats means that Prog 2 should compute the lines in prog 1. What technique do i need to use, presuming im going to use the string code i have listed few posts above.
     
0
 
LVL 4

Expert Comment

by:booki
ID: 10802623
newguy2004,

Use your Prog1 (in your first post) as is. No modification needed. Use my Prog2 as a template.

/////////PROG 2//////////////
#define SIZE 256
int main(int argc, char* argv[]) {
    char buf[SIZE];
    int linenumber = 0;
    while(fgets(buf,SIZE,stdin)) {
        // Parse/extract tokens from buf
        // *note - buf contains " NUMBER OPERATOR NUMBER NEWLINE "
        // Determine result of expression
        linenumber++;
        // Output result to stdout, assuming result is a float/double
        printf("Line %d = %.2f\n",linenumber,result);
     }
     return 0;
}
////////////////////////////////

b.
0
 
LVL 4

Expert Comment

by:booki
ID: 10802678
newguy2004,

>> What technique do i need to use, presuming im going to use the string code i have listed few posts above.

You can use your string_tokenizer_class:

string_tokenizer_class(buf);

Or you can use strtok().
    char *firsttoken, *secondtoken, *thirdtoken;
    firsttoken = strtok(buf,chars_to_ignore); // first number.
    secondtoken = strtok(NULL,chars_to_ignore); // operator
    thirdtoken = strtok(NULL,chars_to_ignore);  // second number

b.
0
 

Author Comment

by:newguy2004
ID: 10802743
Ok two things:
1) Do i even set result = 0, as you didnt declare it in the code.
2) Is char buf[SIZE]; right? Coz it gives a no floating point error. So shouldnt that be a float?

0
 

Author Comment

by:newguy2004
ID: 10802774
Also, for the while statement, how does this sound:

while (fgets((char *)line, (sizeof(float)*SIZE, f)) != EOF)
0
 
LVL 4

Expert Comment

by:booki
ID: 10802785
newguy2004,

>> 1) Do i even set result = 0, as you didnt declare it in the code.

Prog2 is not complete.  You will have to determine what variables you'll need and declare them.  As for result you'll give it a value when evaluate the expression in buf.

Eg. if the operation is addition:

result = firstnum + secondnum;


>> 2) Is char buf[SIZE]; right? Coz it gives a no floating point error. So shouldnt that be a float?

No.  You're reading in a string of chars from stdin.  You need to tokenize the string then convert the first and third tokens to floats and you can probably just do a switch statement on the second token to determine which operation is required.

Eg:

switch (*secondtoken) {
case '+':
 ...
 ...

}

>> while (fgets((char *)line, (sizeof(float)*SIZE, f)) != EOF)

No.  The second parameter to fgets needs to be the size of the char array pointed to by the first parameter.

b.
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 62 total points
ID: 10805260
Hi newguy2004,

To convert your first and third tokens to floating point,u can use strtod():

Here is an example.

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

int main(void)
{
   char input[80], *endptr;//endptr is used for error detection
   double value;

   printf("Enter a floating point number:");
   scanf("%s",input);
   value = strtod(input, &endptr);
   printf("The string is %s the number is %lf\n", input, value);
   return 0;
}
0
 

Author Comment

by:newguy2004
ID: 10810538
Ok cool, let me code prog 2 completely tomorow, and wil see how it goes.
0
 

Author Comment

by:newguy2004
ID: 10817583
Well, how do i convert from const char* to char*?
Thats the error i get:

#define SIZE 256

int main(int argc, char* argv[])
{
    char line[SIZE], *endptr;
    int linenumber = 0;
      double result;
      int first, second;

    while(fgets(line,SIZE,stdin))
      {
            char *firsttoken, *secondtoken, *thirdtoken;
            string_tokenizer_class myTokenizer(line);

            firsttoken  = myTokenizer.get();
            secondtoken = myTokenizer.get();
            thirdtoken  = myTokenizer.get();

            first  = strtod(firsttoken,&endptr);
            second = strtod(secondtoken,&endptr);

                  switch(*secondtoken)
                  {
               case '+':
                         result = (first + second);
               break;

               case '-':
                         result = (first - second);
               break;      

                    case '/':
                         result = (first / second);
               break;

                    case '*':
                         result = (first * second);
               break;
                  }
                  linenumber++;
                printf("Line %d's value = %.2f\n",linenumber,result);
     }
     return 0;
}
0
 
LVL 4

Expert Comment

by:booki
ID: 10817694
newguy2004,

The problem:
    The get method of your class returns a pointer to a constant char, so can either change that so it returns 'unconstant' char * or change firsttoken, secondtoken, etc.. to be const char *.

Solution, change:

>> char *firsttoken, *secondtoken, *thirdtoken;

To:

    const char *firsttoken, *secondtoken, *thirdtoken;


Also a logical problem.  Secondtoken is not the second number.  It is the operator.  Change:

>> second = strtod(secondtoken,&endptr);

To:

    second = strtod(thirdtoken,&endptr);

b.
0
 
LVL 4

Expert Comment

by:booki
ID: 10817802
newguy2004,

Also, consider changing:

>> int first, second;

To:

    double first, second;

This will allow you to get accurate results for expressions such as.

    11.5 + 0.5

Although it appears as though prog1 only uses integers for creating expressions.  If you know for certain that the expression only contains integers than I would suggest you use:

    int first, second;
    ...
    first = strtol(firsttoken,&endptr,10);  // strtol => string to long as opposed to strtod => string to double
    second = strtol(thirdtoken,&endptr,10);



b.
0
 

Author Comment

by:newguy2004
ID: 10818432
Well, it compiles, but the output is weird:

Line 1's value = -92559631349317831000000000000000000000000000000000000000000000
.00
Line 2's value = -92559631349317831000000000000000000000000000000000000000000000
.00
Line 3's value = -92559631349317831000000000000000000000000000000000000000000000
.00
Line 4's value = -92559631349317831000000000000000000000000000000000000000000000
.00
Line 5's value = -92559631349317831000000000000000000000000000000000000000000000
.00
Line 6's value = -92559631349317831000000000000000000000000000000000000000000000
.00
Line 7's value = -92559631349317831000000000000000000000000000000000000000000000
.00
Line 8's value = -92559631349317831000000000000000000000000000000000000000000000
.00
Line 9's value = -92559631349317831000000000000000000000000000000000000000000000
.00
Line 10's value = -9255963134931783100000000000000000000000000000000000000000000
0.00
0
 
LVL 4

Expert Comment

by:booki
ID: 10818497
newguy2004,

Without having looked very closely I'm thinking the problem is in your tokenizer class.

I'll try compiling and running your code.

b.
0
 
LVL 4

Assisted Solution

by:booki
booki earned 63 total points
ID: 10818591

Ok,.. your tokenizer class returns a pointer to a member variable (buffer), so you need to create your own copy of the return value.   You can use the strcpy (string copy) to make your own copy..

Change:
>>     char *firsttoken, *secondtoken, *thirdtoken;
To:
          char firsttoken[MAX_LINE_LEN], secondtoken[MAX_LINE_LEN], thirdtoken[MAX_LINE_LEN];

Change:
>>      firsttoken  = strdup(myTokenizer.get());
>>      secondtoken = strdup(myTokenizer.get());
>>      thirdtoken  = strdup(myTokenizer.get());
To:
          strcpy(firsttoken, myTokenizer.get());
          strcpy(secondtoken, myTokenizer.get());
          strcpy(thirdtoken, myTokenizer.get());

b.
0
 

Author Comment

by:newguy2004
ID: 10818756
Thanks guys, it works fine.
Ive given both of the points almost equally, as both of you really helped me a lot, so i guess both of u deserve the credit.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now