Get Sum of 2 Digits

justzamir 1 Tallied Votes 337 Views Share

I am totaly new to C++, and i am trying to get sum of two digits, but i am getting some error, will somebody please correct my code.

Regards

ilama commented: isy try karo +0
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int a,b,sum;
    cout<<"enter first amount";
    scanf ("%d", &a);
    count<<"enter second amount";
    cin>>b;
    sum=a+b;
    printf ("sum=%d", sum);
    
	getch();
}
L7Sqr 227 Practically a Master Poster

What error are you getting - compiler/runtime? I can see that scanf might be an issue as you don't include <cstdio> (I'm unclear if <conio.h> does that for you). Also, you are mixing input mechanisms (cin and scanf) - it is best to pick one and be consistent.

Also, you posted a code snippet. You should not post questions like these as code snippets but as new questions.

nullptr 167 Occasional Poster

Also in line 11:
count<<"enter second amount";
It should be
cout << "enter second amount";

L7Sqr commented: Nice catch :) +9
jtodd 0 Junior Poster in Training

In addition to what the previous posters said, you may want to consider not using the identifier "sum" as a variable.

Member Avatar for jencas
jencas

This should work (albeit untested):

#include <iostream>


using namespace std;

int main()
{
    int a, b, s;
    cout << "enter first amount";
    cin >> a;
    cout << "enter second amount";
    cin >> b;
    s = a + b;
    cout << "sum = " << s;
    std::cin.get();
    return 0;
}
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.