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...
My First Code:

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

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

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

void main()
{
    int total,Ares,Bres;
    clrscr();
    A(&Ares);
    B(&Bres);
    total=Ares+Bres;
    printf("A + B = %d \n",total);
    getch();
}

That works just fine, but with a lower range of numbers
...
So then I tried converting all "int" to "float"

#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)
{
    int 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();
}

Then I got an output but is a disaster

Please Help...

Recommended Answers

All 9 Replies

You said you were learning C++ ... but did you realize that your code was in the C style?

In C++, one can pass by reference ... and so will not have to dereference a value passed into a function using the * dereference operator (that is used with passed in pointers that one wants to dereference)

And note that main returns an int

so it is ...
int main() // not void main()

commented: Thanks! +0

At line 14 you still have a and b declared as int but you scanf them as floats at line 16 which will produce the strange results you are seeing.

commented: Fixed the code but it doesn't fixed the problem, It gives me 0.00 +0

Actually sir, Our professor start teaching us by C++ Forgive me sir,
I tried using pass by reference but still not working right please try my code in your C++ ...

Here is a start to some C++ revision of your code ...

#include <iostream>
// #include <conio.h> // avoid as it is not standard so code will not be portable


// take in two integers and return the sum 

void takeInAandBandReturnSum( int& result )
{
    bool ok = false;
    while( !ok )
    {
        int a, b;
        std::cout << "Enter Numbers a and b separated by a space : " << std::flush;
        if( std::cin >> a >> b && std::cin.get() == '\n' )
        {
            result = a + b;
            ok = true;
        }
        else
        {
            std::cin.clear(); // clear cin error flags
            std::cin.sync(); // 'flush' cin stream ...
            std::cout << "\nInvalid data ... try again.\n";
        }
    }
}

Do not include <conio.h> unless you are using the functions etc. defined in there. Besides, it is not part of the standard and your code might not work with other compliers. Before each scanf(), put fflush(STDIN); to make sure the input stream is empty.

Another thing to do is check the return value of scanf() which will tell you how many good values were read. If it does not match the number of variables that you were expecting, something went wrong; probably the user entered bad data.

while (1)
{
    fflush(STDIN);
    printf("Please enter two numbers, seperated by a space: ");
    if (scanf("%f %f", &a, &b) == 2)
        break;
    printf("\nThat was not valid input.  Please try again.\n");
}

Besides, it is not part of the standard and your code might not work with other compliers. Before each scanf(), put fflush(STDIN); to make sure the input stream is empty.

Um...I hate to break it to you, but 1) C++ is case sensitive and STDIN is not a valid alias for stdin, and 2) fflush() isn't defined for input streams. So in one sentence you attacked non-standard code and then in the next sentence advocated non-standard code. ;)

Another thing to do is check the return value of scanf() which will tell you how many good values were read. If it does not match the number of variables that you were expecting, something went wrong; probably the user entered bad data.

Failure to check the return value of scanf() is probably one of the most common problems I see in beginner code.

commented: no need to be bashful ... it all needed to be corrected +0

That's what I get for just doing that code from memory without testing (and without sleep) first. Where did I see STDIN, STDOUT and STDERR capitalized? Right, the file handle's type is defined in capitals (FILE), not the handles themselves.

Guys, I know the replies to my question here is right, but what is wrong is my question... I realized that I am using C++, but I am learning only C ... I'll end this and post the correct question :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.