Cin.getline不会#39;打印正确无法正常工作

时间:2015-02-01 15:29:39

标签: c++ printing cin

在我的下面的代码中,我试图在我的文本文件中添加一行信息。目前我尝试了3种不同的选择,但没有一种完全有效。 (cin.get,cin.getline和cin>>)

例如,当我尝试使用cin.getline(name,60)时,它会接受这些信息,但我会收到一些打印输出错误(没有编译错误但显示错误)。

1)如果我将作业键入 1 - Web ,它会在cin.getline中接受它,但是如果我输入 1 - Web Design Part 1 Application 并且我点击了输入我的程序开始无休止地循环菜单选项并且不接收信息。

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

PRINTS:

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();
}

1 个答案:

答案 0 :(得分:0)

我更新了我的代码。问题是将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;
}