1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

error trap and looping

Discussion in 'C++' started by timbomo, Mar 1, 2010.

  1. timbomo

    timbomo New Member

    Joined:
    Mar 1, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    i am trying to error trap this so the user can only choose odd numbers 3-9 as the width and height of shape. i am also trying to loop this so it will continue until the user chooses to quit. using y for yes to continue or n for no to quit.
    Code:
      
    #include<iostream>
    #include<iomanip>
    using namespace std;
    int main()
    {
    int number =0, answer,h=0,width=0, setw=0;
    char symbol = '\0';
    cout << "Please choose a letter that corresponds to the shape of your choosing \n";
    cout << " A. triangle " << endl;
    cout << " B. upside down triangle " << endl;
    cout << " C. rectangle " << endl;
    cout << " D. plus sign " << endl;
    cout << " E. quit " << endl;
    cin >> symbol;
    cout << " Please choose an odd number 3-9 to calculate width and height of your choosing\n";
    cin >> number;
     
    do 
    {
    do 
    {
    switch (symbol)
    {
    case 'a':
    case 'A':
    cout << " Thank you, you have choosen triangle and " << number << " for height and width" << endl;
    cout << " Please enter a symbol to design your shape. " << endl;
    cin >> symbol;
    cout << '\n';
    for(int i=0; i<number; i++)
    {
    for(int y=-1; y<i; y++)
    cout << symbol;
    cout << "\n";
    }
    cout << '\n' << " Do you wish to continue: (y/n) " << endl;
    cin >> answer;
    break;
    
    break;
    default:
    cout << " please enter a valid response. press y for yes and n for no " << endl;
    }
    }while (( number==3) || ( number==5) || (number==7) || ( number==9));
    
    }while (( answer!= 'n') ||(answer != 'N'));
    {
    cout << " thank you for your time " << endl;
    }
    return 0;
    }
    
     
  2. kiruthika

    kiruthika New Member

    Joined:
    Feb 24, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I have modified your code into the following.

    Code:
    
    #include<iostream>
    #include<iomanip>
    using namespace std;
    int main()
    {
            int number =0, h=0,width=0, setw=0;
            char symbol = '\0';
            char answer='\0';
    loop1 : while(1)
            {
                    cout << "Please choose a letter that corresponds to the shape of your choosing \n";
                    cout << " A. triangle " << endl;
                    cout << " B. upside down triangle " << endl;
                    cout << " C. rectangle " << endl;
                    cout << " D. plus sign " << endl;
                    cout << " E. quit " << endl;
                    cin >> symbol;
                    cout << " Please choose an odd number 3-9 to calculate width and height of your choosing\n";
                    cin >> number;
    
                    if (( number==3) || ( number==5) || (number==7) || ( number==9))
                    {
                            switch (symbol)
                            {
                                    case 'a':
                                    case 'A':
                                            cout << " Thank you, you have choosen triangle and " << number << " for height and width" << endl;
                                            cout << " Please enter a symbol to design your shape. " << endl;
                                            cin >> symbol;
                                            cout << '\n';
                                            for(int i=0; i<number; i++)
                                            {
                                                    for(int y=-1; y<i; y++)
                                                            cout << symbol;
                                                    cout << "\n";
                                            }
                                            break;
    
                                            break;
                                    default:
                                            cout << " please choose a symbol between A and E" << endl;
                                            while(1)
                                            {
                                                    cout << '\n' << " Do you wish to continue: (y/n) " << endl;
                                                    cin >> answer;
                                                    if(( answer=='y') ||(answer == 'Y'))
                                                    {
                                                            goto loop1;
                                                    }
                                                    else if((answer=='n')||(answer=='N'))
                                                    {
                                                            cout << " thank you for your time " << endl;
                                                            return 0;
                                                    }
                                                    else
                                                    {
                                                            cout<<"Invalid.Press y for yes or n for no(y/n)"<<endl;
                                                            continue;
                                                    }
                                            }
                            }
                    }
                    else
                    {
    
                            cout<<"\tPlease choose the number between 3-9"<<endl;
                    }
    
            }
    
            return 0;
    }
    
    
    
    
    
     
  3. vivekraj

    vivekraj New Member

    Joined:
    Feb 23, 2010
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    timbo,you try the following code.

    Code:
    #include<iostream>
    #include<iomanip>
    using namespace std;
    int main()
    {
            int number =0,h=0,width=0, setw=0;
            char symbol = '\0', answer;
            while(1)
            {
                    cout << "Please choose a letter that corresponds to the shape of your choosing \n";
                    cout << "A. triangle " << endl;
                    cout << "B. upside down triangle " << endl;
                    cout << "C. rectangle " << endl;
                    cout << "D. plus sign " << endl;
                    cout << "E. quit " << endl;
                    cin >> symbol;
                    switch (symbol)
                    {
                            case 'a':
                            case 'A':
                                    cout << "Please choose an odd number 3-9 to calculate width and height of your choosing\n";
                                    cin >> number;
                                    while(number!=3 && number!=5 && number!=7 && number!=9)
                                    {
                                            cout << "You have chosen Invalid Number\n";
                                            cout << "Please choose an odd number 3-9 to calculate width and height of your choosing\n";
                                            cin >> number;
                                    }
                                    cout << "Thank you, you have chosen triangle and " << number << " for height and width" << endl;
                                    cout << "Please enter a symbol to design your shape. " << endl;
                                    cin >> symbol;
                                    cout << '\n';
                                    for(int i=0; i<number; i++)
                                    {
                                            for(int y=-1; y<i; y++)
                                                    cout << symbol;
                                            cout << "\n";
                                    }
                                    while(1)
                                    {
                                            cout << '\n' << "Do you wish to continue: (y/n) " << endl;
                                            cin >> answer;
                                            if(answer=='y' || answer=='Y')
                                                    break;
                                            else if(answer=='n'||answer=='N')
                                            {
                                                    cout << "Thank You For Your Time " << endl;
                                                    return 0;
                                            }
                                            else
                                                    cout << "Please choose a valid option.Either y or n.";
                                    }
                                    break;
                            case 'e':
                            case 'E':
                                    exit(0);
                            default:
                                    cout << "Please Choose a valid symbol between 'A' and 'E' " << endl;
                    }
            }
            return 0;
    }
    
     

Share This Page