-4

Hi Im trying to store two get_input methods as separate variables but I am not sure how. Also my get_input in the main method keeps saying its implicitly differentiated please help

#include <stdio.h>

double main()
{
   get_input();
   get_input();
}

double get_input(void)
{
   double x1;
   printf("Enter any number: ");
   scanf("%lf", &x1);
   return x1;
}

double get_next(double x2,double x1)
{
   double total;
   total=(((x2)/2)+3(x1));
   return total;
}

void print_result()
{
   return null;
}
Scooter
  • 6,802
  • 8
  • 41
  • 64
  • 2
    move `get_input` to be before `main`, and inside main you need to store the result in a variable, e.g. `double x = get_input(); double y = get_input();` . Your book or course material should explain all this – M.M Feb 01 '16 at 02:54
  • 1
    `return null;` is illegal – M.M Feb 01 '16 at 02:55
  • 2
    This is neither C nor C++. Are you trying to learn either by trial and error? I recommend you don't, it will not get you far. You should read a good book or tutorial. – Baum mit Augen Feb 01 '16 at 02:58
  • `return null` is incorrect as there's no `null` keyword in C, and the function is `void` so you can't return anything. Moreover you're discarding any return values from `get_input` in `main` – phuclv Feb 01 '16 at 03:25
  • C does not support _methods_, only _functions_ – too honest for this site Feb 01 '16 at 03:25
  • Thanks for everyones help. This is my first week in C after a semester in Java so its a little rough going – user5827827 Feb 01 '16 at 03:42

3 Answers3

1

Your compiler is complaining that it doesn't know about your function get_input. Compiling is done top-to-bottom, so when it reaches:

double main()
{
    get_input();

Your compiler doesn't know what get_input is. You need to declare (optionally define) your function before main.

double get_input();
double main()
{
    get_input();

This is called a function prototype and even though get_input hasn't been defined, the compiler at least knows it exists and can continue compiling.

You probably want to assign the return of get_input to a value for later use:

double value = get_input();
Tas
  • 7,023
  • 3
  • 36
  • 51
0

If you are new to C/C++, few basic things and you can rewrite your program as shown below.

#include <stdio.h>

//Function declarations
double get_input(void);
double get_next(double x2,double x1);
void print_result();

int main( int argc, char *argv[])
{
   double d1, d2;
   d1 = get_input();
   d2 = get_input();
   return 0;
}

double get_input(void)
{
   double x1;
   printf("Enter any number: ");
   scanf("%lf", &x1);
   return x1;
}

double get_next(double x2,double x1)
{
   double total;
   total=(((x2)/2)+3(x1));
   return total;
}

void print_result()
{
   return;
}
Umamahesh P
  • 1,224
  • 10
  • 14
-1

any chance that actually said implicitly defined? If so I suspect that the problem is that you are calling your functions before defining them. To fix this you have 2 options:

1.reorder - for this solution, you will want to make sure that no functions are used before being created. In your case the problem would be using get_input() in main.

#include <stdio.h>

double get_input(void)
{
    double x1;
    printf("Enter any number: ");
    scanf("%lf", &x1);
    return x1;
}
double get_next(double x2,double x1)
{
    double total;
    total=(((x2)/2)+3(x1));
    return total;
}
void print_result(void)
{
    return null;
}
double main(void)//move main to the bottom
{
    get_input();
    get_input();
}

2.prototypes

#include <stdio.h>
double_getinput(void);
double get_next(double x2, double x1);//double get_next(double, double); also works, I just think this looks better
void print_result();

double main(void)
{
    get_input();
    get_input();
}
double get_input(void)
{
    double x1;
    printf("Enter any number: ");
    scanf("%lf", &x1);
    return x1;
}
double get_next(double x2,double x1)
{
    double total;
    total=(((x2)/2)+3(x1));
    return total;
}
void print_result(void)
{
    return null;
}

Also, main should always look like one of the following:

1

!implicit void param~ (Apparently invalid)

int main(){//
    ...
    return 0;
}

explicit void param (a little less common, but I would recommend this for you)

int main(void){
    ...
    return 0;
}

explicit command line args (second most common)

int main(int argc, char *argv){
    ...
    return 0;
}

NOTE: the int is also optional for all of these forms. For example (I HIGHLY discourage this option):

main(void){
    ...
    return 0;
}

EDIT the reason main should be formatted like this Regarding 'main(int argc, char *argv[])'

Community
  • 1
  • 1
chrisgotter
  • 383
  • 1
  • 3
  • 13
  • 2
    Why should `main` be written in one of the 4 ways you mention? You should format your code with indentations – Tas Feb 01 '16 at 03:18
  • @Tas it wasn't indented because I copied the code sample from the person who asked the question. – chrisgotter Feb 01 '16 at 03:23
  • `main` has an invalid signature and you do not provide a correct prototype for `print_result`. The compiler should complain about different types between the two declarations actually. Also an empty argument list is an obsolescence feature and might be removed from the next standard 8see future directions in the standard). `int mina(void)` is definitively not "less common", but the correct way. C is not C++! – too honest for this site Feb 01 '16 at 03:33
  • @Olaf, I said it was less common (and it is in my experience). I didn't say it was a good idea. And recommending the implicit `void` was a typo – chrisgotter Feb 01 '16 at 03:39
  • 1
    Please show some statistics. In professional programming, it is actually standard (and enforced by many coding standards. Simple reason is to make it visually compliant with the prototypes, which actually **have to** use `(void)`. An empty argument list in a function declaration which is not a definition means the function takes an `int` argument. This is no correct prototype. – too honest for this site Feb 01 '16 at 03:44
  • 1
    And omitting the result type is invalid, too. Please update your C knowlesge before telling wrong information to beginners, you are >17 years behind. C99 already required prototypes and not just K&R declarators (which does not imply ANSI-C did not already introduce them). – too honest for this site Feb 01 '16 at 03:45
  • look, all I was trying to do was say that I had seen it, I don't carry a little black book of compiler specific syntax examples (it's in my other pants). I have personally dealt with several different compilers and it can be difficult to keep their eccentricities straight. I suspect I saw most of these examples in VC 2010 (which was the compiler I learned on). I **Know** it is bad, and for the record *invalid* does not mean *not supported*. – chrisgotter Feb 01 '16 at 04:01
  • and the Visual C compiler did not even adopt C99 until 2013 - http://herbsutter.com/2012/05/03/reader-qa-what-about-vc-and-c99/ – chrisgotter Feb 01 '16 at 04:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102194/discussion-between-chrisgotter-and-olaf). – chrisgotter Feb 01 '16 at 04:10
  • 1
    We are talking about C, for which a well-accepted ISO standard (9899:2011) exists. As a C programmer you **have to** know what is correct and what not. That has nothing to do with "eccentricities". And while a very bad reference for correct C, even MSVC **at least** allows that syntax (which is standard since C89. Note that my point is this is read by beginners, OP is a beginner. Especially for beginners an answer should be correct and standard-compliant. – too honest for this site Feb 01 '16 at 13:41