Get Sum of 2 Digits

justzamir 1 Tallied Votes 351 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();
}

Dani AI

Generated

Quick summary and practical fixes (building on notes from , , and ): the original errors come from a small typo and mixing C and C++ I/O plus use of a non‑portable header. Use a single I/O style (prefer C++ streams), avoid <conio.h>/getch() for portability, and add simple input validation so bad input doesn't silently break the program.

A compact, safer example that uses only C++ I/O and validates integer input:

#include <iostream>
#include <limits>

int main()
{
    int a = 0, b = 0;
    std::cout << "Enter first integer: ";
    while (!(std::cin >> a)) {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "Please enter a valid integer: ";
    }

    std::cout << "Enter second integer: ";
    while (!(std::cin >> b)) {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "Please enter a valid integer: ";
    }

    long long total = static_cast<long long>(a) + b;
    std::cout << "Total = " << total << '\n';
    return 0;
}

Extra notes and cautions:

  • The typo flagged by (writing count<< instead of cout<<) is a common compile‑time cause; compiler error text usually points to the line.
  • Mixing scanf/printf with cin/cout can lead to surprising buffering behavior; prefer one API.
  • <conio.h> and getch() are non‑standard (primarily MS‑only); use std::cin.get() or run the program in a terminal instead of relying on platform pauses.
  • Use compiler warnings (-Wall -Wextra) to catch typos and suspicious code. For larger sums consider long long to avoid overflow.

These changes address the original problems and make the program more robust for real input.

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 Member #248612
Member #248612

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.