Thread: What's wrong with this code??

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    20

    What's wrong with this code??

    Hi,

    I wrote this for a CSC 160 level class... Can anyone tell me why I'm getting compiling errors (specifically, why are the functions that use address referencing getting errors)? I can't figure it out for the life of me!

    Code:
    #include <stdio.h>
    
    void askforlabs();
    void askforfinal();
    void getlabs(int& lab1, int& lab2, int& lab3, int& lab4, int& lab5, int& lab6, int& lab7, float& labtotal);
    void getfinal(float& final);
    void display(float number);
    void computecourse(float labtotal, float final, float& course);
    
    void askforlabs()
    {
    	printf("Please input the seven lab scores, each separated by a space: ");
    }
    
    void askforfinal()
    {
    	printf("\n\nPlease input the final score: ");
    }
    
    void getlabs(int& lab1, int& lab2, int& lab3, int& lab4, int& lab5, int& lab6, int& lab7, float& labtotal)
    {
    	scanf("%d %d %d %d %d %d %d", &lab1, &lab2, &lab3, &lab4, &lab5, &lab6, &lab7);
    	labtotal = (float)(lab1 + lab2 + lab3 + lab4 + lab5 + lab6 + lab7);
    	labtotal = labtotal * 1.0714;
    }
    
    void getfinal(float& final)
    {
    	scanf("%f", &final);
    }
    
    void computecourse(float labtotal, float final, float& course)
    {
    	course = labtotal + final;
    }
    
    void display(float number)
    {
    	printf("\n%f\n", number);
    }
    
    int main()
    {
    	int lab1=-1,lab2=-1,lab3=-1,lab4=-1,lab5=-1,lab6=-1,lab7=-1;
    	float labtotal=-1;
    	float final=-1;
    	float course=-1;
    	askforlabs();
    	getlabs(lab1, lab2, lab3, lab4, lab5, lab6, lab7, labtotal);
    	askforfinal();
    	getfinal();
    	computecourse(course);
    	display(labtotal);
    	display(final);
    	display(course);
    }
    thanks!

    -Xanth

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    In future, post the messages that you get with you errors messages.

    First of all, you're not calling all your functions correctly. Some are expecting certain parameters and simply not being passed them. Also, if you define your functions before you define main(), then you don't need to list the prototypes at the top.

    Second, when you need to use memory addresses in a function, I would list pointers as parameters. You'll need to modify your code a bit to allow this - but don't forget to dereference your pointers when you need the value, and just put the pointers name when you need a memory address. To list a pointer as a parameter, just put an asterisk between the type and variable name.
    Last edited by sean; 12-22-2004 at 05:37 PM.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    Thanks... I didn't see the functions missing the passing of final! The errors I'm getting though are syntax errors that I just don't see!

    Here's the log:

    --------------------Configuration: lab4 - Win32 Debug--------------------
    Compiling...
    lab4.c
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(5) : error C2143: syntax error : missing ')' before '&'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(5) : error C2143: syntax error : missing '{' before '&'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(5) : error C2059: syntax error : '&'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(5) : error C2059: syntax error : ')'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(6) : error C2143: syntax error : missing ')' before '&'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(6) : error C2143: syntax error : missing '{' before '&'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(6) : error C2059: syntax error : '&'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(6) : error C2059: syntax error : ')'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(8) : error C2143: syntax error : missing ')' before '&'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(8) : error C2143: syntax error : missing '{' before '&'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(8) : error C2059: syntax error : '&'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(8) : error C2059: syntax error : ')'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(20) : error C2143: syntax error : missing ')' before '&'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(20) : error C2143: syntax error : missing '{' before '&'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(20) : error C2059: syntax error : '&'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(20) : error C2059: syntax error : ')'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(27) : error C2143: syntax error : missing ')' before '&'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(27) : error C2143: syntax error : missing '{' before '&'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(27) : error C2059: syntax error : '&'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(27) : error C2059: syntax error : ')'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(32) : error C2143: syntax error : missing ')' before '&'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(32) : error C2143: syntax error : missing '{' before '&'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(32) : error C2059: syntax error : '&'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(32) : error C2059: syntax error : ')'
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(49) : warning C4013: 'getlabs' undefined; assuming extern returning int
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(51) : warning C4013: 'getfinal' undefined; assuming extern returning int
    C:\Program Files\Microsoft Visual Studio\MyProjects\lab4\lab4.c(52) : warning C4013: 'computecourse' undefined; assuming extern returning int
    Error executing cl.exe.

    lab4.exe - 24 error(s), 3 warning(s)

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I seem to have edited my post at the same time you were posting your errors. Try making the changes above (specifically using pointers instead of the &'s and that should clear up a lot of the problems).

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    Thanks for the suggestion, but one of the reqiurements of the assignment is that it be passed by reference (no pointers).

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Xanth
    Thanks for the suggestion, but one of the reqiurements of the assignment is that it be passed by reference (no pointers).

    Then it's a C++ program. The errrors you posted are what you get when you compile it as a C program. C does not have call-by-reference. (Actually, the use of pointers can be considered an explicit call-by-reference, but you can't use pointers)

    If you are using the Visual Studio IDE, make it a C++ project. If you are using the command line compiler, the easiest way is to rename your file lab4.cpp, rather than lab4.c so that cl will compile it as C++.

    You still have a couple of compiler errors, but maybe you can make some sense of them.

    Regards,

    Dave
    Last edited by Dave Evans; 12-22-2004 at 06:19 PM.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    Awesome! That worked perfectly... Thanks guys!!

  8. #8
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    It looks as if some of your function Prototypes are expecting you to pass the address of the variables, yet your calls to those functions are passing the variable's themselves.

    Additionally the prototype needs to use pointers, you can't use the &variable method to denote passing the address of a variable.
    Last edited by Scribbler; 12-22-2004 at 07:42 PM.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Specify the parameters of your functions as pointers, but then pass by reference (e.g. function_name(&variable_name)

  10. #10
    Quote Originally Posted by Xanth
    Code:
    #include <stdio.h>
    This is obsolete C++
    Code:
    void getlabs(int& lab1, int& lab2, int& lab3, int& lab4, int& lab5, int&
    This is not C.

    You must stop to mix C and C++ and make a clear choice concerning the language you are going to use. Once done, please address you request to the appropriate forum (C or C++)
    Emmanuel Delahaye

    "C is a sharp tool"

  11. #11
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Change this:
    Code:
    void getlabs(int& lab1, int& lab2, int& lab3, int& lab4, int& lab5, int& lab6, int& lab7, float& labtotal);
    To this:
    Code:
    void getlabs(int lab1, int lab2, int lab3, int lab4, int lab5, int lab6, int lab7, float labtotal);
    Because you're not sending an address of anything, you're sending the the whole integer. If you want to pass the labs in as reference then go like this:
    Code:
    void getlabs(int *lab1, int *lab2, int *lab3, int *lab4, int *lab5, int *lab6, int *lab7, float *labtotal)
    {
    	scanf("%d %d %d %d %d %d %d", &lab1, &lab2, &lab3, &lab4, &lab5, &lab6, &lab7);
    	labtotal = (float)(lab1 + lab2 + lab3 + lab4 + lab5 + lab6 + lab7);
    	labtotal = labtotal * 1.0714;
    }
    Code:
    getlabs(&lab1, &lab2, &lab3, &lab4, &lab5, &lab6, &lab7, &labtotal);
    You see, the & is saying: Get the address
    The * is saying: Receive as an address

    I hope this helps you!
    Last edited by Kleid-0; 12-23-2004 at 02:37 PM.

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    This is obsolete C++
    Meaning: The new C++ standard requires that you drop the .h from standard header files, and precede C header files with a 'c' (i.e. #include <cstdlib>). You also need to specify the namespaces you're using for certain function calls (in this case, the only one you're going to be using is the std namespace. There are a number of ways to do this. You can precede each reference to something in the standard library with 'std::'. Personally, I just prefer to place "using namespace std" at the top, and then program like I used to the rest of the way...

    This however, only applies to C++. You posted on the C forum, but used some C++ only syntax. Since it appears that you shouldn't not have used that syntax anyway, and also judging by your use of C I/O functions, my guess is that you are only using C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM