处理int num1 ='a'; [英] Handling int num1 = 'a';

查看:103
本文介绍了处理int num1 ='a';的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计算两个整数和打印输出的加法。



样本输入:3和1

样本输出:4



样本输入:'a'和'b'

样本输出:无效输入



样品输入应该是硬编码的,应该如上所示显示



最后我不知道如何处理这种情况请帮助



我尝试了什么:



我尝试过ASCII比较来区分int值和char值。

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.

推荐答案

也许你想要这样的东西:



Maybe you want something like this:

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





上面的程序只显示数字a,您需要稍微修改它以使其与两个数字的总和一起使用。



工作原理

cin.fail()返回true如果它无法从输入中提取可以进入给定变量的数据(在这种情况下, a )。如果失败, cin.clear()重置错误标志的状态和 cin.ignore(numeric_limits< streamsize> :: max(), '\ n')< / streamsize> 忽略错误的输入。



希望这会有所帮助。



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.


这是我在上面的评论的理由,我说 isdigit 将起作用(当它正确使用时)。



在这种情况下,您有
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

在大多数情况下(或当你在课程中移动时)输入将来自命令行(或参数),在这种情况下可以安全地假设任何键都可以使用...所以句柄 string s。

Lusvardi举了一个如何做到这一点的好例子。



我的下面的代码片段旨在向您展示 isdigit 如何在此实例中工作 - 请参阅函数 ValidNum

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

#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;
}


这篇关于处理int num1 ='a';的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆