2011-06-14 166 views
-2

我一直在寻找相当一段时间来回答这个问题,而且我还没有找到一个所以我决定问。出于某种原因,我收到了非法的案件陈述错误。这是为了我的最终项目,我的老师甚至找不到错误。它以前工作正常,但它把在之后的东西,然后拿出来,case语句停止工作......即使代码是相同的......错误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

专业提示 - 切勿将复杂的代码放入交换机的外壳内。一个案例最多应由几行代码组成。使用功能,或使用if-ladder。 – 2011-06-14 13:06:33

+0

哪一行是232行?错误消息中的(232)告诉你错误在哪里。 – 2011-06-14 13:07:11

+0

当您遇到无法找到的编译问题时,请尝试缩小程序的副本(直到找到问题为止)的大小。最终你会发现问题,或者你有一个5行的程序,你可以向你的老师展示。有关此技术的更多信息,请参阅http://sscce.org。 – 2011-06-14 14:22:42

回答

0

你真的应该对你的压痕做功,因为它提高可读性为您和他人。您的问题是case '0':不在switch声明之外(并且有适当的缩进会引起你的注意)。

+0

非常感谢。我会尽力去解决它。有没有我可以看的教程? – Matt 2011-06-14 13:18:27

+1

@Matt:这里给你一个提示:永远不要把大括号内的代码缩进大括号内。如果线条太长,请打破线条。更好的是,将代码分解为函数,正如Neil在他的评论中所建议的那样。 – 2011-06-14 13:34:07

3

你有太多的结束括号,特别是将此前break; case '0':

这是关闭你的switch声明,这就是为什么它抱怨裸体case

话虽如此,删除多余的括号会导致另一个问题,因为你根本没有关闭switch语句。紧接在case '0'块之后,您可以关闭do循环,而不必先关闭switch。您需要在其中放置另一个}


可能我建议你重构你的代码,使它更容易阅读?在我看来,case块应该有少量的行(例如10)。除此之外,或者如果有很多缩进级别,并且它应该只是调用一个函数来完成咕噜声。

这可能会让你的错误更加明显,因为如果它完全避免它,它至少会将它移到case '9'函数。

+0

当它被删除时,我得到致命的错误C1004:找到文件的意外结束。还有两个人。 – Matt 2011-06-14 13:16:48

+0

然后,在'while(x!='9')之前你需要另一个大括号;' - 查看我的更新。 – paxdiablo 2011-06-14 13:19:25

+0

是的,我的结构是星期,而我经常偏离功能,因为他们给我的问题... – Matt 2011-06-14 13:24:12

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 

}