Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Calculate addition of two integers and print output.

Sample input: 3 and 1
Sample Output: 4

Sample input: 'a' and 'b'
Sample output: invalid input

And sample input should be hardcoded and should display as given above

Finally i don't know how to handle such condition please help

What I have tried:

I have tried ASCII comparison to distinguish between int value and char value.
Posted
Updated 7-Jun-16 5:45am
Comments
CHill60 7-Jun-16 10:25am    
The "What I have tried" section is for you to put the code that you have tried.
Are you aware of the isdigit function? isdigit - C++ Reference[^]
Rahul Thengadi 7-Jun-16 10:31am    
As the int num1 = 'a';
Here num1 = 97 // ASCII value of 'a'
isdigit can't handle it
CHill60 7-Jun-16 10:58am    
If you want to reply to a comment use the "Reply" link so that the poster is notified of your response.
isdigit can handle 'a' - there is absolutely no need to get the ascii value of 'a'!
Rahul Thengadi 7-Jun-16 11:11am    
#include<iostream>

using std::cout;
using std::cin;
using std::endl;

int add(int num1,int num2)
{
if(isdigit(num1) != 0 && isdigit(num2) != 0)
{
cout << num1+num2;
}
else
cout << "Error" << endl;
}

int main()
{
int num1 = 3;
int num2 = 1;

add(num1,num2);

num1 = 'a'; // here num1 value is 97 i.e. ASCII value of character a
num2 = 'b'; // here num1 value is 97 i.e. ASCII value of character a

add(num1,num2);

return 0;
}

Output: Error
Error
Rahul Thengadi 7-Jun-16 15:35pm    
Thanks , the validNum() is much useful, i was nearly close to the answer

Maybe you want something like this:

C++
#include<cstdlib>
#include<iostream>
#include<limits>
using namespace std;

int main(){
	cout << "Insert a number -> ";
	int a;
	while((cin >> a).fail()){
	    cout << "Bad value!";
	    cin.clear();
	    cin.ignore(numeric_limits<streamsize>::max(), '\n');
	}
	cout << "The number is -> " <<a <<endl;
	system("PAUSE");
	return 0;
}
</streamsize></limits></iostream></cstdlib>


The program above just displays the number a, you need to modify it a little to get it to work with the sum of two numbers.

How does it work
cin.fail() returns true if it fails to extract data from the input that can go inside the given variable (in this case, a). If it fails, cin.clear() resets the state of the error flag and cin.ignore(numeric_limits<streamsize>::max(), '\n')</streamsize> ignores the wrong input.

Hope this helps.
 
Share this answer
 
Comments
Rahul Thengadi 7-Jun-16 10:57am    
Ya i think but the inputs were hard coded
LLLLGGGG 7-Jun-16 11:36am    
What do you mean by "hard coded"? That you've redefined cin? That the sum is a function?
Rahul Thengadi 7-Jun-16 11:41am    
Hardcoded means sample input values should written in program i.e. program should not prompt for input values to user
Here is my justification for my comments above where I say isdigit will work (when it is used properly).

In this case you have
Quote:
And sample input should be hardcoded and should display as given above
In most cases (or as you move through your course) inputs will come from the command line (or parameters) in which case it is safe to assume any key can be used ... so handle strings.
Lusvardi has given a good example of how to do this.

My snippet below is intended to show you how isdigit would work in this instance - see the function ValidNum
C++
#include<iostream>
#include<string>
using namespace std;

void ProcessSample(string num1, string num2);
bool ValidNum(string num);
void OutputResult(int num1, int num2);

void main(){

	//Sample input is hardcoded - should really be a loop of input

	//Sample input: 3 and 1 : Expected Output: 4
	ProcessSample("3","1");

	//Sample input: 'a' and 'b' : Expected Output 'Invalid input'
	ProcessSample("a","b");

	//Sample input: '1' and 'b' : Expected Output 'Invalid input'
	ProcessSample("1","b");

	//Sample input: 23 and 101 : Expected Output: 124
	ProcessSample("23","101");

	cout << "Hit a key to end";
	getchar();

	return;
}
void ProcessSample(string num1, string num2)
{
	if(ValidNum(num1) && ValidNum(num2))
		cout << "Result: " << stoi(num1) + stoi(num2) << endl;
	else
		cout << "Invalid Input" << endl;
}
// Validates that the input represents a number
// Should handle any number of digits up to MAXINT in length
bool ValidNum(string num)
{
	bool ret = true;
	for(int i = 0; i < num.length(); i++)
		if(!isdigit(num[i])) ret = false;
	return ret;
}
 
Share this answer
 
Comments
Rahul Thengadi 7-Jun-16 15:23pm    
Finally getting 'stoi' was not declared in this scope
CHill60 7-Jun-16 15:45pm    
I only get that error if I remove #include<string> - did you include that standard header?
Rahul Thengadi 7-Jun-16 15:47pm    
Ya it is in my program
Rahul Thengadi 7-Jun-16 15:49pm    
Probably the compiler version doesn't support stoi()
CHill60 8-Jun-16 5:04am    
Possible - just replace that with the equivalent for your set up - it's just converting a string to it's integer equivalent - NOTE ... it is NOT getting the ascii value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900