C ++ cin读取字符串到int类型返回0

时间:2017-03-28 21:11:03

标签: c++

#include <iostream>
using namespace std;
int main()
{
    cout << "Please enter your name and age\n";
    string first_name;
    int age;
    cin >> first_name;
    cin >> age;
    cout << "Hello, " << first_name << " (age " << age << ")!\n";
}

如果我输入Carlos age或任何其他字符串,我会获得0。这是如何工作的(为什么)?

2 个答案:

答案 0 :(得分:9)

不确定为什么这个问题得到了落实/关闭,因为它已经明确说明而且不广泛(从评论来看,很多人都不知道答案)......

无论如何,对于代码:

age

如果输入流不包含任何数字,则流将进入失败状态,0设置为operator>>。输入流中的字符保留在那里;清除失败状态后,可以通过将来的读操作读取它们。

String xml_entrada = "D:\\CeslySoft\\Ivap_facturador\\CPE\\FirmaXML\\Schema-20480510144-RC-20170327-0001.xml"; String xml_salida = "D:\\CeslySoft\\Ivap_facturador\\CPE\\FirmaXML\\20480510144-RC-20170327-0001.xml"; String certi_digital = "D:\\CeslySoft\\Ivap_facturador\\Certificados\\molchiclayo1.jks"; String clave = "9ghi0nmbR0ft"; String alias = "1"; String tipodoc = "09"; int indice = (tipodoc.equals("09")? 0: 1); XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM"); Reference ref = fac.newReference("", fac.newDigestMethod(DigestMethod.SHA1,null), Collections.singletonList(fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)), null,null); SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(ref)); KeyStore ks = KeyStore.getInstance("JKS"); ks.load(new FileInputStream(certi_digital), clave.toCharArray()); KeyStore.PrivateKeyEntry keyEntry = (KeyStore.PrivateKeyEntry) ks.getEntry(alias, new KeyStore.PasswordProtection(clave.toCharArray())); X509Certificate cert = (X509Certificate) keyEntry.getCertificate(); KeyInfoFactory kif = fac.getKeyInfoFactory(); List<Object> x509content = new ArrayList<>(); x509content.add(cert.getSubjectX500Principal().getName()); x509content.add(cert); X509Data xd = kif.newX509Data(x509content); KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd)); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); //Document doc = dbf.newDocumentBuilder().parse(new FileInputStream(xml_entrada)); InputSource is = new InputSource(new InputStreamReader(new FileInputStream(xml_entrada), "ISO-8859-1")); Document doc = dbf.newDocumentBuilder().parse(is); Node nodePadre = doc.getElementsByTagName("ext:ExtensionContent").item(indice); nodePadre.getNodeValue(); DOMSignContext dsc = new DOMSignContext(keyEntry.getPrivateKey(), nodePadre); XMLSignature signature = fac.newXMLSignature(si, ki, null, "SignatureSP", null); signature.sign(dsc); OutputStream os = new FileOutputStream(xml_salida); TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); trans.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); trans.transform(new DOMSource(doc), new StreamResult(os)); 的行为是summarized on cppreference,标准中的完整描述有点复杂。

注意:这在C ++ 11中有所改变;报告垃圾输出的评论员要么是在C ++ 11之前的模式下运行编译器,要么是有问题的。

答案 1 :(得分:3)

如果输入无法转换为int(在您的情况下),则failbit将设置为std::cin。这可以通过调用std::cin.fail()来检索。

 std::cin >> age;
 if (std::cin.fail()) { 
     std::cout << "data entered is not of int type"; 
 }

您也可以使用!std::cin代替std::cin.fail()