?
Solved

My First Function creation

Posted on 2003-02-22
8
Medium Priority
?
176 Views
Last Modified: 2010-04-15
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
Comment
Question by:Lambretta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 8

Expert Comment

by:Exceter
ID: 7999937
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
 

Author Comment

by:Lambretta
ID: 8002431
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
 

Expert Comment

by:gotenks
ID: 8031093
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:gotenks
ID: 8031110
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
 
LVL 8

Accepted Solution

by:
Exceter earned 200 total points
ID: 8031295
>> - 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
 

Author Comment

by:Lambretta
ID: 8041742
Thanks to all who replied..Exceter I think I can get it from your reply..cheers mate

regards
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10014665
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question