2017-10-02 20 views
-3

내가하는 작업은 문자열 벡터를 만들고 문자열을 추가 한 다음 문자열을 삭제하는 것입니다. 스위치 케이스는 경우에 큐를 추가하는 옵션 1.C++의 스위치 케이스 메뉴에서 문자열을 벡터에 추가하는 방법

//gaming Queue 

#include <iostream> 
#include <vector> 
#include <algorithm> 
#include <ctime> 
#include <cstdlib> 

using namespace std; 

int main() 
{ 
    int choice; 
    string input; 
    bool menu = true; 
    vector<string> favGames; 
    while (menu){ 
     cout <<"Welcome to the favorite game queue please add your favorite games:\n"; 
     cout <<"1-Add a favorite game.\n"; 
     cout <<"2-List of your favorite games.\n"; 
     cout <<"3-Remove a game.\n"; 
     cin >> choice; 
     switch (choice) 
      { 
      case 1:  
       cout << "Please add a favorite game to the queue: \n"; 
       string input; 
       cin >> input; 
       favGames.push_back(input);// Here is my problem it just jumps to case 2 and shows an error 
       break; 
      case 2: 
       cout << "Here is a list of your favorite games.\n"; 
       break; 

      default: 
       cout << "You made an illegal choice.\n"; 
     } 
    } 
    return 0; 
} 
+0

'문자열 입력'을 두 번째로 선언합니다. – Seltymar

+0

이 코드가 사례 2로 점프 할 수있는 유일한 방법은 사용자가 실제로 2를 입력으로 입력하는 경우입니다. 그리고 오류가 있다고하지만 오류가 실제로 무엇인지는 말하지 않습니다. –

+0

@Seltymar는 단 한 번만 필요하다면 케이스 또는 외부에 선언해야합니까? – Migdotcom

답변

1

https://www.onlinegdb.com/online_c++_compiler에 코드를 컴파일, 그것은 나에게 그 오류를 보여주고 있습니다 있도록 내가 APPEND의 strings.I에 문제가 있어요 또한이 설정 한 :

main.cpp:34:18: error: jump to case label [-fpermissive] 
     case 2: 
      ^
main.cpp:30:24: note: crosses initialization of 'std::string input' 
      string input; 

컴파일러에서 알 수 있듯이 string input의 초기화를 건너 뛰고 있습니다. 동시에 input을 두 번째로 선언하고 있습니다. 스위치 케이스 안에 input을 제거하면 프로그램이 컴파일되어 의도 한대로 작동합니다.

EDIT :

cin 추출 항상 추출되는 값을 종료로서 공백 (공백, 탭, 개행을 ...)도 고려되기 때문 CIN을 사용하여 하나 개 이상의 단어를 입력 할 수있다.

따라서 getline을 사용해야합니다. choice이 될 때와 동일합니다.

은 여기에 전체 코드 :

#include "stdafx.h" 
#include <vector> 
#include <string> 
#include <iostream> 
#include <sstream> 

using namespace std; 

int main() 
{ 
    int choice; 
    string input; 
    bool menu = true; 
    vector<string> favGames; 
    while (menu) { 
     cout << "Welcome to the favorite game queue please add your favorite games:\n"; 
     cout << "1-Add a favorite game.\n"; 
     cout << "2-List of your favorite games.\n"; 
     cout << "3-Remove a game.\n"; 

     string choiceStr; 
     getline(cin, choiceStr); 
     stringstream(choiceStr) >> choice; 

     switch (choice) 
     { 
     case 1: 
      cout << "Please add a favorite game to the queue: \n"; 
      getline(cin, input); 
      favGames.push_back(input); 
      break; 
     case 2: 
      cout << "Here is a list of your favorite games.\n"; 
      break; 
     case 3: 
      cout << "No longer like a game which game should we remove?\n"; 
      break; 

     default: 
      cout << "You made an illegal choice.\n"; 
     } 
    } 
    return 0; 
} 

가 무한 루프에 진입하는 이유 : 당신은 단지 하나 개의 단어를 입력 할 때 cin >> input 만 사용할 수 있습니다

합니다. 여러 단어를 입력하면 cin >> input은 첫 번째 단어를 포착하고 cin >> choice은 다음 단어를 포착합니다. cin >> choice에 의한 입력 catch가 int가 아니면 cin이 실패합니다.이 경우 무한 루프가됩니다. 여기에 설명되어 있습니다 http://www.cplusplus.com/doc/tutorial/basic_io/.

+0

감사합니다!제게 문제가되는 것이 무엇인지 설명해주십시오. 라인을 얻는 것이 트릭 이었지만 처음에는 왜 작동하지 않는지 혼란 스러웠습니다. – Migdotcom

2

switch 문은 {} 블록을 사용하여 범위를 만들지 않는 한 case 절 내에서 변수를 선언 할 수 없기 때문에 약간 이상합니다.

switch (choice) 
    { 
     case 1:  
     { // start a scope 
      cout << "Please add a favorite game to the queue: \n"; 
      string input; 
      cin >> input; 
      favGames.push_back(input);// Here is my problem it just jumps to case 2 and shows an error 
      break; 
     } // end the scope 

그러나 귀하의 경우에 당신은 이미 switch 외부 std::string 정의 당신이 사용하려는나요? 그런 다음 case 절 안의 것을 제거하면됩니다.