【问题标题】:Dev C++ program closes immediately when entering letters输入字母时,Dev C++ 程序立即关闭
【发布时间】:2016-12-14 03:37:52
【问题描述】:

当我输入一个字母时,我的程序立即关闭。它显示了代码的其余部分,但我没有进入它的其他部分。程序在输入字母时立即关闭,但在输入数字时仍然存在,直到程序应该显示复制信息的部分。我试过在每个cin<<a; 之后加上getchar();,但它会跳过行,我只能输入很少的信息。这是我的代码: * 我是一个极端的新手,这是迄今为止我写过的最长的代码。

 #include <iostream>
  #include <stdio.h>
  #include <stdlib.h>
  #include <string>

using namespace std;

int main()

{

string a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,y,z;

cout<<"Enter Your Name:";
cin>>a;


cout<<"Enter Your Gender:";
cin>>z;

cout<<"Enter Your Age:";
cin>>b;

cout<<"Enter Your Address:";
cin>>c;

cout<<"Enter Your School:";
cin>>d;

cout<<"Enter Your Nickname:";
cin>>e;

cout<<"Enter Highest Educational Attainment:";
cin>>f;

cout<<"What are your skills?:";
cin>>g;

cout<<"How many years experience do you have in this field?:";
cin>>h;

cout<<"What kind of people would you like to have in the workplace?:";
cin>>i;

cout<<"What good values do you have?:";
cin>>j;

cout<<"What bad values do you have?:";
cin>>k;

cout<<"When is your birthday?:";
cin>>l;

cout<<"What is your Father's name?:";
cin>>m;

cout<<"What is your Mother's name?:";
cin>>n;

cout<<"Do you have any children?:";
cin>>o;

cout<<"What is your eye color?:";
cin>>y;

cout<<"What do you dislike?:";
cin>>p;

cout<<"How many are you in the family?:";
cin>>q;

cout<<"What is your favorite food?:";
cin>>r;

cout<<"Your name is:"<<a<<endl;
cout<<"You are a:"<<z<<endl;



cout<<"You are"<<b<<cout<<"years old."<<endl;
cout<<"You live in:"<<c<<endl;
cout<<"You studied in:"<<d<<endl;
cout<<"Your nickname is:"<<e<<endl;
cout<<"Your highest educational attainment is:"<<f<<endl;
cout<<"Your skills are:"<<g<<endl;
cout<<"You have"<<h<<cout<<"years of experience in this field."<<endl;
cout<<"You would like to have"<<i<<cout<<"in the workplace."<<endl;
cout<<"The good thing is, you are:"<<j<<endl;
cout<<"The bad thing is, you are also:"<<k<<endl;
cout<<"Your birthday is in:"<<l<<endl;
cout<<"Your father is:"<<m<<endl;
cout<<"Your mother is:"<<n<<endl;
cout<<"You have"<<o<<cout<<"children."<<endl;
cout<<"You have"<<y<<"eyes."<<endl;
cout<<"You dislike:"<<p<<endl;
cout<<"You are"<<q<<cout<<"in the family"<<endl;
cout<<"Your favorite food is:"<<r<<endl;



return 0;

system ("pause");


}

*编辑:将float 替换为string,并在代码开头添加#include &lt;string&gt;。现在唯一的问题是程序在应该显示输出时关闭,当在输入中输入空格时,下一个问题在一行中。

【问题讨论】:

  • 您正在读取浮点数,而不是字符。要读取字符,请将float 更改为string 并在开头添加#include &lt;string&gt;
  • 如果要输入字母,为什么要提取值到floats?
  • 只是为了将来:为变量使用更明确的名称
  • @V. Kravchenko #include &lt;string&gt; 工作,我现在可以输入字母,但它仍然在应该复制输入的部分关闭。此外,在输入中添加空格时,下一个问题会变得混乱
  • float a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,y,z; 哦,我的....

标签: c++ dev-c++


【解决方案1】:

float a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,y,z;

变量a 和其他float 类型。这意味着他们可以存储 1,10.0,12.3546 等值。只要您为此data type 输入任何无效字符,该程序就会退出。

如果您想运行您的程序,请尝试将所有变量声明为 std::string 并通过 getline 获取输入,例如 getline(std::cin,var)

P.S : 我在这里假设var 的类型是string

【讨论】:

  • 我已将float 更改为string 并将#include &lt;string&gt; 放在代码的开头,但它在显示程序输出之前就关闭了,它应该显示。
【解决方案2】:

如果 x 是浮点类型或整数类型

std::cin >> x;

如果 std::cin 以不能作为浮点或整数类型前缀的字符开头,则不从 std::cin 读取字母。

所以 x 保持为 0 并且您的输入流保持不变。

为下一个

std::cin >> y;

对于浮点或整数类型的 y,会发生相同的行为:y 保持为 0,并且您的输入流 std::cin 仍然以浮点或整数类型无法读取的字符开头。

这种行为一直持续到程序结束。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多