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
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
#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();
} 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:
count<< instead of cout<<) is a common compile‑time cause; compiler error text usually points to the line. 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. -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.
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.
Also in line 11:count<<"enter second amount";
It should becout << "enter second amount";
In addition to what the previous posters said, you may want to consider not using the identifier "sum" as a variable.
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;
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.