2017-05-06 6 views
2

내 소개의 C++ 클래스에 대한 최종 프로젝트를 진행하고 있으며 코드에 문제가 있습니다. getline은 제대로 작동하지 않는 것 같습니다. 다른 시간대와 다른 곳에서 본 것처럼 정확하게 입력했습니다. 여기에 질문이 실행getline을 사용하는 데 문제가 있습니다

if (groceryMenu == 2) 
     { 
      string groceryItem; 

      system("CLS"); 

      cout << "Enter what you would like to add to the grocery list: " << endl; 
      getline(cin, groceryItem); 

      groceryVector.push_back(groceryItem); 

코드의 섹션이의 COUT 라인은 화면에 보여줍니다 (그것은 단지 깜박하지만, 당신이 그것을 유지 볼 수 후 시스템 ("PAUSE")를) 다음 있지만, if 루프를 빠져 나와 주 루프로 돌아갑니다. 나는 내가 여기서 잘못하고있는 것을 알아낼 수 없다. 어떤 도움을 주신 것입니다 :)

여기 내 코드의 나머지는 도움이된다면. 나는 그것이 거친 것을 안다. 방금 시작 했어. 당신이 을 할 수 있기 때문에

// 7.3 lists and vectors 
#include<iostream> 
#include<iomanip> 
#include<string> 
#include<vector> 
#include<fstream> 
#include "stdafx.h" 
using namespace std; 


int main() 
{ 
    int menuInput = 0; 
    int exitProgram = 0; 
    vector<string> groceryVector; 
    vector<string> hardwareVector; 
    vector<string> choreVector; 
    fstream inputFile, outputFile; 

    string groceryInput; 
    inputFile.open("grocery.txt"); 

    while (getline(inputFile, groceryInput)) 
    { 
     groceryVector.push_back(groceryInput); 
    } 
    inputFile.close(); 

    string hardwareInput; 
    inputFile.open("hardware.txt"); 

    while (getline(inputFile, hardwareInput)) 
    { 
     hardwareVector.push_back(hardwareInput); 
    } 
    inputFile.close(); 

    string choreInput; 
    inputFile.open("chore.txt"); 

    while (getline(inputFile, choreInput)) 
    { 
     choreVector.push_back(choreInput); 
    } 
    inputFile.close(); 

    while (exitProgram == 0) 
    { 
     system("CLS"); 

     cout << "List Manager" << endl; 
     cout << "Press 1 to manage the grocery list." << endl; 
     cout << "Press 2 to manage the hardware store list." << endl; 
     cout << "Press 3 to manage the chore list." << endl; 
     cout << "Press 4 to exit." << endl; 

     cin >> menuInput; 

     if (menuInput == 4) 
     { 
      system("CLS"); 
      cout << "Now exiting program." << endl; 
      exitProgram = 2; 
      break; 
     } 

     while (menuInput == 1) 
     { 
      system("CLS"); 

      int groceryMenu = 0; 

      cout << "Press 1 to read the grocery list." << endl; 
      cout << "Press 2 to add an item to the list." << endl; 
      cout << "Press 3 to delete an item from the list." << endl; 
      cout << "Press 4 to return to the main menu." << endl; 

      cin >> groceryMenu; 

      if (groceryMenu == 1) 
      { 
       system("CLS"); 

       for (string groceryList : groceryVector) 
       { 
        cout << groceryList << endl; 
       } 

       system("PAUSE"); 
      } 

      if (groceryMenu == 2) 
      { 
       string groceryItem; 

       system("CLS"); 

       cout << "Enter what you would like to add to the grocery list: " << endl; 
       getline(cin, groceryItem); 

       groceryVector.push_back(groceryItem); 

      } 

      if (groceryMenu == 3) 
      { 
       int eraseLine = 0; 

       system("CLS"); 
       cout << "What line would you like to erase from the list?" << endl; 
       cin >> eraseLine; 

       groceryVector.erase(groceryVector.begin() + (eraseLine - 1)); 
      } 

      outputFile.open("grocery.txt"); 

      for (string groceryList : groceryVector) 
      { 
       outputFile << groceryList << endl; 
      } 
      outputFile.close(); 

      if (groceryMenu == 4) 
      { 
       menuInput = 0; 
      } 
     } 

     while (menuInput == 2) 
     { 
      system("CLS"); 

      int hardwareMenu = 0; 

      cout << "Press 1 to read the hardware list." << endl; 
      cout << "Press 2 to add an item to the list." << endl; 
      cout << "Press 3 to delete an item from the list." << endl; 
      cout << "Press 4 to return to the main menu." << endl; 

      cin >> hardwareMenu; 

      if (hardwareMenu == 1) 
      { 
       system("CLS"); 

       for (string hardwareList : hardwareVector) 
       { 
        cout << hardwareList << endl; 
       } 

       system("PAUSE"); 
      } 

      if (hardwareMenu == 2) 
      { 
       string hardwareItem; 

       system("CLS"); 

       cout << "Enter what you would like to add to the hardware list: " << endl; 
       getline(cin, hardwareItem); 

       hardwareVector.push_back(hardwareItem); 
      } 

      if (hardwareMenu == 3) 
      { 
       int eraseLine = 0; 

       system("CLS"); 
       cout << "What line would you like to erase from the list?" << endl; 
       cin >> eraseLine; 

       hardwareVector.erase(hardwareVector.begin() + (eraseLine - 1)); 
      } 

      outputFile.open("hardware.txt"); 

      for (string hardwareList : hardwareVector) 
      { 
       outputFile << hardwareList << endl; 
      } 
      outputFile.close(); 

      if (hardwareMenu == 4) 
      { 
       menuInput = 0; 
      } 
     } 

     while (menuInput == 3) 
     { 
      system("CLS"); 

      int choreMenu = 0; 

      cout << "Press 1 to read the chore list." << endl; 
      cout << "Press 2 to add an item to the list." << endl; 
      cout << "Press 3 to delete an item from the list." << endl; 
      cout << "Press 4 to return to the main menu." << endl; 

      cin >> choreMenu; 

      if (choreMenu == 1) 
      { 
       system("CLS"); 

       for (string choreList : choreVector) 
       { 
        cout << choreList << endl; 
       } 

       system("PAUSE"); 
      } 

      if (choreMenu == 2) 
      { 
       string choreItem; 

       system("CLS"); 

       cout << "Enter what you would like to add to the chore list: " << endl; 
       getline(cin, choreItem); 

       choreVector.push_back(choreItem); 
      } 

      if (choreMenu == 3) 
      { 
       int eraseLine = 0; 

       system("CLS"); 
       cout << "What line would you like to erase from the list?" << endl; 
       cin >> eraseLine; 

       choreVector.erase(choreVector.begin() + (eraseLine - 1)); 
      } 

      outputFile.open("chore.txt"); 

      for (string choreList : choreVector) 
      { 
       outputFile << choreList << endl; 
      } 
      outputFile.close(); 

      if (choreMenu == 4) 
      { 
       menuInput = 0; 
      } 
     } 
    } 

    return 0; 
} 

답변

2

이 단순히 menuInput 번호를 읽어하지만, 버퍼에 여전히 새로운 라인 \n을 유지합니다. getline으로 전화하면 아직 대기중인 \n이 있었기 때문에 빈 줄이 표시됩니다. 당신은 당신이 getline를 사용하기 전에 입력 버퍼를 지워야합니다 getline 혼합하는 경우 :

cout << "Enter what you would like to add to the grocery list: " << endl; 
cin.ignore(); // <<== you need this! 
getline(cin, groceryItem); 

당신은 std::cin에서 읽을 당신의 getline 전화의 각 해당를 추가해야합니다. 이렇게하면 문제가 해결됩니다. 그 외에는

, 몇 가지 제안 :

  • system("cls") 또는 system("pause")이 그것을 제거하려고 사용하지 않지만, 다음 콘솔이 명확하지 않을 것입니다.
  • 코드를 별도의 함수로 리팩터링하고, 모든 것을 main()에 집어 넣지 마십시오. 예를 들어 을 기준으로 을 입력 한 후 manageGroceries(...), manageHardware(...), manageChores(...) 중 하나를 호출 할 수 있습니다.
0
  1. 문제 설명은 그래서
  2. 가장 큰 문제 추측됩니다 것은 당신이 main 기능에 모든 것을 넣어 것입니다 좋지 않습니다. 더 작은 함수로 분할하면 많은 코드를 재사용 할 수 있으며 최종 코드는 3 배 더 작아집니다. "식료품 점", "하드웨어"및 "작업"은 동일한 작업을 수행합니다. 유일한 차이점은 화장품이며 매개 변수화 할 수 있습니다.
  3. 사용하지 마십시오 system("cls") 과부는 구체적이며 기대하는 바를하지 않습니다 (단지 그러한 느낌을줍니다). 이것은 표준 입력/출력과 콘솔의 차이점을 이해하지 못한 결과입니다.

표준 입력의 변경된 상태가 데이터를 읽을 수없는 상태를 입력 값에 잘못 입력했다고 생각합니다. 입력 및 cin.synch()에 오류 상태를 지 웁니다

사용 cin.clear() 그래서 다음, 콘솔 버퍼에 대기중인 모든 데이터 갓 콘솔에 제공 한 데이터를 읽을 cin에 작업을 읽고 정리 (무시)한다.

관련 문제