Like Tree2Likes

Header File and tolower issue with string

This is a discussion on Header File and tolower issue with string within the C++ Programming forums, part of the General Programming Boards category; good. But if I do, and the sting is in all lowercase, will it take the newly lowercase string and ...

  1. #16
    Registered User Inkinsarto's Avatar
    Join Date
    Apr 2013
    Posts
    34
    good. But if I do, and the sting is in all lowercase, will it take the newly lowercase string and check to see if it equals anything?
    Code:
    string x;
     
    cout<<"Please enter an attribute. Then press ENTER.\n";
    cout<<"The choices are: \n";
    cout<<"1. Steel\n";
    cout<<"2. Dragon\n";
    cout<<"3. Fighting\n";
    cout<<"4. Lightning\n";
    cout<<"5. Earth\n";
    cout<<"\n";
    cout<<endl;
    cout<<endl;
    cout<<endl;
    cout<<"type: ";
    getline(std::cin, x); 
    transform(x.begin(), x.end(), tolower, etc.)
    if(x == "steel"){
    //run program
    }
    if(dragon)
    //etc.
    

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    Why don't you write a program to test? This is what I do when I want to say, make sure that I know a feature/library function/idea works. Once I have it down pat, I then incorporate it into my actual program.
    jimblumberg likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    2,879
    But if I do, and the sting is in all lowercase, will it take the newly lowercase string and check to see if it equals anything?
    No tolower() just converts the elements to lower case, it doesn't check anything.

    Jim

  4. #19
    Registered User Inkinsarto's Avatar
    Join Date
    Apr 2013
    Posts
    34
    Interesting.

  5. #20
    Registered User Inkinsarto's Avatar
    Join Date
    Apr 2013
    Posts
    34
    Would it still check the choices? I can add a code that express the issue. (It asks the user to enter a choice, then checks other choices.)

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    What do you mean?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User Inkinsarto's Avatar
    Join Date
    Apr 2013
    Posts
    34
    Here is the full code that I want to use tolower() on:
    Code:
    #include <iostream>
    #include <algorithm>  //All of the files needed to run the program
    #include <cctype>
    #include <iostream>
    #include <string>
    #include <windows.h>
    #include <stdio.h>    //Plus alot more
    #include <locale>
    #include <math.h>
     
    using namespace std;
     
        float experience;
        int Hp, level, attack, defence, enemyHp = 50, enemyattack, enemydefence, afterattack = 0, battleloop = 0;
     
    int main()
    {
        std::string mmchoice;
        int y = 0,z = 0, smenuloop = 0;
        string battlechoice, smenuchoice;          //Variables
        float experience;
        int Hp, level, attack, defence, enemyHp = 50, enemyattack, enemydefence, afterattack = 0, battleloop = 0;
        cout<<"Press ENTER.";
        cin.ignore();
        for ( int x = 500; x < 537; x++ ) {
            for ( int y = 0; y< 90; y++){
              y = y + 14;
              x = x + 20;
              for(int ldloop = 0; ldloop < 21; ldloop++){
                 cout<<endl;
              }
          Sleep(2000);//2000
          cout<<"Loading file directories( "<< x <<" of 640)"<<endl;
          cout<<"Reaching files to load program...\n";
          cout<<"Progress: "<< y <<"% complete\n";
          }
          for(int ldloop = 0; ldloop < 21; ldloop++){
             cout<<endl;
          }
          Sleep(2000);
          cout<<endl;
          cout<<endl;
          cout<<endl;
          cout<<endl;
          Sleep(1500);
          cout<< x <<" files out of 640 files accessed.\n";
          Sleep(1500);
          cout<<"89 percent complete.\n";
          Sleep(1500);
          cout<<"Recovering files";Sleep(1000);cout<<".";Sleep(1200);cout<<".";Sleep(2000);cout<<".";Sleep(700);cout<<"\n\n\n\n";
          Sleep(1000); cout<<"Finishing process.\n"; Sleep(5000);/*5000*/ cout<<"Process complete.\n";
      }
      Sleep(500);
      cout<<"Press ENTER to continue.";
      cin.ignore();
      for(int ldloop = 0; ldloop < 15; ldloop++){
      cout<<endl;
      } cout<<"\n\n\n";
      cout<<"                           Welcome to RPG Maina!\n\n\n\n\n\n";
     
        do{                // This begins the loop or repeatable program
        cout<<"Please enter an attribute. Then press ENTER.\nThe choices are: \nSteel\nDragon\nFighting\n";
        for(int ldloop = 0; ldloop < 7; ldloop++){
             cout<<endl;
        }
        cout<<"______________________\n";
        cout<<"type: ";
        getline(std::cin, mmchoice);
        for(int endloop = 0; endloop < 22; endloop++){
        cout<<endl;
        }
        {  //Within this proccess, the program shows a reading and loading text.
            cout<<"\n"; cout<<"analizing"; Sleep(900); cout<<"."; Sleep(600); cout<<".";
            Sleep(800); cout<<".\n"; Sleep(500); cout<<"\n"; cout<<"\n"; cout<<"loading";
            Sleep(500); cout<<"."; Sleep(500); cout<<"."; Sleep(500); cout<<".\n";
            for(int ldloop = 0; ldloop < 15; ldloop++){
              cout<<endl;
            }
            Sleep(500);
          }
          if(mmchoice == "steel"){
              y = 0;
              level = 1;
              experience = 0;
              Hp = 100;
              attack = 10;
              defence = 20;
              enemyattack = defence + 7;
              enemydefence = attack - 4;
     
            cout<<"Here are the character stats for Steel: \n"<<endl;
            cout<<"Level: "<<level<<"\n";
            cout<<"Experience: "<<experience<<"\n";
            cout<<"Hp: "<<Hp<<"\n";
            cout<<"Attack: "<<attack<<"\n";
            cout<<"Defence: "<<defence<<"\n";
          }
          else if(mmchoice == "Steel"){
             y = 0;
             level = 1;
             experience = 0;
             Hp = 100;
             attack = 10;
             defence = 20;
     
             enemyattack = defence + 6;
             enemydefence = attack - 5;
     
            cout<<"Here are the character stats for Steel: \n"<<endl;
            cout<<"Level: "<<level<<"\n";
            cout<<"Experience: "<<experience<<"\n";
            cout<<"Hp: "<<Hp<<"\n";
            cout<<"Attack: "<<attack<<"\n";
            cout<<"Defence: "<<defence<<"\n";
          }
          else if(mmchoice == "STEEL"){
             y = 0;
             level = 1;
             experience = 0;
             Hp = 100;
             attack = 10;
             defence = 20;
     
             enemyattack = defence + 8;
             enemydefence = attack - 3;
     
            cout<<"Here are the character stats for Steel: \n"<<endl;
            cout<<"Level: "<<level<<"\n";
            cout<<"Experience: "<<experience<<"\n";
            cout<<"Hp: "<<Hp<<"\n";
            cout<<"Attack: "<<attack<<"\n";
            cout<<"Defence: "<<defence<<"\n";
          }
          else if(mmchoice == "dragon"){
             y = 0;
             level = 1;
             experience = 0;
             Hp = 100;
             attack = 15;
             defence = 15;
     
             enemyattack = defence + 7;
             enemydefence = attack - 4;
     
            cout<<"Here are the character stats for Dragon: \n"<<endl;
            cout<<"Level: "<<level<<"\n";
            cout<<"Experience: "<<experience<<"\n";
            cout<<"Hp: "<<Hp<<"\n";
            cout<<"Attack: "<<attack<<"\n";
            cout<<"Defence: "<<defence<<"\n";
          }
          else if(mmchoice == "Dragon"){
             y = 0;
             level = 1;
             experience = 0;
             Hp = 100;
             attack = 15;
             defence = 15;
     
             enemyattack = defence + 8;
             enemydefence = attack - 3;
     
            cout<<"Here are the character stats for Dragon: \n"<<endl;
            cout<<"Level: "<<level<<"\n";
            cout<<"Experience: "<<experience<<"\n";
            cout<<"Hp: "<<Hp<<"\n";
            cout<<"Attack: "<<attack<<"\n";
            cout<<"Defence: "<<defence<<"\n";
          }
          else if(mmchoice == "DRAGON"){
             y = 0;
             level = 1;
             experience = 0;
             Hp = 100;
             attack = 15;
             defence = 15;
     
             enemyattack = defence + 6;
             enemydefence = attack - 5;
     
            cout<<"Here are the character stats for Dragon: \n"<<endl;
            cout<<"Level: "<<level<<"\n";
            cout<<"Experience: "<<experience<<"\n";
            cout<<"Hp: "<<Hp<<"\n";
            cout<<"Attack: "<<attack<<"\n";
            cout<<"Defence: "<<defence<<"\n";
          }
          else if(mmchoice == "fighting"){
             y = 0;
             level = 1;
             experience = 0;
             Hp = 100;
             attack = 20;
             defence = 10;
     
             enemyattack = defence + 8;
             enemydefence = attack - 3;
     
            cout<<"Here are the character stats for Fighting: \n"<<endl;
            cout<<"Level: "<<level<<"\n";
            cout<<"Experience: "<<experience<<"\n";
            cout<<"Hp: "<<Hp<<"\n";
            cout<<"Attack: "<<attack<<"\n";
            cout<<"Defence: "<<defence<<"\n";
          }
          else if(mmchoice == "Fighting"){
             y = 0;
             level = 1;
             experience = 0;
             Hp = 100;
             attack = 20;
             defence = 10;
     
             enemyattack = defence + 6;
             enemydefence = attack - 5;
     
            cout<<"Here are the character stats for Fighting: \n"<<endl;
            cout<<"Level: "<<level<<"\n";
            cout<<"Experience: "<<experience<<"\n";
            cout<<"Hp: "<<Hp<<"\n";
            cout<<"Attack: "<<attack<<"\n";
            cout<<"Defence: "<<defence<<"\n";
          }
          else if(mmchoice == "FIGHTING"){
              y = 0;
             level = 1;
             experience = 0;
             Hp = 100;
             attack = 20;
             defence = 10;
     
             enemyattack = defence + 7;
             enemydefence = attack - 4;
     
            cout<<"Here are the character stats for Fighting: \n"<<endl;
            cout<<"Level: "<<level<<"\n";
            cout<<"Experience: "<<experience<<"\n";
            cout<<"Hp: "<<Hp<<"\n";
            cout<<"Attack: "<<attack<<"\n";
            cout<<"Defence: "<<defence<<"\n";
          }
          else if (mmchoice == "10005686"){
             y = 0;
             level = 1;
             experience = 0;
             Hp = 100000;
             attack = 10000;
             defence = 10000;
     
             enemyattack = 0;
             enemydefence = 0;
     
            cout<<"Here are the character stats for The Secret Samurai: \n"<<endl;
            cout<<"Level: "<<level<<"\n";
            cout<<"Experience: "<<experience<<"\n";
            cout<<"Hp: "<<Hp<<"\n";
            cout<<"Attack: "<<attack<<"\n";
            cout<<"Defence: "<<defence<<"\n";
          }
          else{
              y = y + 1;
              cout<<"ERROR. incorrect input.\n";
            if(y == 2){
              cout<<"Just enter a correct choice already!\n";
           }
            else if (y == 3){
              cout<<"You have GOT to be kidding me!\n";
              cout<<"It is not that hard!\n";
            }
            else if (y == 4) {
              cout<<"HOW STUPID ARE YOU!!!!!!!!!!!\n";
            }
            else if (y == 5) {
              cout<<"I give up.\n";
            }
           for(int endloop = 0; endloop < 5; endloop++){
              cout<<endl;
             }
             cout<<"press ENTER to return to the menu.\n";
             cin.ignore();
             cout<<"\n\n\n\n\n\n\n\n\n\n";
             Sleep(1200);
          }
        } while(y != 0);
     
            cout<<endl;
            cout<<"Press ENTER to continue.\n";
            cin.ignore();
            cout<<"Waiting for next proccess";
            Sleep(1200); cout<<"."; Sleep(700); cout<<"."; Sleep(500); cout<<".\n"; Sleep(1500);
            for(int ldloop = 0; ldloop < 18; ldloop++){
               cout<<endl;
            }
            cout<<"Welcome to the status menu!\nWhile here, you can buy items, train, or battle me.\n\n\n\n";
            cout<<"Market\n\n"; cout<<"Training arena\n\n"; cout<<"Final Boss\n\n"; cout<<"Press ENTER to go to continue.\n";
            cin.ignore(); cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
            do{
                do{
                cout<<"Market\n\n"; cout<<"Training arena\n\n"; cout<<"Final Boss\n\n";
                cout<<"choice: ";
                getline(std::cin, smenuchoice);
                if(smenuchoice == "market"){
                    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                    cout<<"Sorry, but the market is currently\nunder construction.\n\n";
                    cout<<"Press ENTER to return to the menu.";
                    cin.ignore();
                    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                }
                else if(smenuchoice == "Market"){
                    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                    cout<<"Sorry, but the market is currently\nunder construction.\n\n";
                    cout<<"Press ENTER to return to the menu.";
                    cin.ignore();
                    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                }
                else if(smenuchoice == "MARKET"){
                    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                    cout<<"Sorry, but the market is currently\nunder construction.\n\n";
                    cout<<"Press ENTER to return to the menu.";
                    cin.ignore();
                    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                }
                else if(smenuchoice == "training arena"){
                    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                    smenuloop = smenuloop + 1;
                }
                else if(smenuchoice == "Training Arena"){
                    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                    smenuloop = smenuloop + 1;
                }
                else if(smenuchoice == "TRAINING ARENA"){
                    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                    smenuloop = smenuloop + 1;
                }
                else if (smenuchoice == "final boss"){
                    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                    cout<<"The Final Boss is currentlly unavailable.\nPlease wait for an update to the game.\n";
                    cout<<"Press ENTER to return to the menu.";
                    cin.ignore();
                    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                }
                else if (smenuchoice == "Final Boss"){
                    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                    cout<<"The Final Boss is currentlly unavailable.\nPlease wait for an update to the game.\n";
                    cout<<"Press ENTER to return to the menu.";
                    cin.ignore();
                    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                }
                else if (smenuchoice == "FINAL BOSS"){
                    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                    cout<<"The Final Boss is currentlly unavailable.\nPlease wait for an update to the game.\n";
                    cout<<"Press ENTER to return to the menu.";
                    cin.ignore();
                    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                }
                else{
                    cout<<"Incorrect input.";Sleep(2000);cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                }
            }while( smenuloop == 0 );
     
            smenuloop = 0;
            enemyHp = 50;
            battleloop = 0;
            cout<<"It is time to train.\nPress ENTER to begin.";
            cin.ignore();
            cout<<"\n\nPlease wait while the battle system loads.";
            for(int ldloop = 0; ldloop < 2; ldloop ++){
                 Sleep(1200);
                 cout<<".";
              }
            Sleep(1200);
            for(int ldloop = 0; ldloop < 21; ldloop ++){
                cout<<"\n";
            }
            cout<<"An enemy has appeared.\n";
            Sleep(3000);
            do{
               for(int endloop = 0; endloop < 21; endloop ++){
                  cout<<"\n";
               }
            cout<<"The enemy attacks!\n";
            afterattack = enemyattack - defence;
            Sleep(1000);
            cout<<"\n\n\n\n\n\n\n\n\n\n";
     
            Hp = Hp - afterattack;
            afterattack = 0;
            Sleep(500);
            if (Hp <= 0){
                battleloop = battleloop + 1;
                cout<<"You were slain by the enemy!\n";
            }
            else{
                Sleep(1500);
                cout<<"You have "<< Hp <<" Hp\n";
                Sleep(2000);
                cout<<"Will you attack? Enter yes or no. Then press ENTER.\n";
                   getline(std::cin, battlechoice);
                   Sleep(500);
                   cout<<endl;
                if(battlechoice == "yes"){
                    cout<<"\n\n\n\n\n\n\n\n\n\n";
                    cout<<"You attack!\n";
                    Sleep(1000);
                    afterattack = attack - enemydefence;
                    enemyHp = enemyHp - afterattack;
                    cout<<"enemy Hp = "<<enemyHp<<"\n";
                    Sleep(2000);
                    if(enemyHp <= 0){
                        battleloop = battleloop + 1;
                        cout<<"The enemy ran away!\n";
                        cout<<"You have earned "<< enemyattack + enemydefence <<" !";
                        afterattack = enemyattack + enemydefence;
                        experience = afterattack;
                        Sleep(1500);
     
                        cout<<"Press ENTER to return to the status menu\n";
                        cin.ignore();
                        cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                    }
                }
                else if (battlechoice == "Yes"){
                    cout<<"\n\n\n\n\n\n\n\n\n\n";
                    cout<<"You attack!\n";
                    Sleep(1000);
                    afterattack = attack - enemydefence;
                    enemyHp = enemyHp - afterattack;
                    cout<<"enemy Hp = "<<enemyHp<<"\n";
                    Sleep(1000);
                    if(enemyHp <= 0){
                        battleloop = 1;
                        cout<<"The enemy ran away!\n";
                        cout<<"Press ENTER to return to the status menu\n";
                        cin.ignore();
                        cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                    }
                }
                else{
                    battleloop = battleloop + 1;
                    cout<<"you chose to return.\n";
                    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                }
            }
            }while ( battleloop == 0 );
        }while (z == 0);
    }
    As you can see, the attribute choice you enter is checked to see if it equals steel, Steel, STEEL, dragon, Dragon, DRAGON, fighting, Fighting, FIGHTING.
    I wish to use tolower() to change the attribute choice to all lowercase so it will only have to check for steel, dragon, fighting.

  8. #23
    Registered User Inkinsarto's Avatar
    Join Date
    Apr 2013
    Posts
    34
    I would highly recommend running the program to see how it works.

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    Quote Originally Posted by Inkinsarto
    Here is the full code that I want to use tolower() on:
    Your main function spans 400+ lines. It is less than half as bad as a pair of team mates of mine in university who wrote a 900+ line function, but they were tackling a problem that might have been more complex... (and indent style plays a part too, heh)

    Your need to break your code into functions that do one thing and do it well.

    Quote Originally Posted by Inkinsarto
    As you can see, the attribute choice you enter is checked to see if it equals steel, Steel, STEEL, dragon, Dragon, DRAGON, fighting, Fighting, FIGHTING.
    I wish to use tolower() to change the attribute choice to all lowercase so it will only have to check for steel, dragon, fighting.
    Then instead of posting your full code, you should have posted a program that demonstrates this problem. You came close to this in post #16, except that that code snippet was syntactically incorrect.

    The idea here is simple: write a function, e.g., named to_lowercase, that takes a string as an argument and returns the same string, in lowercase. You can use std::transform to implement this function. Then, you call this function when you want to check an attribute, thus you can write:
    Code:
    if (to_lowercase(attribute) == "steel")
    jimblumberg likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scope issue with header file used as interface
    By Rath in forum C Programming
    Replies: 4
    Last Post: 02-02-2012, 05:04 PM
  2. Header File Issue CodeBlocks
    By mathguy in forum C Programming
    Replies: 8
    Last Post: 11-18-2011, 04:57 PM
  3. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 11:04 AM
  4. How to use tolower command for a string??
    By yong83 in forum C++ Programming
    Replies: 8
    Last Post: 04-01-2003, 05:44 AM
  5. String header file
    By Stan100 in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2003, 01:26 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21