i dont understand why this isnt working ill post the code thats relevent

for some reason no matter what numbers i enter it will just print out that the complex number is 0.00000... idk how to fix it

heres the main function

#include <stdio.h>
#include "globals.h"
#include "complex.h"

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

  complex complex1, complex2, complex3 ;
  double r, i;

  printf( "Complex 1: Please enter two floating point values: " );
  scanf( "%1f", &r ) ; scanf( "%1f", &i ) ;
  complex1 = load_complex( r, i );

  printf( "\nThe complex number is " );  print_complex( complex1 );
  printf( "\n" );

print_complex function

/*******************************************************************/
/* Programmer:  Jonathan Doucette                                  */
/* complex.c                                                       */
/*******************************************************************/

#include "globals.h"
#include "complex.h"
#include <stdio.h>


void print_complex( complex complex1 ){

  printf( "%1f  ", complex1.real ) ;
  printf( "%1f  ", complex1.imaginary ) ;

  return ;
}

and the complex macro

#ifndef _complex
#define _complex

#include "globals.h"

typedef struct complex { double real ; double imaginary ; } complex ;

Recommended Answers

All 8 Replies

theres no way to tell whats going on by the sparse amount of code you've posted.

the problem, i'll wager, is likely in your "load complex" function, whatever that is...

Member Avatar for dmachop

post your complete program and you'll get the solution, it's pretty vague....................

the complete program is pretty large... but ask and you will receive

/***********************************/
/* Programmer:  Dewey                                */
/* complex.c                                                  */
/***********************************/

#include "globals.h"
#include "complex.h"
#include <stdio.h>
complex load_complex( double real, double imaginary ){

  complex a ;

  a.real      = real      ;
  a.imaginary = imaginary ;

  return( a );

}

void retrieve( complex complex1, double *p_real, double *p_imaginary ){

  *p_real       = complex1.real      ;
  *p_imaginary  = complex1.imaginary ;

  return ;
}

complex add_complex( complex complex1, complex complex2 ){

  complex a;

  a.real      = complex1.real      + complex2.real      ;
  a.imaginary = complex1.imaginary + complex2.imaginary ;

  return ( a );

}
complex subtract_complex( complex complex1, complex complex2 ){

  complex a;

  a.real      = complex1.real      - complex2.real      ;
  a.imaginary = complex1.imaginary - complex2.imaginary ;

  return ( a );

}
complex multiply_complex( complex complex1, complex complex2 ){

  complex a ;

  a.real      = ( complex1.real * complex2.real ) - ( complex1.imaginary *  complex2.imaginary ) ;

  a.imaginary = ( complex1.real * complex2.imaginary ) + ( complex1.imaginary *                                                           complex2.real ) ;

  return a ;
}
bool equal_complex( complex complex1, complex complex2 ){

  return ( ( complex1.real == complex2.real ) && ( complex1.imaginary ==                                                          complex2.imaginary ) ) ;

}

complex divide_complex ( complex complex1, complex complex2 ){

  complex a ;

  a.real      = (( complex1.real * complex2.real ) + ( complex1.imaginary * complex2.imaginary ) ) / ( ( complex2.real *      complex2.real ) + ( complex2.imaginary * complex2.imaginary ) ) ;

  return ( a ) ;

}
void print_complex( complex complex1 ){

  printf( "%1f  ", complex1.real ) ;
  printf( "%1f  ", complex1.imaginary ) ;

  return ;
}
/**************************************/
/* complex.h                                                       */
/**************************************/

#ifndef _complex
#define _complex

#include "globals.h"

typedef struct complex { double real ; double imaginary ; } complex ;

extern complex load_complex( double real, double imaginary );

extern void retrieve( complex complex1, double *p_real, double *p_imaginary );

extern complex add_complex( complex complex1, complex complex2 );

extern complex multiply_complex( complex complex1, complex complex2 );

extern complex subtract_complex( complex complex1, complex complex2 );

extern bool equal_complex ( complex complex1, complex complex2 );

extern complex divide_complex( complex complex1, complex complex2 );

extern void print_complex( complex complex1 );

#endif
/********************************/
/* globals.h                                             */
/********************************/

#ifndef _globals
#define _globals

#define DATA( L ) ( ( L ) -> datapointer )
#define NEXT( L ) ( ( L ) -> next )

typedef enum { OK, ERROR } status ;
typedef enum { FALSE=0 , TRUE=1 } bool ;
typedef void *generic_ptr ;

#endif

/****************************************/
/*  Programmer: Dewey                                          */
/*                                                                            */
/*  Main driver to test complex number module.     */
/****************************************/

#include <stdio.h>
#include "globals.h"
#include "complex.h"

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

  complex complex1, complex2, complex3 ;
  double r, i;

  printf( "Complex 1: Please enter two floating point values: " );
  scanf( "%1f", &r ) ; scanf( "%1f", &i ) ;
  complex1 = load_complex( r, i );

  printf( "\nThe complex number is " );  print_complex( complex1 );
  printf( "\n" );

  printf( "Complex 2: please enter two floating point values: " );
  scanf( "%1f", &r ) ; scanf( "%1f", &i ) ;
  complex2 = load_complex( r, i );

  printf( "\nThe complex number is " ); print_complex( complex2 );
  printf( "\n" );

  complex3 = add_complex( complex1, complex2 );
  printf( "The addition of the two complex numbers is " );
  print_complex( complex3 );
  printf( "\n" );

  complex3 = subtract_complex( complex1, complex2 );
  printf( "The subtraction of the two complex numbers is " );
  print_complex( complex3 );
  printf( "\n" );
  complex3 = multiply_complex( complex1, complex2 );
  printf( "The multiplication of the two complex numbers is " );
  print_complex( complex3 );
  printf( "\n" );

  complex3 = divide_complex( complex1, complex2 );
  printf( "The division of the two complex numbers is " );
  print_complex( complex3 );
  printf( "\n" );

  if( equal_complex( complex1, complex2 ) ) printf( "\nThey are two equal complex numbers.\n" );

  else printf( "\nThe numbers are not equal.\n" );

  return 0;
}
commented: thanks for properly posting syntax-highlighted code that is also compilable! +9

You might want to put your prototypes before the function bodies. Otherwise I think your program will assign random things for your functions to return, or something like that.

Edited: realized you used seperate files, ignore my answer

the functions are all declared in the file complex.h which is included in complex.c at the top

complex.c, complex.h, globals.h, main.c are 4 individual files incase you guys didnt know that

your first problem is that you're not getting the values entered by the user. this is becasue you're using the horrible function 'scanf()'

you could try to implement scanf in this manner:

printf( "Complex 1: Please enter two floating point values: " );
  scanf( "%lf %lf", &r, &i);
  printf(" you entered: %f and %f\n",r,i);

or, better yet, you would use 'fgets()' in conjunction with 'sprintf()' to perform some minimal error checkign on the input.

thanks jep its workin now

glad to hear it... seriously tho, consider removing 'scanf()' from your bag of tricks. that's generally a bad function to use.

'fgets()' is much, much better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.