Thread: Best way to conditionally branch this?

  1. #1
    Registered User roll cast's Avatar
    Join Date
    Nov 2011
    Location
    NI
    Posts
    9

    Best way to conditionally branch this?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {cout<< "Welcome to RollCast's Classic Salmon Fly Proportion Calculator!"<<endl;
    cout<< "Please enter the gape of your hook in mm."<<endl;
    int gape;
    cin>> gape;
    cout<< "Your hook gape ="; cout<< (gape); cout<< "mm"<<endl;
    cout<< "Please select which tyers proportions you wish to use"<<endl;
    cout<< "A: Kelson \nB: Cohen \nC: Alcott \nD: Carne \nE: Guidry \nF: Inman \nG: Boyer \nH: Ostoj \nI: Gotzmer"<<endl;
    cout<< "Please input the letter for which proportion you wish to use"<<endl;
    char tyer;
    cin>> tyer;
    }
    Sorry if this is really simple but I'm only learning C++ and can't figure out this problem.

    The idea for this project is to make a program that will take an integer input and then output a set of integers based on which tyer's proportions you choose to use.

    However I wanted to try and make the code as foolproof as possible so I don't have to rely on the user checking their inputs?

    Basically I want to take the users input, a char and then dependant on what character they input then use a certain scaling factor on their integer input.

    Thanks
    AL

  2. #2
    Registered User roll cast's Avatar
    Join Date
    Nov 2011
    Location
    NI
    Posts
    9
    I tried doing this with the switch function but I keep on getting errors?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {cout<< "Welcome to RollCast's Classic Salmon Fly Proportion Calculator!"<<endl;
    cout<< "Please enter the gape of your hook in mm."<<endl;
    int gape;
    cin>> gape;
    cout<< "Your hook gape ="; cout<< (gape); cout<< "mm"<<endl;
    cout<< "Please select which tyers proportions you wish to use"<<endl;
    cout<< "1: Kelson \n2: Cohen \n3: Alcott \n4: Carne \n5: Guidry \n6: Inman \n7: Boyer \n8: Ostoj \n9: Gotzmer"<<endl;
    cout<< "Please input the number for which proportion you wish to use"<<endl;
    int tyer;
    cin>> tyer;
    switch (tyer)
       {case 1:
          float dtl =1.8;
          float has =0.8;
          float dbb =0.7;
        break;
       case 2:
          float dtl =2;
          float has =0.875;
          float dbb =0.5;
        break;
        case 3:
          float dtl =1.4;
          float has =0.7;
          float dbb =0.3;
        break;
       case 4:
          float dtl =1.8;
          float has =1;
          float dbb =0.44;
        break;
        case 5:
          float dtl =1.88;
          float has =1.1;
          float dbb =0.75;
        break;
       case 6:
          float dtl =1.9;
          float has =0.81;
          float dbb =0.69;
        break;
        case 7:
          float dtl =1.78;
          float has =0.5;
          float dbb =0.83;
        break;
       case 8:
          float dtl =2;
          float has =0.78;
          float dbb =0.78;
        break;
        case 9:
          float dtl =1.69;
          float has =0.58;
          float dbb =0.53;
        break;
         default:
        cout << "Not a valid option" << endl;
        }
    
        cout<< (dtl)<<endl;
        cout<< (has)<<endl;
        cout<< (dbb)<<endl;
    }

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    For one, declare your floats dtl, has, and dbb above the switch block.

    As in :

    Code:
    float dtl;
    float has;
    float dbb;
    
    switch()
    {
        case 1:
            dtl = xx.x;
            has = xx.x;
            dbb = xx.x;
        break;
    }

  4. #4
    Registered User roll cast's Avatar
    Join Date
    Nov 2011
    Location
    NI
    Posts
    9
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {cout<< "Welcome to RollCast's Classic Salmon Fly Proportion Calculator!"<<endl;
    cout<< "Please enter the gape of your hook in mm."<<endl;
    int gape;
    cin>> gape;
    cout<< "Your hook gape ="; cout<< (gape); cout<< "mm"<<endl;
    cout<< "Please select which tyers proportions you wish to use"<<endl;
    cout<< "1: Kelson \n2: Cohen \n3: Alcott \n4: Carne \n5: Guidry \n6: Inman \n7: Boyer \n8: Ostoj \n9: Gotzmer"<<endl;
    cout<< "Please input the number for which proportion you wish to use"<<endl;
    int tyer;
    cin>> tyer;
    float dtl;
    float has;
    float dbb;
    switch (tyer)
       {case 1:
          dtl =1.8;
          has =0.8;
          dbb =0.7;
        break;
       case 2:
          dtl =2;
          has =0.875;
          dbb =0.5;
        break;
        case 3:
          dtl =1.4;
          has =0.7;
          dbb =0.3;
        break;
       case 4:
          dtl =1.8;
          has =1;
          dbb =0.44;
        break;
        case 5:
          dtl =1.88;
          has =1.1;
          dbb =0.75;
        break;
       case 6:
          dtl =1.9;
          has =0.81;
          dbb =0.69;
        break;
        case 7:
          dtl =1.78;
          has =0.5;
          dbb =0.83;
        break;
       case 8:
          dtl =2;
          has =0.78;
          dbb =0.78;
        break;
        case 9:
          dtl =1.69;
          has =0.58;
          dbb =0.53;
        break;
         default:
        cout << "Not a valid option" << endl;
        }
        cout<< "Diagonal tail length ="; cout<< gape*dtl; cout<< "mm"<<endl;
        cout<< "Height above shank ="; cout<< gape*has; cout<< "mm"<<endl;
        cout<< "Distance behind bend ="; cout<< gape*dbb; cout<< "mm"<<endl;
    
        cout<< "Thanks for testing, RollCast \n Press ANY KEY to continue";
    
      return 0;
      }
    This is what I have so far, however when I debug it, the console window disappears when it gets the switch part just after I declare the 3 floats

  5. #5
    spaghetticode
    Guest
    For two, don't put variables in brackets when outputting their value.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Your code might say "Press ANY KEY to continue" but it doesn't actually wait for a key. Check the FAQs about that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    10
    In the switch for each case use a char so example would be case 'a':
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {cout<< "Welcome to RollCast's Classic Salmon Fly Proportion Calculator!"<<endl;
    cout<< "Please enter the gape of your hook in mm."<<endl;
    int gape;
    cin>> gape;
    cout<< "Your hook gape ="; cout<< (gape); cout<< "mm"<<endl;
    cout<< "Please select which tyers proportions you wish to use"<<endl;
    cout<< "a: Kelson \nb: Cohen \nc: Alcott \nd: Carne \ne: Guidry \nf: Inman \ng: Boyer \nh: Ostoj \ni: Gotzmer"<<endl;
    cout<< "Please input the number for which proportion you wish to use"<<endl;
    char tyer;
    cin>> tyer;
    float dtl;
    float has;
    float dbb;
    switch (tyer)
       {
           case 'a':
          dtl =1.8;
          has =0.8;
          dbb =0.7;
        break;
       case 'b':
          dtl =2;
          has =0.875;
          dbb =0.5;
        break;
        case 'c':
          dtl =1.4;
          has =0.7;
          dbb =0.3;
        break;
       case 'd':
          dtl =1.8;
          has =1;
          dbb =0.44;
        break;
        case 'e':
          dtl =1.88;
          has =1.1;
          dbb =0.75;
        break;
       case 'f':
          dtl =1.9;
          has =0.81;
          dbb =0.69;
        break;
        case 'g':
          dtl =1.78;
          has =0.5;
          dbb =0.83;
        break;
       case 'h':
          dtl =2;
          has =0.78;
          dbb =0.78;
        break;
        case 'i':
          dtl =1.69;
          has =0.58;
          dbb =0.53;
        break;
         default:
        cout << "Not a valid option" << endl;
        }
        cout<< "Diagonal tail length ="; cout<< gape*dtl; cout<< "mm"<<endl;
        cout<< "Height above shank ="; cout<< gape*has; cout<< "mm"<<endl;
        cout<< "Distance behind bend ="; cout<< gape*dbb; cout<< "mm"<<endl;
    
        cout<< "Thanks for testing, RollCast \n Press ANY KEY to continue";
    
      return 0;
      }
    Last edited by Dynamic; 11-02-2011 at 01:26 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A way to branch structures
    By kiros88 in forum C Programming
    Replies: 2
    Last Post: 09-11-2009, 12:55 PM
  2. Compile GCC Branch
    By Dae in forum Linux Programming
    Replies: 10
    Last Post: 09-06-2009, 02:33 AM
  3. branch
    By only1st in forum C++ Programming
    Replies: 2
    Last Post: 05-07-2007, 11:24 AM
  4. wxWidgets - branch moving
    By pixsta in forum C++ Programming
    Replies: 0
    Last Post: 01-27-2006, 10:15 AM
  5. How do you branch to another part of the program?
    By gcn_zelda in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2003, 12:27 PM