C++ Cin.getline无法正确打印,也无法正常工作
在下面的代码中,我试图在文本文件中添加一行信息。目前,我已经尝试了3种不同的选择,但没有一种完全有效。cin.get、cin.getline和cin>> 例如,当我尝试使用cin.getlinename 60时,它将接受信息,但我得到一些打印输出错误,不是编译错误,而是显示错误 1如果我将作业输入为1-Web,它会在cin.getline中接受它,但如果我输入1-Web设计第1部分应用程序并点击enter,我的程序将开始无休止地循环菜单选项,并且不会获取信息 2当我打印文件时,它会打印出来 例: 感谢您的帮助!我可以诚实地说,当我阅读文件时,我已经生疏了。这是我第一次添加到文件中,因此我希望您能解释我为什么会遇到此问题,并可能帮助我解决此问题 下面是我当前的函数,它添加了一行代码和我的主文件代码C++ Cin.getline无法正确打印,也无法正常工作,c++,printing,cin,C++,Printing,Cin,在下面的代码中,我试图在文本文件中添加一行信息。目前,我已经尝试了3种不同的选择,但没有一种完全有效。cin.get、cin.getline和cin>> 例如,当我尝试使用cin.getlinename 60时,它将接受信息,但我得到一些打印输出错误,不是编译错误,而是显示错误 1如果我将作业输入为1-Web,它会在cin.getline中接受它,但如果我输入1-Web设计第1部分应用程序并点击enter,我的程序将开始无休止地循环菜单选项,并且不会获取信息 2当我打印文件时,它会打印出来 例:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
int options = 0;
char name[60];
//string name; //this was when I attempted cin >>
//string assign; // this was when I attempted cin >>
char assign[60];
string date;
string lines;
void addProject () {
cout <<"Enter class name with class number like so: (MCS 220)"<<endl;
cin.getline(name, 60);
cin.ignore();
//cin >> name;
cout <<endl;
cout <<"Enter the Project Number < - > then Project title like so:" <<
" (1 - Web Design Part Application)"<<endl;
cin.getline(assign, 60);
cin.ignore();
//cin >> assign;
cout <<endl;
cout << "Enter the due date for assignment like so: (05-12-2015)"<<endl;
char cMonth[3];
char cDay [3];
char cYear[5];
cin.get(cMonth,3, '-');
cin.ignore(2, '-');
cin.get(cDay, 4, '-');
cin.ignore(2,'/');
cin.get(cYear, 5);
//input into file
ofstream readFile;
readFile.open("list.txt", std::ios::app);
readFile << name << ", " << "Project " << assign << ", "
<< cMonth <<"-"<< cDay <<"-"<< cYear<< endl;
}
//BEGIN MAIN CODE
int main() {
ifstream readFile("list.txt");
if (!readFile){
cout <<"I am sorry but we could not process "<<
"the file information."<<endl;
}
menu();
cin >>options;
cout <<endl;
cout <<"In our system we show Class information."<<endl;
//read and output the file
while (options != 4)
{
//print the listing
if (options == 3){
while (getline(readFile, lines)){
cout << lines<<endl;
}
}
// Add Assignments
if (options == 1){
addProject();
}
menu();
cin >>options;
cout <<endl;
}//end while
readFile.close();
}
我更新了我的代码。问题是cin.ignore被放置在下一个cin变量的右边
char name[256];
char pnum[20];
char title[256];
string date;
string lines;
void addProject () {
cout <<"Enter class name with class number like so: (MCS 220)"<<endl;
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
cin.getline(name, 256, '\n');
//char upperName = toupper(name); /*
for (char *caps = name; *caps != '\0'; ++caps)
{
*caps = std::tolower(*caps);
++caps;
}
cout <<endl;
cout <<"Enter the Project Number: "<<endl;
cin.getline(pnum, 20, '\n');
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
cout<<"Enter the Project's title next: "<<endl;
cin.getline(title, 256, '\n');
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
cout <<endl;
cout << "Enter the due date for assignment like so: (05-12-2015)"<<endl;
char cMonth[3];
char cDay [3];
char cYear[5];
cin.get(cMonth,3, '-');
cin.ignore(2, '-');
cin.get(cDay, 4, '-');
cin.ignore(2, '-');
cin.get(cYear,5);
//input into file
ofstream readFile;
readFile.open("list.txt", std::ios::app);
readFile << name <<",Project "<< pnum << " - " << title <<
", "<< cMonth <<"-" <<cDay <<"-"<< cYear<< endl;
}
首先,您从不检查输入是否失败。知道哪个输入首先失败将对解决您的问题有很大帮助。如果您的意思是要单独排除每个cin,那么我有。如果我注释掉除第一行之外的所有cin.getline,它会接受输入,然后在我的菜单选项中连续循环。第二个cin也是一样。getlineassign。我不是指故障排除。即使在一个似乎可以工作的程序中,在使用输入的值之前,也必须验证输入是否成功。
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
int options = 0;
char name[60];
//string name; //this was when I attempted cin >>
//string assign; // this was when I attempted cin >>
char assign[60];
string date;
string lines;
void addProject () {
cout <<"Enter class name with class number like so: (MCS 220)"<<endl;
cin.getline(name, 60);
cin.ignore();
//cin >> name;
cout <<endl;
cout <<"Enter the Project Number < - > then Project title like so:" <<
" (1 - Web Design Part Application)"<<endl;
cin.getline(assign, 60);
cin.ignore();
//cin >> assign;
cout <<endl;
cout << "Enter the due date for assignment like so: (05-12-2015)"<<endl;
char cMonth[3];
char cDay [3];
char cYear[5];
cin.get(cMonth,3, '-');
cin.ignore(2, '-');
cin.get(cDay, 4, '-');
cin.ignore(2,'/');
cin.get(cYear, 5);
//input into file
ofstream readFile;
readFile.open("list.txt", std::ios::app);
readFile << name << ", " << "Project " << assign << ", "
<< cMonth <<"-"<< cDay <<"-"<< cYear<< endl;
}
//BEGIN MAIN CODE
int main() {
ifstream readFile("list.txt");
if (!readFile){
cout <<"I am sorry but we could not process "<<
"the file information."<<endl;
}
menu();
cin >>options;
cout <<endl;
cout <<"In our system we show Class information."<<endl;
//read and output the file
while (options != 4)
{
//print the listing
if (options == 3){
while (getline(readFile, lines)){
cout << lines<<endl;
}
}
// Add Assignments
if (options == 1){
addProject();
}
menu();
cin >>options;
cout <<endl;
}//end while
readFile.close();
}
char name[256];
char pnum[20];
char title[256];
string date;
string lines;
void addProject () {
cout <<"Enter class name with class number like so: (MCS 220)"<<endl;
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
cin.getline(name, 256, '\n');
//char upperName = toupper(name); /*
for (char *caps = name; *caps != '\0'; ++caps)
{
*caps = std::tolower(*caps);
++caps;
}
cout <<endl;
cout <<"Enter the Project Number: "<<endl;
cin.getline(pnum, 20, '\n');
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
cout<<"Enter the Project's title next: "<<endl;
cin.getline(title, 256, '\n');
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
cout <<endl;
cout << "Enter the due date for assignment like so: (05-12-2015)"<<endl;
char cMonth[3];
char cDay [3];
char cYear[5];
cin.get(cMonth,3, '-');
cin.ignore(2, '-');
cin.get(cDay, 4, '-');
cin.ignore(2, '-');
cin.get(cYear,5);
//input into file
ofstream readFile;
readFile.open("list.txt", std::ios::app);
readFile << name <<",Project "<< pnum << " - " << title <<
", "<< cMonth <<"-" <<cDay <<"-"<< cYear<< endl;
}