Results 1 to 5 of 5

Thread: Kind a beginner thingie...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ilirska Bistrica, Slovenia
    Posts
    242

    Kind a beginner thingie...

    Hi

    For about an hour now I'm trying to get with my head through the wall, but I can't.. ;o)
    I really can't figure out what't the problem with my program. It's something with variables and &-operator, but what...
    Can someone please look at it?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    
    void getFunctionParameters(void);
    void sqeq(double a, double b, double c, double &x1r, double &x1i, double &x2r, double &x2i);
    //void sqeq(double &a, double &b, double &c, double &x1r, double &x1i, double &x2r, double &x2i);
    //void sqeq(double a, double b, double c, double x1r, double x1i, double x2r, double x2i);
    
    double paramA, paramB, paramC;
    
    int main(int argc, char* argv[]) {
    	double X1r = 0, X1i = 0, X2r = 0, X2i = 0;
    	
    	if (argc == 1) 
    		getFunctionParameters();
    	else {
    		printf("\n/--------------------------------------------------\\\n");
    		printf("| Program za izracun vrednosti kvadratne funkcije. |\n");
    		printf("|                                                  |\n");
    		printf("| (C) 2002 by Tanja Vasilevska                     |\n");
    		printf("\\--------------------------------------------------/\n");
    		exit(0);
    	}
    	
    	sqeq(paramA, paramB,paramC, X1r, X1i, X2r, X2i);
    
    	printf("Rezultat:\n");
    	if (X1i == 0 && X2i == 0) {
    		printf("\tX1 = %f\n", X1r);
    		printf("\tX2 = %f\n", X2r);
    	}
    	else {
    		printf("\tRe(X1) = %f; Im(X1) = %f\n", X1r, X1i);
    		printf("\tRe(X2) = %f; Im(X2) = %f\n", X2r, X2i);
    	}
    
    	return 0;
    }
    
    
    void getFunctionParameters(){
    	char pravilen = ' ';
    	float A, B, C;
    
    	printf("----------------------------------------------\n");
    	printf("Vpiši vrednost parametra A: ");
    	scanf("%f", &A);
    	printf("Vpiši vrednost parametra B: ");
    	scanf("%f", &B);
    	printf("Vpiši vrednost parametra B: ");
    	scanf("%f", &C);
    	printf("----------------------------------------------\n");
    	printf("Funkcija: 0 = %fx^2 + %fx + %f\n", A, B, C);
    	printf("----------------------------------------------\n");
    	
    	paramA = (double)A;
    	paramB = (double)B;
    	paramC = (double)C;
    }
    
    
    void sqeq(double a, double b, double c, double &x1r, double &x1i, double &x2r, double &x2i) {
    	double d = b * b - 4 * a * c;
    
    	if (d >= 0) {
    		x1r = (-b + sqrt(d)) / (2 * a);
    		x2r = (-b - sqrt(d)) / (2 * a);
    		x1i = 0;
    		x2i = 0;
    	}
    	else {
    	    x1r = -b / (2 * a);
    		x2r = -b / (2 * a);
    		x1i = sqrt(-d) / (2 * a);
    		x2i = -x1i;
        }
    }
    And errors that I get with VC++ 6:
    Code:
    --------------------Configuration: main - Win32 Debug--------------------
    Compiling...
    main.c
    C:\Homecooked\Tanja\main.c(7) : error C2143: syntax error : missing ')' before '&'
    C:\Homecooked\Tanja\main.c(7) : error C2143: syntax error : missing '{' before '&'
    C:\Homecooked\Tanja\main.c(7) : error C2059: syntax error : '&'
    C:\Homecooked\Tanja\main.c(7) : error C2059: syntax error : ')'
    C:\Homecooked\Tanja\main.c(27) : warning C4013: 'sqeq' undefined; assuming extern returning int
    C:\Homecooked\Tanja\main.c(64) : error C2143: syntax error : missing ')' before '&'
    C:\Homecooked\Tanja\main.c(64) : error C2143: syntax error : missing '{' before '&'
    C:\Homecooked\Tanja\main.c(64) : error C2059: syntax error : '&'
    C:\Homecooked\Tanja\main.c(64) : error C2059: syntax error : ')'
    Error executing cl.exe.
    
    main.exe - 8 error(s), 1 warning(s)
    Thanks
    Zvonko Bostjancic
    Ilirska Bistrica, Slovenia
    [email protected]
    Using VS6 Professional with SP3
    Programming mostly in VB and I've started to learn VC++ & MFC

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    C doesn't have references - you can't use:
    Code:
    void func(int &ref) {
        ref += 1;
    }
    ...you'd have to use:
    Code:
    void func(int *ptr) {
        *ptr += 1;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ilirska Bistrica, Slovenia
    Posts
    242
    Huh... ;o)

    So I have to change all & to *. Am I right?
    I already tried that but somehow didn't work properly - i got standard windows error: "The program made illegal funcion call in module ... ". I'll try it again - it may work.
    Thanks

    Zvonko
    Zvonko Bostjancic
    Ilirska Bistrica, Slovenia
    [email protected]
    Using VS6 Professional with SP3
    Programming mostly in VB and I've started to learn VC++ & MFC

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You do in the function prototypes, then when you pass the variables to the function, use & in front of it (to emphasise passing the address). Then in the function, use * to dereference the pointer to get a variable you can change.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ilirska Bistrica, Slovenia
    Posts
    242
    Thanks.

    I did that and it worked.
    You see, we were learning C in school but I kind a forgot that arithmetics with pointers... I hate pointers...
    Zvonko Bostjancic
    Ilirska Bistrica, Slovenia
    [email protected]
    Using VS6 Professional with SP3
    Programming mostly in VB and I've started to learn VC++ & MFC

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width