从一个循环重复中断

时间:2015-10-25 02:33:46

标签: c++ while-loop

我想要实现的是用户输入一个值然后进行测量。然后将其放置为最小,最大或介于两者之间。 保留已输入的值的计数。以米为单位的所有值的总和。

最初程序有效,但是当我输入中断字符时,程序会重复相同的行。我想要的是结束打印行129和130的程序。这是我的代码。

int main()
{
    double var1; //the variable entered by the user
    double sum;  //the sum of calculations to convert into centimeters
    double total=0/100; //converts to meters
    int e=0; //will be used to count how many numbers were entered
    string measurment; //the users desired measurement input
    string centimeter = "cm";
    string meter = "m";         //these are used to compare the users measurement input
    string inch = "in";
    string foot = "ft";
    char d='t';//this will be used to break the loop (t is just the default)
    char a='a';
    char c='c';
    char m='m';//char a-f are used for a switch
    char i='i';
    char f='f';
    double small=20000;
    double large=0;
    double cm=1;
    double me=100;//centimeters
    double in=2.54; //centimeters
    double ft=12; //inches

    //the following code creates a break when the character is entered.
    while(d!='q')//break rule
    {
        if(false)
            break;

        cout<<"please enter a double and a unit of measurement."<<'\n';
        cin >>var1>>measurment;
        e++;

        // the following portion of code sets the char for the switch which
        // which will be used in the following code. it will also perform the
        // the math which will calculate between distances.

        if (measurment==centimeter)
        {
            a=c;
            sum=var1*cm;
        }
        else if (measurment==meter)
        {
            a=m;
            sum=var1*me;
        }
        else if (measurment==inch)
        {
            a=i;
            sum=var1*in;
        }
        else if (measurment==foot)
        {
            a=f;
            sum=(var1*ft)*in;
        }
        else
        {
        cout<<"I am sorry. But, that is not a valid measurement for this program."<<'\n';
        }

        //the following code places the number entered into either
        //smallest largest or in between.

        if (sum<small)
        {
            small=sum;
            total+=sum;
            switch(a){
        case'c':
            cout<<small/cm<<centimeter<<" is the smallest measurement so far."<<'\n';
            break;
        case'm':
            cout<<small/me<<meter<<" is the smallest measurement so far."<<'\n';
            break;
        case'i':
            cout<<small/in<<inch<<" is the smallest measurement so far."<<'\n';
            break;
        case'f':
            cout<<(small/in)/ft<<foot<<" is the smallest measurement so far."<<'\n';
            break;
            }
        }
        else if (sum>large)
        {
            large=sum;
            total+=sum;
            switch(a){
        case'c':
            cout<<large/cm<<centimeter<<" is the largest measurement so far."<<'\n';
            break;
        case'm':
            cout<<large/me<<meter<<" is the largest measurement so far."<<'\n';
            break;
        case'i':
            cout<<large/in<<inch<<" is the largest measurement so far."<<'\n';
            break;
        case'f':
            cout<<(large/in)/ft<<foot<<" is the largest measurement so far."<<'\n';
            break;
            }
        }
        else if(sum>small&&sum<large)
        {
            total+=sum;
        switch(a){
        case'c':
            cout<<var1<<centimeter<<" is neither the longest or shortest measurement."<<'\n';
            break;
        case'm':
            cout<<var1<<meter<<" is neither the longest or shortest measurement."<<'\n';
            break;
        case'i':
            cout<<var1<<inch<<" is neither the longest or shortest measurement."<<'\n';
            break;
        case'f':
            cout<<var1<<foot<<" is neither the longest or shortest measurement."<<'\n';
            break;
            }
        }
    }
    //after the break, this should be printed to screen
    cout<<"Of a total of "<<e<<" entries. "<<small<<meter<<" is the smallest length."<<'\n';
    cout<<"And "<<large<<meter<<" is the largest length. "<<total<<meter<<" is the total length."<<'\n';

    return 0;
}

如果代码太长,请道歉。我不太确定你需要什么。我检查了很多帖子和网站,尝试了不同的破解码,但无济于事。我被卡住了。

3 个答案:

答案 0 :(得分:2)

也许我错过了一些明显的东西,但是打破while循环的正确方法是使条件失败。中断是FOR循环,因为它们可以是非条件循环。而不是打破while循环,你只需要创建变量

d ='q'

你想要突破的地方。请注意,中断对于中断switch语句仍然有效,因此如果需要,请将那些中断保留在那里。

对于任何拼写错误或者如果我错过了一个明显的问题,我是移动设备并且可能错过了某些内容

答案 1 :(得分:0)

永远不会执行第一个break语句,因为&#39; if&#39;条件总是假的。所有其他break语句都从switch语句中断,但不是while循环,因此循环将永远循环。 如果我理解正确,我认为你应该改变“假”。真的&#39;在第一个&#39;如果&#39;条件。话虽这么说,代码可能总体来说设计不好,但是你提到你是初学者,所以我建议你也学习如何优化你的代码并有效地编写代码。祝你好运。

答案 2 :(得分:0)

感谢您的帮助。通过我的计数器激活休息来解决问题。     而(真)     {         如果(E == d)         打破; 当e达到d值时。代码中断并输出我想要的信息。

这不是我希望它如何工作,但如上所述。我认为它的编程很糟糕。

谢谢你的帮助,谢谢。