-2
#include <stdio.h> //adding program
int main()
{
    int iOperand1 = 0;
    int iOperand2 = 0;

    printf("\n\tAdder Program, by Keith Davenport\n");
    printf("\nEnter first operand: ");
    scanf_s("%d", &iOperand1);

    printf("Enter second operand:");
    scanf_s("%d", &iOperand2);

    printf("The result is %d\n", iOperand1 + iOperand2);
    return 0;
}

int revenueProgram()  //Revenue program
{
    float fRevenue, fCost;      

    printf("\nEnter total revenue:");
    scanf_s("%f", &fRevenue);

    printf("\nEnter total cost:");
    scanf_s("%f", &fCost);

    printf("\nYour profit os $%.2f\n", fRevenue - fCost);
    return 0;
}

That is my code. I ran it, and the program only ran the first program. It did not run the second program. Why does it happen? and How to fix it? Please help. I am still new to C programming.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • You need to call the function from main. And the function should be declared before its call for example before main. – Vlad from Moscow Jan 11 '22 at 15:51
  • you did not call the function in main, write `revenueProgram()` before `return` – Gresta Jan 11 '22 at 15:52
  • 1
    There are *not* two programmes, just a single one containing two *functions*, one of being the `main` function, the entry point to your programme, and the other one not being called... – Aconcagua Jan 11 '22 at 15:52
  • 4
    You urgently need a beginner's book on C. You cannot learn by accident. – the busybee Jan 11 '22 at 15:52
  • Before you can call another function its *declaration* needs to be available, so you need: `int revenueProgram(); int main(void) { /* ... */ } int revenueProgram() { /* ... */ }` – then you can call the second function from within `main`: `int main(void) { /* ... */ revenueProgram(); return 0; }` – Aconcagua Jan 11 '22 at 15:53

1 Answers1

1

You need to call the second function revenueProgram() inside of your main() function, before return 0;. Also, declare the second function before main().

Something like:

#include <stdio.h> //adding program

int revenueProgram(); //function prototype declaration

int main()
{
    int iOperand1 = 0;
    int iOperand2 = 0;

    printf("\n\tAdder Program, by Keith Davenport\n");
    printf("\nEnter first operand: ");
    scanf_s("%d", &iOperand1);

    printf("Enter second operand:");
    scanf_s("%d", &iOperand2);

    printf("The result is %d\n", iOperand1 + iOperand2);

    revenueProgram();

    return 0;
}

int revenueProgram()  //Revenue program
{
    float fRevenue, fCost;      

    printf("\nEnter total revenue:");
    scanf_s("%f", &fRevenue);

    printf("\nEnter total cost:");
    scanf_s("%f", &fCost);

    printf("\nYour profit os $%.2f\n", fRevenue - fCost);
    return 0;
}
fungible
  • 129
  • 4