Thread: How to jump directly in c++ if statement

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    37

    How to jump directly in c++ if statement

    HELLO,
    I have made code in which i want to jump from CHD to CL
    but it is not working why?

    Code:
    #include <iostream>using namespace std;
    
    
    int main()
    {
      while(1){
       char IP1[10];
        
        std::string bar = "CHD";
        std::string bar1 = "KSH";
        std::string bar2 = "CL";
        
        cin >> IP1;
       
        if (IP1==bar) std::cout << "PLEASE TELL WHERE YOU WANT TO GO  \n";
        
       cin >> IP1;
       if (IP1==bar1) std::cout << "YOUR TICKET CHARGE IS Rs5/.  \n";
    
    
         
       cin >> IP1;
         if (IP1==bar2) std::cout << "YOUR TICKET CHARGE IS Rs10/.  \n";
      
       return 0;
    }}

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    156
    You can't compare a char array with a std::string.
    Better make IP1 as string as well.
    In C++ you rarely need char arrays, so better use strings.

    Also how do you want to exit your while loop ?

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    37
    HELLO I HAVE DONE

    WORKING PERFECT

    Code:
    #include <iostream>using namespace std;
    
    
    int main()
    {
      while(1){
       char IP1[10];
        
        std::string bar = "CHD";
        std::string bar1 = "KSH";
        std::string bar2 = "CL";
        std::string bar3 = "VS";
        std::string bar4 = "VIS";
        std::string bar5 = "GTB";
        std::string bar6 = "MT";
        std::string bar7 = "AZ";
        std::string bar8 = "AN";
        std::string bar9 = "JH";
         std::string bar10 = "HB";
         std::string bar11 = "RS18";
        std::string bar12 = "SB";
        std::string bar13 = "CB";
         std::string bar14 = "ND";
         std::string bar15 = "RC";
         std::string bar16 = "PC";
         std::string bar17 = "CS";
         std::string bar18 = "UB";
          std::string bar19 = "LK";
          std::string bar20 = "JB";
           std::string bar21 = "INA";
           std::string bar22 = "AIMS";
           std::string bar23 = "GP";
           std::string bar24 = "HK";
            std::string bar25 = "MN";
         std::string bar26 = "SA";
          std::string bar27 = "QM";
           std::string bar28 = "CH"; 
           std::string bar29 = "SU";
            std::string bar30 = "GH";
            std::string bar31 = "AR";
            std::string bar32 = "GD";
            std::string bar33 = "SI";
            std::string bar34 = "MGR";
            std::string bar35 = "IFFCO";
            std::string bar36 = "HUDA";
        cin >> IP1;
       
       
        if (IP1==bar) std::cout << "PLEASE TELL WHERE YOU WANT TO GO  \n";
       cin >> IP1;
        {
    	
       
       if (IP1==bar1) std::cout << "YOUR TICKET CHARGE IS Rs5/.  \n";
    
    
         
         if (IP1==bar2) std::cout << "YOUR TICKET CHARGE IS Rs10/.  \n";
         if (IP1==bar3) std::cout << "YOUR TICKET CHARGE IS Rs15/.  \n";
         
         if (IP1==bar4) std::cout << "YOUR TICKET CHARGE IS Rs20/.  \n";
         if (IP1==bar5) std::cout << "YOUR TICKET CHARGE IS Rs25/.  \n";
          if (IP1==bar6) std::cout << "YOUR TICKET CHARGE IS Rs30/.  \n";
          if (IP1==bar7) std::cout << "YOUR TICKET CHARGE IS Rs35/.  \n";
           if (IP1==bar8) std::cout << "YOUR TICKET CHARGE IS Rs40/.  \n";
           if (IP1==bar9) std::cout << "YOUR TICKET CHARGE IS Rs45/.  \n";
           if (IP1==bar10) std::cout << "YOUR TICKET CHARGE IS Rs50/.  \n";
           if (IP1==bar11) std::cout << "YOUR TICKET CHARGE IS Rs55/.  \n";
           if (IP1==bar12) std::cout << "YOUR TICKET CHARGE IS Rs60/.  \n";
           if (IP1==bar13) std::cout << "YOUR TICKET CHARGE IS Rs5/.  \n";
           if (IP1==bar14) std::cout << "YOUR TICKET CHARGE IS Rs12/.  \n";
           if (IP1==bar15) std::cout << "YOUR TICKET CHARGE IS Rs20/.  \n";
           if (IP1==bar16) std::cout << "YOUR TICKET CHARGE IS Rs25/.  \n";
            if (IP1==bar17) std::cout << "YOUR TICKET CHARGE IS Rs30/.  \n";
            if (IP1==bar18) std::cout << "YOUR TICKET CHARGE IS Rs35/.  \n";
            if (IP1==bar19) std::cout << "YOUR TICKET CHARGE IS Rs40/.  \n";
            if (IP1==bar20) std::cout << "YOUR TICKET CHARGE IS Rs45/.  \n";
            if (IP1==bar21) std::cout << "YOUR TICKET CHARGE IS Rs50/.  \n";
            if (IP1==bar22) std::cout << "YOUR TICKET CHARGE IS Rs55/.  \n";
        if (IP1==bar23) std::cout << "YOUR TICKET CHARGE IS Rs60/.  \n";
        if (IP1==bar24) std::cout << "YOUR TICKET CHARGE IS Rs65/.  \n";
        if (IP1==bar25) std::cout << "YOUR TICKET CHARGE IS Rs70/.  \n";
        if (IP1==bar26) std::cout << "YOUR TICKET CHARGE IS Rs75/.  \n";
        if (IP1==bar27) std::cout << "YOUR TICKET CHARGE IS Rs80/.  \n";
        if (IP1==bar28) std::cout << "YOUR TICKET CHARGE IS Rs85/.  \n";
        if (IP1==bar29) std::cout << "YOUR TICKET CHARGE IS Rs90/.  \n";
        if (IP1==bar30) std::cout << "YOUR TICKET CHARGE IS Rs95/.  \n";
        if (IP1==bar31) std::cout << "YOUR TICKET CHARGE IS Rs100/.  \n";
        if (IP1==bar32) std::cout << "YOUR TICKET CHARGE IS Rs105/.  \n";
        if (IP1==bar33) std::cout << "YOUR TICKET CHARGE IS Rs110/.  \n";
        if (IP1==bar34) std::cout << "YOUR TICKET CHARGE IS Rs115/.  \n";
        if (IP1==bar35) std::cout << "YOUR TICKET CHARGE IS Rs120/.  \n";
        if (IP1==bar36) std::cout << "YOUR TICKET CHARGE IS Rs125/.  \n";
    }
    }
       return 0;
    }

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    .....
    Last edited by userxbw; 11-10-2017 at 07:49 AM.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You should not use char arrays with cin's >> operator. This is a very serious safety concern. What happens if the user enters more than 9 characters? Have you considered using std::vector to store all your destinations? Maybe even std::map, to store the destination and the price. Any time you start adding sequential numbers to variable names, you should be thinking about array-like structures like std::vector. Using std::map as I suggest would literally make your code half as long, and arguably more correct.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I do agree, especially with that amount of same type different values put in an array would be better suited as well, vector string would give you that.
    Code:
    #include<iostream>
    #include<vector>
    #include<cstring>
    #include<fstream>
    
    using namespace std;
    
    int main ()
    {
    vector <string> space_list;
    
        string word;
        ifstream fillfile("string_vector_data", ios::in);
        while(!fillfile.eof()) 
        {
            fillfile >> word;
            space_list.push_back(word);
        }
        if (!fillfile) 
        {
            cerr << "Can't open input file " << endl;
            exit(1);
        }
        for (unsigned int n=0; n<space_list.size(); ++n) 
                cout << space_list.at( n ) << endl;
    return 0;
    }
    Code:
    $ ./string_vector
    CHD
    KSH
    CL
    VS
    VIS
    GTB
    MT
    AZ
    AN
    JH
    then all you need to do is put it together maybe using a loop with if 's or even better I personally would try to figure out how to use a switch using element numbers or IP1 maybe make it an int I do not know it is depending on what you're doing with it. Either way I know their has to be a better way at this.
    Last edited by userxbw; 11-10-2017 at 02:07 PM.

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861

    Post just trying to think of a better way to do this.

    dummy code
    Code:
    int main()
    {
    
    char where [30]; // if char is one char it is an int value you can use
    // for a switch.
    
    cout << " where ya want a go?";
    cin >> where;
    // maybe even have a means to print out a list of available places to go to. 
    // then user can pick one.
    
    // needs to be an int here
    //the only thing needed is a means to connect the two together, 
    // input against data 
    // enum 
    // for easier comparison 
    switch ( where )
    {
        // std::string bar = "CHD";
          case 'c':               
               std::cout << "YOUR TICKET CHARGE IS Rs5/.  \n";
               break;
    
         std::string bar1 = "KSH";
    
        std::string bar2 = "CL";
    
        std::string bar3 = "VS";
        std::string bar4 = "VIS";
        std::string bar5 = "GTB";
    
        std::string bar6 = "MT";
        std::string bar7 = "AZ";
    
        cin >> IP1;
    
    return 0;
    }
    so I would scrap that idea and I'd go to enum.
    Code:
    #include<iostream>
    #include<vector>
    #include<cstring>
    #include<fstream>
    
    using namespace std;
     
    int main()
    {
    
    int where;
    
    vector <string> space_list;
    
        string word;
        ifstream fillfile("string_vector_data", ios::in);
        if (!fillfile) 
        {
            cerr << "Can't open input file " << endl;
            exit(1);
        }
        while(!fillfile.eof()) 
        {
            fillfile >> word;
            space_list.push_back(word);
        }
        fillfile.close();
    
        for (unsigned int n=0; n<space_list.size(); ++n) 
                cout << n << ": "<< space_list.at( n ) << endl;
                
         
      
    cout << "pick a number where ya want a go?";
    cin >> where;
    
    
        switch (where)
        {
            case 0:
                std::cout << "YOUR TICKET CHARGE IS Rs10/.  \n";
                break;
            case 1:
                std::cout << "YOUR TICKET CHARGE IS Rs20/.  \n";
                break;
            default:
                break;
        }
    return 0;
    }
    I scraped that enum idea too, really fast. building off
    what I already had got me this output.
    Code:
    userx@slackwhere:~/bin
    $ ./enummy_prices
    0: CHD
    1: KSH
    2: CL
    3: VS
    4: VIS
    5: GTB
    6: MT
    7: AZ
    8: AN
    9: JH
    pick a number where ya want a go?0
    YOUR TICKET CHARGE IS Rs10/.
    that is the benefits of an array as well.

    I'd figure out a formatting method to print that really long list in columns, left to right. So it will not print out in its entire length making it easier to read.
    Last edited by userxbw; 11-10-2017 at 02:46 PM.

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    completed after thought: output
    Code:
    userx@slackwhere:~/bin
    $ ./enummy_prices
    space_list size= 10
    0: CHD 1: KSH 2: CL 3: VS 
    4: VIS 5: GTB 6: MT 7: AZ 
    8: AN 9: JH 
    pick a number where ya want a go?1
    YOUR TICKET CHARGE IS Rs20/.  
    userx@slackwhere:~/bin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. jump into c++
    By reaper2984 in forum C++ Programming
    Replies: 9
    Last Post: 12-22-2013, 09:23 AM
  2. Jump to another function
    By ulti-killer in forum C Programming
    Replies: 2
    Last Post: 11-15-2012, 05:12 PM
  3. character jump
    By pode in forum Game Programming
    Replies: 7
    Last Post: 12-31-2002, 07:22 AM
  4. assembled Jump
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 12-14-2001, 10:10 AM
  5. next step in a jump
    By cheesehead in forum Linux Programming
    Replies: 0
    Last Post: 12-11-2001, 10:08 AM

Tags for this Thread