Calling functions help

This is a discussion on Calling functions help within the C++ Programming forums, part of the General Programming Boards category; does anybody know what is wrong with this code i wrote? i'm working through a C++ book on my own ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    45

    Calling functions help

    does anybody know what is wrong with this code i wrote? i'm working through a C++ book on my own and its difficult for me learning this stuff on my own. I tought myself visual basic and VB.NET no problem, but i've been having some trouble with this.

    Code:
    #include<iostream.h>
    
    void main()
    {
    	
    	const int arraySize=4;
    	int integer[arraySize], a;
    	void quadruple(int integer[]);
    	for (a=0; a<arraySize; ++a)
    	{
    		cout<<"Enter an integer: ";
    		cin>>integer[a];
    	}
    	quadruple(int integer[])
    }
    
    void quadruple(int values[])
    {
    	for (a=0; a<arraySize; ++a)
    		integer[a]=integer[a]*4;
    	for (a=0; a<arraySize;++a)
    		cout<<integer[a]<<endl;
    }

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    22

    ??

    There are a few things wrong with your program, but what are you trying to do?? I can't help you unless you tell me what you program should do....

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571

    Re: Calling functions help

    Code:
    #include<iostream>
    using namespace std;
    
    void quadruple(int [], int );
     // It's more common to put the prototype outside of any function
    
    int main()
    {
    	
    	const int arraySize=4;
    	int integer[arraySize], a;
    	for (a=0; a<arraySize; ++a)
    	{
    		cout<<"Enter an integer: ";
    		cin>>integer[a];
    		//You need to clear the input stream between numbers
    	}
    	quadruple(integer, arraySize);
    }
    
    void quadruple(int values[], int arraySize)
    {
    	int a; //You need to declare a in this function if you plan to use it here, too
    	for (a=0; a<arraySize; ++a)
    		integer[a]=integer[a]*4;
    	for (a=0; a<arraySize;++a)
    		cout<<integer[a]<<endl;
    }
    Last edited by Cat; 09-28-2003 at 05:10 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    ok, i have another problem ....how would i do this overload function thingy....i get a lot of errors. somebody please help. here's what i have'


    Code:
    #include<iostream.h>
    
    class Shirt
    {
    	public:
    		int collarSize, widthInFeet;
    };
    class Pants
    {
    	public:
    		int waistSize, inSeam;
    };
    
    void main()
    {
    	Shirt oneShirt;
    	Pants onePair;
    	int oneShirt.collarSize=15, oneShirt.widthInFeet=23;
    	double onePair.waistSize=30, onePair.inSeam=32;
    	printClothingFacts(oneShirt);
    	printClothingFacts(onePair);
    };
    
    void printClothingFacts(int x)
    {
    	cout<<"The shirt has a collar size "<<oneShirt.collarSize<<" and a sleeve length "<<oneShirt.widthInFeet;
    	
    }
    void printClothingFacts(double x)
    {
    	cout<<"The pants have waist size "<<onePair.waistSize<<" and inseam "<<onePair.inSeam;
    }

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    first of all...DO NOT BUMP YOUR POSTS...second, read or reread the top two posts on this board.

    now to your problem:

    do not use void main()!!!!!!!!! ALWAYS use int main(){ return 0;}

    when initializing you class member variables you don't have to include int...
    Code:
    int oneShirt.collarSize=15, oneShirt.widthInFeet=23;
    //should be
    oneShirt.collarSize = 15;
    oneShirt.widthInFeet = 23; //shirt width in feet?!?
    second you can't just call a function without having declared the prototype at the top. The compiler works top-down...in order for it to recognize something in your code t had to be somewhere above it...you wouldn't have something like this would you:
    Code:
    std::cout << "enter number: ";
    std::cin >> x;
    int x;
    the compiler would give you an error that x is an unandentified variable. same thing goes for functions...

    First get a grasp of functions and then do classes.

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    i know, i messed up on that last one, thats the code i had before, i saved the new one i was working on to a different name and loaded the wrong one. here is what i have...i dont understand the errors im getting. (yes i know im using void main(), but im doing what the book tells me to do)
    Code:
    #include<iostream.h>
    
    
    class Shirt
    {
    	public:
    		int collarSize, sleeveLength;
    };
    class Pants
    {
    	public:
    		double waistSize, inSeam;
    };
    
    void main()
    {
    	Shirt oneShirt;
    	Pants onePair;
    	oneShirt.collarSize=15;
    	oneShirt.sleeveLength=23;
    	onePair.waistSize=30;
    	onePair.inSeam=32;
    	void printClothingFacts(int Shirt);
    	void printClothingFacts(double Pants);
    	printClothingFacts(oneShirt);
    	printClothingFacts(onePair);
    };
    Shirt printClothingFacts(Shirt oneShirt)
    {
    
    	cout<<"The shirt has a collar size "<<oneShirt.collarSize<<" and a sleeve length "<<oneShirt.sleeveLength;
    	
    };
    Shirt printClothingFacts(Pants onePair)
    {
    	cout<<"The pants have waist size "<<onePair.waistSize<<" and inseam "<<onePair.inSeam;
    }
    Last edited by ForlornOdium; 09-28-2003 at 08:08 PM.

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Your book is wrong, don't use void main(). It's just plain wrong. Read this page. This discussion has occured too many times at these forums, please just accept that you should not use void main(). It may work sometimes, but it's just not right.

    As for your errors, what are they? You haven't actually said yet.
    Last edited by bennyandthejets; 09-28-2003 at 08:22 PM.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    heres the errors i get.

    error C2665: 'printClothingFacts' : none of the 2 overloads can convert parameter 1 from type 'class Shirt'

    error C2665: 'printClothingFacts' : none of the 2 overloads can convert parameter 1 from type 'class Pants'
    Error executing cl.exe.

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by ForlornOdium
    i know, i messed up on that last one, thats the code i had before, i saved the new one i was working on to a different name and loaded the wrong one. here is what i have...i dont understand the errors im getting. (yes i know im using void main(), but im doing what the book tells me to do)
    If your book tells you to use void main() and include <iostream.h>, throw the book in the trash and buy a good book.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  10. #10
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Originally posted by ForlornOdium
    i know, i messed up on that last one, thats the code i had before, i saved the new one i was working on to a different name and loaded the wrong one. here is what i have...i dont understand the errors im getting. (yes i know im using void main(), but im doing what the book tells me to do)
    What book are you using?! if that is what it tells you do the following: get an empty metallic garbage can, some lighter fluid, and some matches...throw the book into the can, dump some fluid on it, light a match, enjoy the fire...it is not worth anything more...

    but seriously now...do get a new book: I suggest Absolute C++ by Walter Savitch. Also do a board search for good book; you'll get plenty suggestions.

    axon

    EDIT:: sorry for restating the obvious about the book; did not see bennyandthejets and cat's replies...

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  11. #11
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    Am I doing something wrong when I declare the prototypes or when i call the functions? the only 2 errors I'm getting now are these:

    error C2665: 'printClothingFacts' : none of the 2 overloads can convert parameter 1 from type 'class Shirt'

    error C2665: 'printClothingFacts' : none of the 2 overloads can convert parameter 1 from type 'class Pants'
    Error executing cl.exe.

  12. #12
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Function prototypes (in your code: void printClothingFacts(int Shirt);) should be the same as when you define the function (in your code: Shirt printClothingFacts(Shirt oneShirt)). Obviously in your code they are different, which is why you get compile errors - there is no prototype for the function you are calling.

  13. #13
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    im still getting errors. here's my code

    Code:
    #include<iostream.h>
    
    
    class Shirt
    {
    	public:
    		int collarSize, sleeveLength;
    };
    class Pants
    {
    	public:
    		int waistSize, inSeam;
    };
    
    int main()
    {
    	Shirt oneShirt;
    	Pants onePair;
    	oneShirt.collarSize=15;
    	oneShirt.sleeveLength=23;
    	onePair.waistSize=30;
    	onePair.inSeam=32;
    	void printClothingFacts(Shirt oneShirt);
    	void printClothingFacts(Pants onePair);
    	printClothingFacts(oneShirt);
    	printClothingFacts(onePair);
    };
    Shirt printClothingFacts(Shirt oneShirt)
    {
    
    	cout<<"The shirt has a collar size "<<oneShirt.collarSize<<" and a sleeve length "<<oneShirt.sleeveLength;
    };
    Shirt printClothingFacts(Pants onePair)
    {
    	cout<<"The pants have waist size "<<onePair.waistSize<<" and inseam "<<onePair.inSeam;
    }

  14. #14
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    The return types of your functions should be void.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  15. #15
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    oops, didn't see that i forgot to change that. thanks everybody!...sorry for bein so stupid! heh.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  2. calling functions and error messages
    By l2u in forum C++ Programming
    Replies: 9
    Last Post: 07-27-2008, 01:06 PM
  3. Replies: 9
    Last Post: 01-26-2008, 02:12 AM
  4. calling functions: exit and return
    By 911help in forum C Programming
    Replies: 3
    Last Post: 12-28-2007, 12:24 PM
  5. I Need Help Defining and Calling Functions
    By jonbuckets in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2007, 09:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21