输入字母时,Dev C ++程序立即关闭

时间:2016-08-08 11:19:29

标签: c++ dev-c++

当我输入一封信时,我的节目立即关闭。它显示了其余的代码,但我没有进入其他部分。输入字母时程序立即关闭,但在输入数字时保留,直到程序应显示复制信息的部分。我尝试在 $(document).on('click','div that contains the buttons selector', function() .. 之后添加getchar();,但它会跳过行,我只能输入一些信息。这是我的代码: *我是一个极端的新手,这是迄今为止我用过的最长的代码。

cin<<a;

*修改:将 #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。现在唯一的问题是程序在应该显示输出时关闭,当在输入中输入空格时,下一个问题在一行中。

2 个答案:

答案 0 :(得分:0)

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

std::cin >> x;
如果std :: cin以不能作为float或整数类型的前缀的字符开头,则

不读取std :: cin中的字母。

因此x保持为0,输入流保持不变。

下一个

std::cin >> y;

如果类型为float或integer,则会出现相同的行为:y保持为0,输入流std :: cin仍然以float或整数类型无法读取的字符开头。

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

答案 1 :(得分:0)

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 e.t.c这样的值。只要您为此data type输入任何无效字符,此程序就会退出。

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

P.S:我假设var的类型为string