2011-06-14 2 views
-2

나는 이에 대한 답변을 꽤 오랫동안 찾고 있었고, 나는 물어보기로 한 것을 찾지 못했습니다. 웬일인지 나는 불법적 인 성명서를 내고있다. 이것은 나의 마지막 프로젝트를위한 것이며 교사는 오류를 찾을 수 없습니다. 나는 그런 긴 코드 그러나 나는 사과오류 C2046 : 불법 사례

  do //start of do while loop 
{ 
cout << "Welcome to the Sperry Store!" << '\n'; //greeting 

cout << " " << '\n'; 
cout << "1- Men's Authentic Original Seasonal Boat Shoe.. $79.99" << '\n'; 
cout << "2- Men's Authentic Original Suede Boat Shoe..... $64.99" << '\n'; 
cout << "3- Men's Mako 2-Eye Canoe Moc Boat Shoe......... $70.00" << '\n'; 
cout << "4- Men's Charter 2-Eye.......................... $90.00" << '\n'; 
cout << "5- Defender 2-Eye Boat Shoe..................... $65.00" << '\n'; 
cout << "6- Men's Embroidered Mako Short................. $34.99" << '\n'; //user friendly menu 
cout << "7- Men's Figawi Jacket.......................... $99.99" << '\n'; 
cout << "8- Men's Mako Pant.............................. $45.99" << '\n'; 
cout << "9- Check Out " << '\n'; 
cout << "0- Start Over and Erase all current sales information. " << '\n'; 


cout << " " << '\n'; 
cout << "Please select the item you're interested in." << '\n'; 
cout << setprecision (5) << "Your current total is: $" << total << '\n'; //displays running total 
cin >> x; //allows entered value to take user to disired case statement 

switch (x) //creates switch for the case statements 
{ 
case '1': 
    cout << "How many pairs would you like?" << '\n'; 
    cin >> amount; 

    sub_total1 = (amount * seasonal); 
    total = sub_total1 + total;         //case statements for getting the mula 

    cout << "Your current total is: $" << total << '\n'; 


    break; //break statemets required to end case 

case '2': 

    cout << "How many pairs would you like?" << '\n'; 
    cin >> amount; 

    sub_total2 = (amount * original); 
    total = sub_total2 + total; 

    break; 

    case '3': 

    cout << "How many pairs would you like?" << '\n'; 
    cin >> amount; 

    sub_total3 = (amount * canoe); 
    total = sub_total3 + total; 

    break; 

    case '4': 

    cout << "How many pairs would you like?" << '\n'; 
    cin >> amount; 

    sub_total4 = (amount * charter); 
    total = sub_total4 + total; 

    break; 

    case '5': 

    cout << "How many pairs would you like?" << '\n'; 
    cin >> amount; 

    sub_total5 = (amount * defender); 
    total = sub_total5 + total; 

    break; 

    case '6': 

    cout << "How many pairs would you like?" << '\n'; 
    cin >> amount; 

    sub_total6 = (amount * embroidered); 
    total = sub_total6 + total; 

    break; 

    case '7': 

    cout << "How jackets would you like?" << '\n'; 
    cin >> amount; 

    sub_total7 = (amount * figawi); 
    total = sub_total7 + total; 

    break; 

    case '8': 

    cout << "How many pairs would you like?" << '\n'; 
    cin >> amount; 

    sub_total8 = (amount * mako_pant); 
    total = sub_total8 + total; 


    break; 

case '9': //check out case statement, lets the function end 

    cout << "Welcome to the check out, please finalize your order." << '\n'; 
    tax = total * .0875; //calculate tax 
    cout << setprecision (5) << "Your total plus tax is: $" << (total = total + tax) << '\n'; //display total + tax 
    cout << "Would you like your order to be shipped? (Type 1 for yes and 2 for no)" << '\n'; //allows choice for shipping or not 
    cin >> shipping; 

    if (shipping == '1') //adds a shipping charge                  
    { 
     cout << "There is a flat shipping rate of $9.99" << '\n'; 
     cout << setprecision (5) << "Your new total is: $" << (total + 9.99) << '\n'; 
     cout << "Would you like a Receipt? (Press 1 for yes and 2 for no)" << '\n'; 
     cin >> receipt; 


     if (receipt == '1') 
     { 
      cout << "     " << "Your Receipt" << '\n'; 
cout << "Men's Authentic Original Seasonal Boat Shoe.."<< sub_total1 <<'\n'; 
cout << "Men's Authentic Original Suede Boat Shoe....."<< sub_total2 <<'\n'; 
cout << "Men's Mako 2-Eye Canoe Moc Boat Shoe........."<< sub_total3 <<'\n'; 
cout << "Men's Charter 2-Eye.........................."<< sub_total4 <<'\n'; //receipit 
cout << "Defender 2-Eye Boat Shoe....................."<< sub_total5 <<'\n'; 
cout << "Men's Embroidered Mako Short................."<< sub_total6 <<'\n'; 
cout << "Men's Figawi Jacket.........................."<< sub_total7 <<'\n'; 
cout << "Men's Mako Pant.............................."<< sub_total8 <<'\n'; 
    cout << "If you have any questions please call (781)-452-2079."<< '\n'; 
    cout << "Thank you for shopping at The Sperry Store!" << '\n'; 
     } 
     if (receipt == '2') 
     { 
      cout << "If you have any questions please call (781)-452-2079."<< '\n'; 
      cout << "Thank you for shopping at The Sperry Store!" << '\n'; 
     } 


    } 
    if (shipping == '2') //checks out without shipping 
    { 
        cout << "     " << "Your Receipt" << '\n'; 
cout << "Men's Authentic Original Seasonal Boat Shoe.."<< sub_total1 <<'\n'; 
cout << "Men's Authentic Original Suede Boat Shoe....."<< sub_total2 <<'\n'; 
cout << "Men's Mako 2-Eye Canoe Moc Boat Shoe........."<< sub_total3 <<'\n'; 
cout << "Men's Charter 2-Eye.........................."<< sub_total4 <<'\n'; //receipit 
cout << "Defender 2-Eye Boat Shoe....................."<< sub_total5 <<'\n'; 
cout << "Men's Embroidered Mako Short................."<< sub_total6 <<'\n'; 
cout << "Men's Figawi Jacket.........................."<< sub_total7 <<'\n'; 
cout << "Men's Mako Pant.............................."<< sub_total8 <<'\n'; 
    cout << "If you have any questions please call (781)-452-2079."<< '\n'; 
    cout << "Thank you for shopping at The Sperry Store!" << '\n'; 
     } 
     if (receipt == '2') 
     { 
      cout << "If you have any questions please call (781)-452-2079."<< '\n'; 
      cout << "Thank you for shopping at The Sperry Store!" << '\n'; 
     } 

     no_ship(); //declares function 

    } 

    break; 

    case '0': //restart order statement 

    cout << "Are you sure you would like to erase all data (This choice is non-reversable)?" << '\n'; 
    cout << "Press 1 for yes and 2 for no" << '\n'; 
    cin >> erase; 

    if (erase == '1') //erases the data 
    { 
     shipping = 0; 
     erase = 0; 
     amount = 0; 
     total = 0;   
     tax = 0; 
     x = 0; 
     sub_total1 = 0; 
     sub_total2 = 0; 
     sub_total3 = 0; 
     sub_total4 = 0; 
     sub_total5 = 0; 
     sub_total6 = 0; 
     sub_total7 = 0; 
     sub_total8 = 0; 
    } 
    if (erase == '2') //returns to menu 
    { 
     cout << setprecision (4) << "Your current total is: $ " << total << '\n'; //returns user to the menu without erasing order 
    } 

    break; 

    system("command/ccls"); 


} 



while (x != '9'); //end of while loop (when x does not = 9) 


return 0; 
} 

void no_ship() // displays the no_ship function data 
{ 
    cout << "Your item will be ready in 1-2 business days." << '\n'; 
    cout << "If you have any questions please call (781)-452-2079."<< '\n'; 
    cout << "Thank you for shopping at The Sperry Store!" << '\n'; 
} 

... 그것은 전에 잘 작동했지만, 그 다음 무언가에 넣어 그것을 꺼내서 후, 경우 문은 코드가 동일합니다 ... 비록 작동이 중지 오류가 어디 있는지 전혀 모른다.

Compiling... 
final project.cpp 
H:\Comp. Prog\final project.cpp(232) : error C2046: illegal case 
Error executing cl.exe. 
final project.exe - 1 error(s), 0 warning(s) 
+2

Pro 팁 - 스위치의 케이스 안에 복잡한 코드를 넣지 마십시오. 케이스는 최대 두 줄의 코드로 구성되어야합니다. 기능을 사용하거나 사다리꼴을 사용하십시오. –

+0

라인 232는 어느 라인입니까? 오류 메시지의 (232)는 오류의 위치를 ​​알려줍니다. –

+0

찾을 수없는 컴파일 문제가 발생하면 문제를 찾을 때까지 프로그램의 복사본 크기를 줄이십시오. 결국에는 문제를 찾거나 선생님에게 보여 줄 수있는 5 가지 프로그램이 있습니다. 이 기술에 대한 자세한 내용은 http://sscce.org를 참조하십시오. –

답변

0

당신은 정말 당신의 들여 쓰기 작동합니다. 당신의 문제는 case '0':switch 성명 (당신에게 뛰어 들었을 적절한 들여 쓰기와 함께) 밖에 있다는 것입니다.

+0

정말 고마워요. 나는 그것에 노력하려고 노력할 것이다. 내가 볼 수있는 자습서가 있습니까? – Matt

+1

@Matt : 여기에 한 가지 힌트가 있습니다. 중괄호보다 적은 중괄호 안에 코드를 들여 쓰지 마십시오. 선이 너무 길면 선을 끊으십시오. Neil이 그의 코멘트에서 제안한 것처럼, 코드를 함수로 분해하는 것이 더 나았습니다. –

3

당신은 너무 많은 닫는 괄호가, 특히 하나가 바로 break; case '0': 앞에 다음은 컴파일러에서 오류입니다.

이것은 귀하의 switch 성명서를 닫고 있습니다.이 진술서는 알몸의 case에 대한 불만 사항입니다.

그렇다면 외계 버팀대를 삭제하면 스위치 문을 닫지 않기 때문에 또 다른 문제가 발생할 것입니다. case '0' 블록 바로 다음에 switch을 닫지 않고 do 루프를 닫습니다. 거기에 또 다른 }을 넣어야합니다.


좀 더 쉽게 읽을 수 있도록 코드를 리팩터링 하시겠습니까? 제 생각에는 case 블록에 줄 수가 적어야합니다 (예 : 10). 더 이상 또는 들여 쓰기 수준이 많은 경우에는 그냥 쓸데없는 작업을 수행하는 함수를 호출해야합니다.

아마도 오류를 더 분명하게 만들었을 것입니다. 완전히 피하지 않으면 최소한 case '9' 기능으로 이동했을 것입니다.

+0

그것이 제거되면 치명적인 오류가 발생합니다. C1004 : 예상치 못한 파일 끝이 발견되었습니다. 그리고 다른 두 명. – Matt

+0

그러면 while (x! = '9');의 바로 앞에 다른 버팀대가 필요합니다. - 내 업데이트를 참조하십시오. – paxdiablo

+0

네, 구조가 일주일이고, 종종 문제가 생겨서 함수에서 빠져 나옵니다. – Matt

1

당신은 case '9'에 여분의 닫는 중괄호를 가지고 : 당신과 다른 사람에 대한 가독성을 향상으로

if (shipping == '2') //checks out without shipping 
{ 
    // skipped 
    } // <------- 
    if (receipt == '2') 
    { 
     cout << "If you have any questions please call (781)-452-2079."<< '\n'; 
     cout << "Thank you for shopping at The Sperry Store!" << '\n'; 
    } 

    no_ship(); //declares function 

}