簡體   English   中英

Cin.getline無法正確打印或正常工作

[英]Cin.getline Won't Print Right nor Work Properly

在下面的代碼中,我試圖在文本文件中添加一行信息。 目前,我嘗試了3種不同的選擇,但沒有一個完全起作用。 (cin.get,cin.getline和cin >>)

例如,當我嘗試使用cin.getline(name,60)時,它將接受該信息,但會出現一些打印輸出錯誤(不是編譯錯誤,但它們顯示錯誤)。

1)如果我將分配鍵入為1-Web,則它在cin.getline中接受它;但是如果我輸入1-Web設計第1部分應用程序,然后按Enter鍵,我的程序將無休止地循環菜單選項,並且不接收信息。

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 != '\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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM