2016-08-08 60 views
-5

當我輸入一個字母時,程序立即關閉。它顯示了代碼的其餘部分,但我沒有進入其他部分。程序在輸入字母時立即關閉,但在輸入數字時仍然保留,直到程序應顯示覆制信息的部分。我曾嘗試在每cin<<a;之後放置getchar();,但它會跳過線條,而我只能輸入很少的信息。這裏是我的代碼: *我是一個極端的新手,這是迄今爲止我工作過的最長的代碼。Dev C++程序在輸入字母時立即關閉

#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"); 


} 

*編輯:用string替換float,並在代碼的開始增加#include <string>。現在唯一的問題是程序關閉時,它應該顯示輸出,當輸入一個空格時,下一個問題在一行中。

+1

您正在閱讀的是浮點數字,而不是字符。要讀取字符,請將'float'更改爲'string'並在開始處添加'#include ' –

+2

如果要輸入字母,爲什麼要將值提取到'float'中? –

+4

只是爲了將來:使用更明確的變量名稱 – Garf365

回答

0

如果x是一個浮子式或整數類型

std::cin >> x; 

讀取沒有自std ::信CIN如果給std :: cin與字符開始不能成爲一個浮動或整數類型的前綴。

所以x保持爲0,您的輸入流保持不變。

在接下來

std::cin >> y; 

與float類型或整數的Y,發生相同的行爲:Y保持爲0,並且您的輸入流的std :: CIN仍不能由一個被讀出的字符開始浮點型或整型。

此行爲持續到您的程序結束。

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;

可變afloat類型。這意味着他們可以存儲像1,10.0,12.3546 e.t.c這樣的值。只要你輸入這個data type的任何無效字符,該程序將退出。

如果你想運行你的程序,嘗試聲明所有變量爲std::string,並通過getline這樣輸入,像這樣getline(std::cin,var)

P.S:我在這裏假設varstring類型。

+0

我已將'float'更改爲'string',並在代碼的開頭放置了'#include ',但在它顯示應該顯示的程序輸出之前關閉。 – Oblivionchain