Cin.getline 无法正确打印 无法正常工作

Cin.getline Won't Print Right nor Work Properly

在我的以下代码中,我试图将一行信息添加到我的文本文件中。目前我已经尝试了 3 种不同的选择,但 none 已经完全奏效了。 (cin.get, cin.get行和 cin >>)

例如,当我尝试使用 cin.getline(name,60) 时,它会接受信息,但我得到一些打印错误(不是编译错误,但它们显示错误)。

1) 如果我将作业键入 1 - Web 它会在 cin.get 行中接受它,但如果我键入 1 - Web Design第 1 部分应用程序 并按下回车键我的程序开始无限循环菜单选项并且不获取信息。

2) 当我打印我的文件时,它打印出奇怪的东西

例如:

Accept from the following options to proceed.
1) Add Assignment
2) Remove Assignment
3) Print Listing
4) Quit
1

In our system we show Class information.
Enter class name with class number like so: (MCS 220)
MCS 299

Enter the Project Number < - > then Project title like so: (1 - Web Design     Part Application)
1 - Web

Enter the due date for assignment like so: (05-12-2015)
05-12-2015

版画:

COMPSCI 181,Project 5 - Web page development,04-14-2015
MCS 220,Project 3 - Java fundamentals,04-15-2015
MCS 220,Project 4 - Array, 04-15-2015
, Project CS 299,  - We-05-1 // Prints this line

感谢任何帮助!老实说,在阅读文件方面我很生疏。这是我第一次添加到文件中,所以我希望你能解释为什么我遇到这个问题,也许可以帮助我解决它。

下面是我当前添加一行的函数和我的主文件代码。

#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 != '[=10=]'; ++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;
}