Welcome to Dream.In.Code
Become a C++ Expert!

Join 136,900 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,714 people online right now. Registration is fast and FREE... Join Now!




multiple functions and switch problem in C

 
Reply to this topicStart new topic

multiple functions and switch problem in C

steverh
31 Mar, 2008 - 08:44 AM
Post #1

New D.I.C Head
*

Joined: 30 Mar, 2008
Posts: 13

Hello, I am having a problem getting the proper result and for the switch to prompt for input. The program is supposed to find the largest number, smallest number, average and sum of input numbers. We have not yet covered loops or arrays so I am limited in not being able to use those. Any direction is appreciated.

CODE


#include <stdio.h>
int smallestinteger(int, int, int, int, int); //function prototype
int largestinteger(int, int, int, int, int); //function prototype
int suminteger(int, int, int, int, int); //function prototype
int averageinteger(int, int, int, int, int); //function prototype

main()
{

    int num1 = 0;
    int num2 = 0;
    int num3 = 0;
    int num4 = 0;
    int num5 = 0;
    int intresponse = 0;
        
    printf("\nEnter 5 integers separated by a space, then press enter:  ");
    scanf("%d", num1, num2, num3, num4, num5);
            
    printf("\nSelect an action to perform: ");
    printf("\nTo determine the smallest integer, enter 1.\n");
    printf("To determine the largest integer, enter 2.\n");
    printf("To determine the sum of the integers, enter 3.\n");
    printf("To determine the average of the integers, enter 4.\n");
    scanf("%d", &intresponse);
        
    switch (intresponse) {
        
        case 1:
            printf("\nThe result of the selected action is %d\n.", smallestinteger);
            break;
        case 2:
            printf("\nThe result of the selected action is %d\n.", largestinteger);
            break;
        case 3:
            printf("\nThe result of the selected action is %d\n.", suminteger);
            break;
        case 4:
            printf("\nThe result of the selected action is %d\n.", averageinteger);
            break;
        default:
            printf("\nPlease run the program again and choose an action from 1 thru 4.");
                printf("\nThe result of the selected action is 0.");
    
     }//end switch  

} //end main


int smallestinteger (int num1,int num2,int num3,int num4,int num5) //function definition
{    
    if(num1 < num2 && num1 < num3 && num1 < num4 && num1 < num5)
        printf("%d", num1);
        
    else if(num2 < num1 && num2 < num3 && num2 < num4 && num2 < num5)
        printf("%d", num2);
        
    else if(num3 < num1 && num3 < num2 && num3 < num4 && num3 < num5)
        printf("%d", num3);
            
    else if(num4 < num1 && num4 < num2 && num4 < num3 && num4 < num5)
        printf("%d", num4);
                
    else if(num5 < num1 && num5 < num2 && num5 < num3 && num5 < num4)
        printf("%d", num5);
    
    return smallestinteger;
}            

    
int largestinteger(int num1,int num2,int num3,int num4,int num5) //function definition
{    if(num1 > num2 && num1 > num3 && num1 > num4 && num1 > num5)
        printf("%d", num1);
        
    else if(num2 > num1 && num2 > num3 && num2 > num4 && num2 > num5)
        printf("%d", num2);
        
    else if(num3 > num1 && num3 > num2 && num3 > num4 && num3 > num5)
        printf("%d", num3);
            
    else if(num4 > num1 && num4 > num2 && num4 > num3 && num4 > num5)
        printf("%d", num4);
                
    else if(num5 > num1 && num5 > num2 && num5 > num3 && num5 > num4)
        printf("%d", num5);
            
    return largestinteger;
}            


int suminteger(int num1, int num2, int num3, int num4, int num5) //function definition
{
    return (num1 + num2 + num3 + num4 + num5);
}

    
int averageinteger(int num1, int num2, int num3, int num4, int num5) //function definition
{
    return ((num1 + num2 + num3 + num4 + num5) / 5);
}

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Multiple Functions And Switch Problem In C
31 Mar, 2008 - 11:02 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
You had a few things wrong, most of them are beginner errors related to programming. So I have corrected them and provided in code comments to show you what needs to be done where.

cpp

#include <stdio.h>

int smallestinteger(int, int, int, int, int); //function prototype
int largestinteger(int, int, int, int, int); //function prototype
int suminteger(int, int, int, int, int); //function prototype
int averageinteger(int, int, int, int, int); //function prototype

// Main typically returns an integer
int main()
{

int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
int num5 = 0;
int intresponse = 0;

// Notice we use five format specifiers (aka %d)
// and then provide the address of all five integer variables to put them in.

printf("\nEnter 5 integers separated by a space, then press enter: ");
scanf("%d %d %d %d %d", &num1, &num2, &num3, &num4, &num5);


printf("\nSelect an action to perform: ");
printf("\nTo determine the smallest integer, enter 1.\n");
printf("To determine the largest integer, enter 2.\n");
printf("To determine the sum of the integers, enter 3.\n");
printf("To determine the average of the integers, enter 4.\n");
scanf("%d", &intresponse);

// Remember we have to pass all the variables to the functions since they take 5 parameters
// They then return a value so we use a variable to hold the return value.
int smallestintegers = smallestinteger(num1, num2, num3, num4, num5);
int largestintegers = largestinteger(num1, num2, num3, num4, num5);
int sumintegers = suminteger(num1, num2, num3, num4, num5);
int averageintegers = averageinteger(num1, num2, num3, num4, num5);

// Our switch is working fine except you have to specify the integer variables we setup.
// We could have called the functions directly, but it would have made it look very ugly.

switch (intresponse) {

case 1:
printf("\nThe result of the selected action is %d\n.", smallestintegers);
break;
case 2:
printf("\nThe result of the selected action is %d\n.", largestintegers);
break;
case 3:
printf("\nThe result of the selected action is %d\n.", sumintegers);
break;
case 4:
printf("\nThe result of the selected action is %d\n.", averageintegers);
break;
default:
printf("\nPlease run the program again and choose an action from 1 thru 4.");
printf("\nThe result of the selected action is 0.");

}//end switch

// Since main returns an integer, we return 0 saying all is ok.
return 0;
} //end main


// You defined the function to take five integers so you must call it giving it 5 integers
// It also returns an integer, so you have to make sure you use the "return" statement and return
// the appropriate value, not just print them.

// If a function was to just print, the function would have to be defined as returning "void"
int smallestinteger (int num1,int num2,int num3,int num4,int num5) //function definition
{
if(num1 < num2 && num1 < num3 && num1 < num4 && num1 < num5)
return num1;

else if(num2 < num1 && num2 < num3 && num2 < num4 && num2 < num5)
return num2;

else if(num3 < num1 && num3 < num2 && num3 < num4 && num3 < num5)
return num3;

else if(num4 < num1 && num4 < num2 && num4 < num3 && num4 < num5)
return num4;

else if(num5 < num1 && num5 < num2 && num5 < num3 && num5 < num4)
return num5;
}

// Again returns an integer value so we have to use the return statement.
int largestinteger(int num1,int num2,int num3,int num4,int num5) //function definition
{ if(num1 > num2 && num1 > num3 && num1 > num4 && num1 > num5)
return num1;

else if(num2 > num1 && num2 > num3 && num2 > num4 && num2 > num5)
return num2;

else if(num3 > num1 && num3 > num2 && num3 > num4 && num3 > num5)
return num3;

else if(num4 > num1 && num4 > num2 && num4 > num3 && num4 > num5)
return num4;

else if(num5 > num1 && num5 > num2 && num5 > num3 && num5 > num4)
return num5;
}

// And again we return an integer.
int suminteger(int num1, int num2, int num3, int num4, int num5) //function definition
{
return (num1 + num2 + num3 + num4 + num5);
}

// Typically a division like this may return a decimal (11/5 is a decimal after all).
// Then you would specify it as returning a double data type, not int. I left it alone for you.

int averageinteger(int num1, int num2, int num3, int num4, int num5) //function definition
{
return ((num1 + num2 + num3 + num4 + num5) / 5);
}




You need to reread the section on functions. When you call a function, you must pass it parameters (aka arguments) that are the type and number specified in the function definition. If your suminteger function takes 5 integers, you must specify 5 integers. Also pay attention to return types. If it supposed to return a value (like an integer or anything) you have to use the return statement in the function to actually return the value. Otherwise the function uses "void" as the return type if it doesn't return a value.

Notice at the top that we call the functions and give them the values we collected. The return values are then stored in integer variables we then use in the switch statement.

Last thing was your scanf. If you are going to collect multiple values you need multiple specifiers and then list all the variables receiving values. Don't forget to use the ampersand symbol to tell scanf to put the value into the variable.

Read through and let us know if you have any further questions.

Enjoy!

"At DIC we be C programming code ninjas! We like to "C" people getting thrown out of windows too!" decap.gif
User is online!Profile CardPM
+Quote Post

steverh
RE: Multiple Functions And Switch Problem In C
31 Mar, 2008 - 12:32 PM
Post #3

New D.I.C Head
*

Joined: 30 Mar, 2008
Posts: 13

QUOTE

// Remember we have to pass all the variables to the functions since they take 5 parameters
// They then return a value so we use a variable to hold the return value.
int smallestintegers = smallestinteger(num1, num2, num3, num4, num5);
int largestintegers = largestinteger(num1, num2, num3, num4, num5);
int sumintegers = suminteger(num1, num2, num3, num4, num5);
int averageintegers = averageinteger(num1, num2, num3, num4, num5);

// Our switch is working fine except you have to specify the integer variables we setup.
// We could have called the functions directly, but it would have made it look very ugly.


The direction that you have provided has explained alot and makes sense to me now. I still have a question about this part though. A new variable is being initialized here (smallestintegers, largestintegers, etc.). But when I try to compile this it give me an parse error "expecting }". Then if I enclose these new variables in the brackets it does not recoginze the variables. It seems like it is treating this as a function maybe? Thanks again for your help.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Multiple Functions And Switch Problem In C
31 Mar, 2008 - 12:55 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well the example I have provided is valid and should compile just fine. So the error may be related to either how you copied the example or something specific to your compiler. The place where I created the new integer variables... notice that I put an "s" on the end of them and that I made the variables in the switch statement also matching with the "s".

Also make sure that you have matching closing braces for every opening one.

Lastly, can you provide the text for the error message exactly including the line number it is complaining about? Thanks.

smile.gif
User is online!Profile CardPM
+Quote Post

steverh
RE: Multiple Functions And Switch Problem In C
31 Mar, 2008 - 02:32 PM
Post #5

New D.I.C Head
*

Joined: 30 Mar, 2008
Posts: 13


I did notice the new variable name with the s on the end. I have searched for any out of place braces ad cannot find any that are not matched. The error message that I am getting is

line 35: Parse Error, expecting `'}''
'int smallestintegers = smallestinteger(num1, num2, num3, num4, num5)'
aborting compile

I am using Miracle C compiler and tried to download and a couple of others to try and compile in on your reccomendation but was not succesful in finding something that I can use.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Multiple Functions And Switch Problem In C
31 Mar, 2008 - 02:44 PM
Post #6

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Does line 35 there have a semicolon on the end? Check to make sure all your lines have semicolons. If that is ok, delete the two comment lines before line 35 there and see if that helps.

I have never heard of that compiler and it sounds like it is being picky. Did you copy my program exactly using a cut and paste?
User is online!Profile CardPM
+Quote Post

steverh
RE: Multiple Functions And Switch Problem In C
31 Mar, 2008 - 04:13 PM
Post #7

New D.I.C Head
*

Joined: 30 Mar, 2008
Posts: 13

Got it fixed *sigh of relief*, I removed the new variables (smallestintegers, etc) and moved the function into the case and although it may not be pretty it works. I really appreciate your help.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 09:32PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month