dev dev c++程序在输入字母时立即关闭

dev Dev C++ program closes immediately when entering letters

本文关键字:dev c++ 程序 输入      更新时间:2023-10-16

输入字母后程序立即关闭。它显示了其余的代码,但我不能输入它的其他部分。当输入字母时,程序立即关闭,但当输入数字时,程序保持不变,直到程序应该显示复制信息的部分。我试过把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,并在代码开头添加#include <string>。现在唯一的问题是程序在应该显示输出时关闭,并且在输入中输入空格时,下一个问题在一行中。

如果x是float类型或integer类型

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

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

所以x保持为0,你的输入流保持不变。

下一个

std::cin >> y;

如果y为浮点型或整型,则会发生相同的行为:y保持为0,并且您的输入流std::cin仍然以浮点型或整型不能读取的字符开始。

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

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