0

I would like to ask if what is wrong in my c program. I am a college freshmen, and we have a project and it is to make a program that could be useful in our course, that is why I chose the calorimetry topic. I am not good in programming so hope somebody could help me.. Below is my code. Thank you in advance

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void shc();
void main(){
    int c=0;
    do{
        printf("\n----------------------\n\nEnter-1 for Quantity of Heat\nEnter-2 for  Mass of Substance\nEnter-3 to Specific Heat Capacity of Material\nEnter-4 for Change in Temperature\n\nEnter-5 to exit\n");
        scanf("%d",&c);
        switch(c){
        case 1:qoh();
                break;
        case 2:mos();
                break;
        case 3:shcm();
                break;
        case 4:cit();
                break;
        case 5:exit(0);
                break;
    }
    }while(!(c==5));

        void qoh(){
        int mos, shcm, cit, qoh;
        printf("Enter Mass of Substance: ");
        scanf("%d",&mos);
    
        printf("Specific Heat Capacity of Material: ");
        scanf("%s", &shcm);

        printf("Change in Temperature: ");
        scanf("%d",&cit);
    
        qoh=(mos*shcm*cit);
        printf("Quantity of Heat:  %d",qoh);
        }
    
        void mos(){
        int mos, shcm, cit, qoh;
        printf("Enter Quantity of Heat: ");
        scanf("%d",&qoh);
    
        printf("Specific Heat Capacity of Material: ");
        scanf("%s", &shcm);

        printf("Change in Temperature: ");
        scanf("%d",&cit);
    
        mos=qoh/(shcm*cit);
        printf("Mass of Substance:  %d",mos);
        }
    
        void cit(){
        int mos, shcm, cit, qoh;
        printf("Enter Mass of Substance: ");
        scanf("%d",&mos);
    
        printf("Specific Heat Capacity of Material: ");
        scanf("%s", &shcm);

        printf("Enter Quantity of Heat: ");
        scanf("%d",&qoh);
    
        cit=qoh/(mos*shcm);
        printf("Quantity of Heat:  %d",qoh);
        }
}
dslay
  • 1
  • 1
  • did you compile ? are you getting any error? – IrAM Dec 12 '20 at 05:57
  • yes there are errors, but honestly i don't understand it at all. it says that undefined reference to qoh, mos, shcm, and cit. and also it says [Error] Id returned 1 exit status – dslay Dec 12 '20 at 06:02
  • `scanf("%s", &shcm); `shcm` is an `int` you should use `%d` instead of `%s` – IrAM Dec 12 '20 at 06:03
  • I tried using your advise but when i compile it, it is still an error – dslay Dec 12 '20 at 06:06
  • your main function does not end with closing curly brace `}` . use `}` before you start `void qoh(){` and dont use same name for functions and variables `mos` – IrAM Dec 12 '20 at 06:06
  • 1
    In C, functions need to be defined (or at least declared) before use. Try moving those function definitions above `main` -- or (better practice) add prototypes above `main` and move the functions definitions *below* `main` (i.e. outside of it). – costaparas Dec 12 '20 at 06:06
  • Also, `main` returns an `int` -- you *can't* say its `void`, that is completely invalid C. See [this](https://stackoverflow.com/questions/4273507/what-are-the-different-valid-prototypes-of-main-function) post for the valid prototypes for `main`. – costaparas Dec 12 '20 at 06:07
  • @costaparas, sorry but what do you mean?? because honestly i dont understand programming. could you enlighten me a bit further, or could you show what do you mean?? sorry – dslay Dec 12 '20 at 06:09
  • @IrAM do you mean that i should change my variables for every function even though they have the same meaning or purpose?? – dslay Dec 12 '20 at 06:14
  • @dslayezzz addressed in more detail below – costaparas Dec 12 '20 at 06:24
  • @dslayezzz, what do you mean by same purpose? in `C` you cannot have same name for functions and variables if you are using them in same block of code. I have edited your code in answer go through it and ask if you still have doubts – IrAM Dec 12 '20 at 06:27
  • Actually in this case, since the variables are local to the function, it *is* technically possible to have the same name as the function itself (**although you shouldn't**). – costaparas Dec 12 '20 at 06:31
  • Hi I would like to say thank you all for helping me. It's my first time asking/posting here in stack overflow since I just recently made an account, but many of you helped me, so thank you a lot. – dslay Dec 12 '20 at 08:55

3 Answers3

0

problems:

  1. Your main function does not have closing brace } or you are using an extra } at the end of your file.

  2. dont use same names for functions and variables inside it, all of your functions are using variales with same name as function.

  3. void main() is not correct use int main()

  4. you are using all int variables so use %d in scanf , dont use %s ( this is for strings)

  5. you dont have the definition of schm(), so commented in call,enable once you have the definition of it.

Correct Modified and Edited:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

void shc();
void mos();
void shcm();
void cit();
void qoh();

int main() {
    int c=0;
    do{
        printf("\n----------------------\n ");
        printf("Enter-1 for Quantity of Heat \n");
        printf("Enter-2 for  Mass of Substance \n");
        printf("Enter-3 to Specific Heat Capacity of Material\n");
        printf("Enter-4 for Change in Temperature\n");
        printf("Enter-5 to exit\n");

        scanf("%d",&c);
        
        switch(c) {
        case 1:
            qoh();
            break;
        case 2:
            mos();
            break;
        case 3:
            //shcm();
            break;
        case 4:
            cit();
            break;
        case 5:
            exit(0);
            break;
        }
    }while(!(c==5));
}

void qoh()
{
    int mos, shcm, cit, qoh1;
    printf("Enter Mass of Substance: ");
    scanf("%d",&mos);
    
    printf("Specific Heat Capacity of Material: ");
    scanf("%d", &shcm);
    
    printf("Change in Temperature: ");
    scanf("%d",&cit);
    
    qoh1=(mos*shcm*cit);
    printf("Quantity of Heat:  %d",qoh1);
}

void mos()
{
    int mos1, shcm, cit, qoh;
    printf("Enter Quantity of Heat: ");
    scanf("%d",&qoh);
    
    printf("Specific Heat Capacity of Material: ");
    scanf("%d", &shcm);
    
    printf("Change in Temperature: ");
    scanf("%d",&cit);
    
    mos1=qoh/(shcm*cit);
    printf("Mass of Substance:  %d",mos1);
}

void cit()
{
    int mos, shcm, cit1, qoh;
    printf("Enter Mass of Substance: ");
    scanf("%d",&mos);
    
    printf("Specific Heat Capacity of Material: ");
    scanf("%d", &shcm);
    
    printf("Enter Quantity of Heat: ");
    scanf("%d",&qoh);
    
    cit1=qoh/(mos*shcm);
    printf("Quantity of Heat:  %d",qoh);
}

IrAM
  • 1,720
  • 5
  • 18
0

Step 1:

As it stands, your helper functions are actually declared within the main function -- because the closing brace is right at the bottom of your code. You should move this closing brace right before your function definition void qoh().

Step 2:

Next, change your main function's prototype, which is invalid C. Refer to this post. Best to use the first version -- i.e. int main(void); since your code isn't using arguments in this case. Also, since main is meant to return an int, you should actually do that -- add return 0; at the end of main.

Step 3:

Your main function is calling your helper functions; however, they are defined after (i.e. below) your main function. You should add prototypes above your main function -- in exactly the format you've done already at the top:

void shc(); /* This is a function prototype */

(though this appears to be unused in your code).

Step 4:

As mentioned in the comments, some of your scanfs use the %s format specifier instead of %d -- you are reading in ints in all cases, so they should all use %d.

Step 5:

As mentioned in the comments above, you shouldn't be re-using the name of a function as a local variable. Although it is possible, it is bad style and will only lead to confusion.

Step 6:

Work on fixing the indentation in your code to be more consistent throughout -- it not only makes it easier to read & understand, but above all will assist with further debugging as you continue working on it.

Hope this helps!

costaparas
  • 5,047
  • 11
  • 16
  • 26
0

I see that this question has been answered now, but I had already written most of this when I saw that, so I'll post anyway.

A few things:

1

When it says implicit declaration of function or undefined reference to, it means that the you have used a variable name, function name, etc. that the compiler hasn't seen before. In this case, the cases in your switch statement are calling functions that you haven't declared. You do declare and define your functions later in your code, but the compiler reads your code top-to-bottom, so when it reaches the function call qoh, for example, it hasn't seen a declaration of that function before, so it throws an error. You can fix this (as noted in the comments) by either moving your function definitions above main or (better) by putting function prototypes above main (like what you have done for shc). This tells the compiler that function will exist, so it doesn't throw an error, but still allows you to have main near the top of your program (good for readability). You can do this by copying and pasting the first line of your function definition (e.g. void cit(){ to the top of your program above main, and simply ending it with a semicolon instead of an opening curly brace (again, just as you have done for shc).

2

As noted in the other answer, void main() is incorrect. Normally, when a function returns a value, that value can then be used wherever the function was called, for example you could call a function with int x = func(3), and then x would be equal to the return value of func. It is different for main. main's return value becomes the exit code of the program, which is essentially an indicator of what caused the program to exit. By convention, an exit code of 0 means the program ran successfully and exited normally. Any other exit code means a problem (and, if you were writing a program that was going to be distributed to users, you might define what each exit code means in your documentation). So, while it is different from other functions, main does return a value, so you should replace void main() with int main(). You should also have main return 0 if your program runs successfully, by adding return 0; to the end of main.

3

The ending curly brace for main should be before your other function definitions.

4

As noted in the comments and the other answer, use %d rather than %s in your scanfs if you are reading ints.

5

In your switch statement, in the third case, you call a shcm function which you don't define anywhere. You have a prototype at the top for an shc function, so I'm not sure what your intention is. You should define the shcm function and add a prototype for it, or define an shc function and call it instead of shcm

6

While you can get away with it, it is bad practice to use a variable with the same name as a function, just because it causes confusion and makes your code harder to read. What you name your variables instead is up to you.

7

Your style overall is very difficult to read. More consistent indentation would make it more readable, as well as making it easier to spot bracket issues like the one you have with main.

AJ-Williams1
  • 126
  • 1
  • 8