Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用单个变量对字符输入计数_C++_Loops - Fatal编程技术网

C++ 使用单个变量对字符输入计数

C++ 使用单个变量对字符输入计数,c++,loops,C++,Loops,我试图计算输入的字符数,例如:1 1 1 1 2 2 2 5 5 5 5 10 10 10,但我无法摆脱循环并开始新的倒计时 当我试图在屏幕上只记录美分的总数时,cout总是在每个周期中重复,我如何才能在周期结束时只获得全部金额 如何更改代码以简化数字计算 有什么建议吗 int main(){ int cent = 0; int cent_input = 0; int cent_quantity = 0; int downgrade_to_one = 0;

我试图计算输入的字符数,例如:1 1 1 1 2 2 2 5 5 5 5 10 10 10,但我无法摆脱循环并开始新的倒计时

  • 当我试图在屏幕上只记录美分的总数时,cout总是在每个周期中重复,我如何才能在周期结束时只获得全部金额

  • 如何更改代码以简化数字计算

  • 有什么建议吗

    int main(){
        int cent = 0;
        int cent_input = 0;
        int cent_quantity = 0;
        int downgrade_to_one = 0;
    
        do{
            while(true){
                cout << "\nEnter number: " && cin >> cent;
                switch(cent){
                    case 1:
                         downgrade_to_one =  cent + downgrade_to_one;
                         if (cent == 0)
                         cent + 1;
                         cout << "\nYou have: " << downgrade_to_one << " one cent monets.";
                         break;
    
                    case 2:
                         cent_input = (cent_input + cent) / cent;
                         cent_quantity = cent_quantity + cent_input;
                         cout << "\nYou have: " << cent_quantity << " two cent monets.";
    
                    case 5:
                         cent_input = (cent_input + cent) / cent;
                         cent_quantity = cent_quantity + cent_input;
                         cout << "\nYou have: " << cent_quantity << " five cent monets.";
                         break;
    
                    case 10:
                         cent_input = (cent_input + cent) / cent;
                         cent_quantity = cent_quantity + cent_input;
                         cout << "\nYou have: " << cent_quantity << " ten cent monets.";
                         break;
    
                    case 25:
                         cent_input = (cent_input + cent) / cent;
                         cent_quantity = cent_quantity + cent_input;
                         cout << "\nYou have: " << cent_quantity << " twentyfive cent monets.";
                         break;
    
                    case 50:
                         cent_input = (cent_input + cent) / cent;
                         cent_quantity = cent_quantity + cent_input;
                         cout << "\nYou have: " << cent_quantity << " fifty cent monets.";
                         break;
    
                    default:
                        cout << "\nUndefined input!";
                }
            }
        }
        while(false);
    }
    
    intmain(){
    整数=0;
    int cent_输入=0;
    int cent_数量=0;
    int降级_至_one=0;
    做{
    while(true){
    分;
    开关(分){
    案例1:
    降级至1=分+降级至1;
    如果(分==0)
    分+1;
    
    谢谢大家,他们否决了我的问题。我自己解决了这个问题

    int main(){
    int cent = 0;
    int one_cent_amount = 0;
    int two_cent_amount = 0;
    int five_cent_amount = 0;
    int ten_cent_amount = 0;
    int twentyfive_cent_amount = 0;
    int fifty_cent_amount = 0;
    int cent_input = 0;
    int cent_quantity = 1;
    int dollars = 0;
    int cents = 0;
    int entire_quantity = 0;
    
    cout << "\nEnter number: ";
    while(cin >> cent){
    switch(cent){
    case 1:
    if(cent == 0)
    cent + 1;
    one_cent_amount =  cent + one_cent_amount;
    break;
    case 2:
    cent_input = (cent_input + cent) / cent;
    two_cent_amount = two_cent_amount+ cent_input;
    break;
    case 5:
    cent_input = (cent_input + cent) / cent;
    five_cent_amount = five_cent_amount + cent_input;
    break;
    case 10:
    cent_input = (cent_input + cent) / cent;
    ten_cent_amount = ten_cent_amount + cent_input;
    break;
    case 25:
    cent_input = (cent_input + cent) / cent;
    twentyfive_cent_amount = twentyfive_cent_amount + cent_input;
    break;
    case 50:
    cent_input = (cent_input + cent) / cent;
    fifty_cent_amount = fifty_cent_amount + cent_input;
    break;
    default:
    cout << "\nUndefined input!";
     }
                                scanf("%c", &cent); //%c to read a character
                    if(cent == '\n') {
                                break;
            }
    }
    entire_quantity = one_cent_amount + (two_cent_amount * 2) + (five_cent_amount * 5) + (ten_cent_amount * 10)  + (twentyfive_cent_amount * 25) + (fifty_cent_amount * 50);
    dollars = entire_quantity / 100;
    cents = entire_quantity % 100;
    if (entire_quantity != 1 && entire_quantity != 0)
    cout << "\nYou have: " << one_cent_amount << " one cent monets."
                  << '\n' << two_cent_amount << " two cent monets."
                  << '\n' << five_cent_amount<< " five cent monets."
                  << '\n' << ten_cent_amount << " ten cent monets."
                  << '\n' << twentyfive_cent_amount << " twentyfive cent monets."
                  << '\n' << fifty_cent_amount << " fifty cent monets."
                  << '\n' << "Entire quantity: " << entire_quantity << " cents."
                  << '\n' << "Your money are: " << dollars << " dollars and " << cents << " cents.";
    else if (entire_quantity == 1)
    cout << "\nYou have only " << entire_quantity << " cent.";
    else if (entire_quantity == 0)
    cout << "\nNothing is specified, you need to enter the amount!";
    }
    
    intmain(){
    整数=0;
    整数一分金额=0;
    整数二分金额=0;
    整数五分金额=0;
    整数十美分=0;
    整数二十五美分金额=0;
    整数五十美分=0;
    int cent_输入=0;
    整数=1;
    整数美元=0;
    整数分=0;
    整数整_数量=0;
    cout>cent){
    开关(分){
    案例1:
    如果(分==0)
    分+1;
    一分金额=一分+一分金额;
    打破
    案例2:
    分输入=(分输入+分)/分;
    二分金额=二分金额+分输入;
    打破
    案例5:
    分输入=(分输入+分)/分;
    五分金额=五分金额+分输入;
    打破
    案例10:
    分输入=(分输入+分)/分;
    十美分金额=十美分金额+美分输入;
    打破
    案例25:
    分输入=(分输入+分)/分;
    二十五美分金额=二十五美分金额+美分输入;
    打破
    案例50:
    分输入=(分输入+分)/分;
    50美分金额=50美分金额+美分输入;
    打破
    违约:
    
    你似乎对你使用的是哪种语言感到困惑吗?象双循环结构
    do{while(true){…}while(false)这样的符号;
    也是深奥而新颖的。在案例2之后:break命令丢失了
    &&
    cout>中;
    执行一个无意义的布尔and。我建议您去掉它,并将
    cout
    cin
    语句放在单独的行上。@alterigel:
    &
    检查输出是否成功-它不会成功如果输出流设置为失败状态,请尝试输入。