• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

My First Function creation

Given this main body of code, I need to put the calculation part into a seperate function, which i can then call within the main body.

I have tried but get syntax errors so am now at a loss. Many thanks for any help or answers:

float in_float(void);
void wait(void);

/*
 * main() function
 * ===============
 */
main()
     {
     float number=0.0;
     float total =0.0;
     int count=0;
     clrscr();
     

     while (number >= 0)
          {
          float number=0.0;
          float total=0.0;
          int count=0;
          do          
          {
               cprintf("\r\nEnter a number between 0 and 10.0\r\n");
               cprintf("Or enter a negative number to terminate.\r\n");
               number = in_float();
          }while (number >10);    
          while ((total < number) && (kbhit() == 0))
               {
               count++;
               total = total + 1.0/(float)count;
               }
     
          cprintf("\r\nThe number of reciprocals"
          " needed was %u\r\n",count);
          }
     
     wait();
     
     }
/*
 * end of main()
 */
0
Lambretta
Asked:
Lambretta
  • 2
  • 2
  • 2
  • +1
1 Solution
 
ExceterCommented:
That code will not even compile.

>> calculate(int)

What is this? This line does not have a terminating semicolon in addition to the fact that you are attempting to pass a type a parameter. What code are you attempting to place in the calculate function?

>>   cprintf("\r\nEnter a number between 0 and 10.0\r\n");
>>    cprintf("Or enter a negative number to terminate.\r\n");
>>    target = in_float();
>>         }while (target >10);    

This is a prblem because you are telling the user to enter values between 0 and 10 and that -1 will break out of the loop. However, your while statement will loop ONLY if target is greater than 10. I think you while statement should look like this,

while( target >= 0 );

Of course you would need to do a little error chacking to ensure that the user did not enter a value greater than 10 if you wanted to ensure that the user did not enter values greater than 10.

>> cprintf("\r\nThe number of reciprocals"
" needed was %u\r\n",number);

What is this? As far as I understand this should read,

cprintf("\r\nThe number of reciprocals needed was %u\r\n",number);

Also, where is number declared? Here is an example of how to create a function that computes a reslut based on its inputs.

int calculate( int, int ); // This is the function prototype and it should be placed above the main function.

int calculate( int x, int y )
{
    return x * y; // Return the product of x and y
}

You can then call this function by saying,

int x = calculate( 4, 7 );

printf("%i",x); // The result will be 28

Exceter
0
 
LambrettaAuthor Commented:
Thanks for the reply, I copied the wrong version in, but one now amended is correct.

There is a error check for a number gretaer than 10, it asks the question again.

>> calculate(int)was part of an error from a previous try i had

while ((total < number) && (kbhit() == 0))
              {
              count++;
              total = total + 1.0/(float)count;
              }
 The above is the calculating part and the part that needs a function creating, which can then be called as part of the rest of the main body as shown.

Thanks for patience and help
0
 
gotenksCommented:
i am not sure of what errors you have but i have seen one obvious error in your code :
there is not return value for the main function.
  - insert the 'return 0;' at the end of the main function.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
gotenksCommented:
sorry for the previous one...

have you got your
#include<conio.h>
as well in your program?
you need that in order to use some of the function you have used in your code, like kbhit().
0
 
ExceterCommented:
>> - insert the 'return 0;' at the end of the main function.

I have seen some compilers allow main to not have a return type. Although this is a HORRIBLE practise. If you were to return 0 from main you would also have to specify that it return int. As to including conio.h who knows. After all, none of the includes have been posted.

Lambretta, are you getting any errors when you attempt to compile this? Could you post the complete code? What compiler are you using? I suspect Borland but it would be helpfull if you clarified.

>> while ((total < number) && (kbhit() == 0))
>> {
>>     count++;
>>     total = total + 1.0/(float)count;
>> }

If all you want is to place this code into a function then the following should work.

float calculate( float t, float n )
{
    int count 0;

    while ( ( t < n ) && ( kbhit() == 0 ) )
    {
        count++;
        t = t + 1.0/(float)count;
    }
    return t;
}

You can then call it by saying,

total = calculate( total, number );

Exceter
0
 
LambrettaAuthor Commented:
Thanks to all who replied..Exceter I think I can get it from your reply..cheers mate

regards
0
 
jmcgOwnerCommented:
Nothing has happened on this question in over 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by Exceter (interpreting the wishes of asker).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now