2015-02-01 6 views
0

다음 코드에서 텍스트 파일에 정보 행을 추가하려고합니다. 현재 3 가지 옵션을 시도했지만 아직 완전히 작동하지는 않습니다. (cin.get, cin.getline and cin >>)Cin.getline이 올바르게 인쇄되지 않거나 올바르게 작동하지 않습니다.

예를 들어 cin.getline (name, 60)을 사용하려고하면 정보를 받아들이지 만 몇 가지 출력 오류가 발생합니다 (컴파일되지 않음). 오류는 있지만 잘못 표시됨).

1) 내가 1로 할당 입력하면 - 웹을가 cin.getline에서 그것을 받아들이는하지만 1 개을 입력하면 - 웹 디자인 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(); 
} 
+0

처음에는 입력이 실패했는지 여부를 확인하지 않았습니다. 어떤 입력이 처음 실패했는지 알면 문제를 해결하는 데 많은 도움이됩니다. –

+0

각 cin 개별적으로 문제를 해결하려는 경우 그렇습니다. 첫 번째 것을 제외한 모든 cin.getline을 주석 처리하면 입력을 허용 한 다음 내 메뉴 옵션을 통해 계속 반복합니다. 두 번째 cin.getline (assign)과 동일합니다. – narue1992

+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; 
} 
관련 문제