0

Hi there, I'm just new to programming and C++
...
I have trouble with my code
At first I am using "int" for variables but then I realize that i need a much larger range of numbers and also support for decimal operations so I change all "int" to "float"...
Code:

#include <stdio.h>
#include <conio.h>

void A(float& res)
{
    float a,b;
    printf("Enter Numbers A: \n");
    scanf("%.2f %.2f",&a,&b);
    res=a+b;
}

void B(float& res)
{
    float a,b;
    printf("Enter Numbers B: \n");
    scanf("%.2f %.2f",&a,&b);
    res=a+b;
}

void main()
{
    float total,Ares,Bres;
    clrscr();
    A(Ares);
    B(Bres);
    total=Ares+Bres;
    printf("A + B = %.2f \n",total);
    getch();
}

There's no error in compiling, but output is a disaster.. :(

Edited by jandanielsaclolo: add comment

2
Contributors
3
Replies
26
Views
3 Years
Discussion Span
Last Post by Gonbe
Featured Replies
  • 2

    #include <stdio.h> void A(float* res) { float a, b; printf("Enter Numbers A: \n"); scanf(" %f %f",&a,&b); (*res) = a+b; } void B(float* res) { float a, b; printf("Enter Numbers B: \n"); scanf(" %f %f", &a, &b); (*res) = a + b; } int main(void) { float total,Ares,Bres; A(&Ares); B(&Bres); total= … Read More

  • Thanks man! but can I do by using Pass by Reference? not Pass by Pointers? Read More

  • 1

    Thanks man! but can I do by using Pass by Reference? not Pass by Pointers? Not in C, that's a C++ feature. Closest thing you could do is make the pointers you pass const. (e.g `float* const res`) Read More

2
#include <stdio.h>

void A(float* res)
{
    float a, b;
    printf("Enter Numbers A: \n");
    scanf(" %f %f",&a,&b);
    (*res) = a+b;
}

void B(float* res)
{
    float a, b;
    printf("Enter Numbers B: \n");
    scanf(" %f %f", &a, &b);
    (*res) = a + b;
}

int main(void)
{
    float total,Ares,Bres;

    A(&Ares);
    B(&Bres);

    total= Ares + Bres;
    printf("A + B = %.2f \n",total);

    return 0;
}
Votes + Comments
Thanks!
1
Thanks man! but can I do by using Pass by Reference? not Pass by Pointers?

Not in C, that's a C++ feature. Closest thing you could do is make the pointers you pass const. (e.g float* const res)

Edited by Gonbe

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.