我的程序在到达"cin << ans1 "时崩溃

My Program crashes when it reaches "cin << ans1 "

本文关键字:lt ans1 崩溃 cin 程序 我的      更新时间:2023-10-16

该程序在到达cin >> ans1;时实际上崩溃。 我用atom编写了这段代码,但我在code::blocks中尝试了相同的程序,它运行良好。我的atom编译器有问题吗...

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
string name[30];
string gender;
int age;
void Job()
{
printf("To apply for Job, Plz Enter your name: ");
getline(cin, name[20]);
printf("Enter Your Gender: ");
cin >> gender;
printf("Enter your age: ");
cin >> age;
printf("n");
while (age > 100)
{
printf("Enter a real age Dumbass: ");
cin >> age;
}
return;
}
int main(int argc, char const *argv[])
{
Job();
char ans1;
printf("n");
printf("Entered Information is...n");
cout << "Name: " << name[20] << "       " << "Sex: " << gender << endl;
cout << "Age: " << age;
printf("Is this Correct?(y/n): ");
cin >> ans1;
if (ans1 == 'y')
{
if (age < 60 && age > 14 )
{
printf("It seems that you are eligible to vote...n");
printf("Go to our Website to Sign up...n");   /* code */
}
else
{
printf("Sorry... But you are not Eligable to be hired...n");
}
}
else
{
while(ans1 != 'y')
{
printf("Write again...n");
Job();
}
}
return 0;
}

首先,将ans1 != y放在 while 循环中没有任何意义,因为ans1不会改变,也不会发生任何新情况,请尝试使用它

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
string name[30];
string gender;
int age;
void Job()
{
printf("To apply for Job, Plz Enter your name: ");
getline(cin, name[20]);
printf("Enter Your Gender: ");
cin >> gender;
printf("Enter your age: ");
cin >> age;
printf("n");
while (age > 100)
{
printf("Enter a real age Dumbass: ");
cin >> age;
}
return;
}
void h()
{
Job();
char ans1;
printf("n");
printf("Entered Information is...n");
cout << "Name: " << name[20] << "       " << "Sex: " << gender << endl;
cout << "Age: " << age;
printf("Is this Correct?(y/n): ");
cin >> ans1;
if (ans1 == 'y')
{
if (age < 60 && age > 14 )
{
printf("It seems that you are eligible to vote...n");
printf("Go to our Website to Sign up...n");   /* code */
}
else
{
printf("Sorry... But you are not Eligable to be hired...n");
}
}
else
{
if(ans1 != 'y')
{
printf("Write again...n");
h();
}
}
}
int main()
{
h();
}