Argh! *pulls out hairs* Please help me.

This is a discussion on Argh! *pulls out hairs* Please help me. within the C++ Programming forums, part of the General Programming Boards category; Hello again. So far, my ventures have been productive, also thanks to some excellent support I got here. My thanks ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    20

    Unhappy Argh! *pulls out hairs* Please help me.

    Hello again.

    So far, my ventures have been productive, also thanks to some excellent support I got here. My thanks for that .

    Now, however, I have a new problem >.<.

    Since I am planning on writing a rather huge program with several functions, which will be strung together at some point, I decided to split it up into individual, smaller programs first, to test if they all do what they are supposed to do.

    The first of these is the program with the code below this. It is designed to do the following (though the comment should give some hints :P): It accepts input for a name and stores it in a string, then it queries for a 'casting cost', again storing the input into a string.
    Now, casting cost is comprised of colorless (a simple number such as 1, 2, 3 or whatever) and the five main colors: White (W), Blue (U), Black (B), Red (R) and Green (G).
    Thus, a spell with a casting cost of 2 colorless and 2 green would be akin to 2GG.

    following a simple example program from the site, I used the program on the side that counts the number of times "cat" occurs in the input sentence. (Should be under the Lesson on Strings)
    I simply edited it to incorporate my variables instead of the variables used there.

    I compiled the code, got a few errors, quickly removed them (thank god, there were only some stupid errors like a forgotten semicolon or a forgotten " here and there :-D) and compiled again. It compiled! I was happy.

    Then I ran it.
    It asked me for a Spell Name. I was delighted, it worked.
    I entered a spell name, and it went on to ask me for Casting Cost! Woohoo!
    But as soon as I hit the enter/return key after entering a casting cost, the thing immediately shuts down . I thought I added all the proper instances of cin.ignore() and cin.get() to avoid this issue!

    Can anyone help me identify why my program immediately shuts down after I press the enter/return key after typing in the Casting Cost?

    Thank you so much!

    (lots of code...if anyone has any suggestions on how to make it more efficient while sticking to relatively easy code, I'd love to hear it, too.)

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    string SpellName = " ";
    // To ask for Spell Name
    cout<< "Spell Name: \n";
    cin>> SpellName;
    cin.ignore();
    
    string CastingCost = " ";
    // To ask for Mana Cost
    cout<< "Casting Cost: \n";
    cin>> CastingCost;
    cin.ignore();
    
    //Defining variables needed to find and recognise colors.
    int White_Counter = 0;
    int WhiteMana = 0;
    int Blue_Counter = 0;
    int BlueMana = 0;
    int Black_Counter = 0;
    int BlackMana = 0;
    int Red_Counter = 0;
    int RedMana = 0;
    int Green_Counter = 0;
    int GreenMana = 0;
    int ColorNumber = 0;
    string Colors = "Colorless.\n";
    string Color1 = " ";
    string Color2 = " ";
    string Color3 = " ";
    string Color4 = " ";
    string Color5 = " ";
    
    
    // Series of 'for' statements with .find to find colored mana and count it.
    // ColorMana can be used for seeing if the spell is of a color AND the amount of mana.
    // Color_Counter is an indicator for the .find function to function properly.
    for(White_Counter = CastingCost.find("W", 0); White_Counter != string::npos; White_Counter = CastingCost.find("W", White_Counter))
    {
        WhiteMana++;      
        White_Counter++;  // Move past the last discovered instance to avoid finding same string
    }
    
    for(Blue_Counter = CastingCost.find("U", 0); Blue_Counter != string::npos; Blue_Counter = CastingCost.find("U", Blue_Counter))
    {
        BlueMana++;      
        Blue_Counter++;  // Move past the last discovered instance to avoid finding same string
    }
    
    for(Black_Counter = CastingCost.find("B", 0); Black_Counter != string::npos; Black_Counter = CastingCost.find("B", Black_Counter))
    {
        BlackMana++;      
        Black_Counter++;  // Move past the last discovered instance to avoid finding same string
    }
    
    for(Red_Counter = CastingCost.find("R", 0); Red_Counter != string::npos; Red_Counter = CastingCost.find("R", Red_Counter))
    {
        RedMana++;      
        Red_Counter++;  // Move past the last discovered instance to avoid finding same string
    }
    
    for(Green_Counter = CastingCost.find("G", 0); Green_Counter != string::npos; Green_Counter = CastingCost.find("G", Green_Counter))
    {
        GreenMana++;      
        Green_Counter++;  // Move past the last discovered instance to avoid finding same string
    }
    
    // Series of 'if' statements to set color values to strings
    if (WhiteMana > 0) {
                  Color1 = "White";
                  }
    if (BlueMana > 0) {
                 Color2 = "Blue";
                 }
    if (BlackMana > 0) {
                  Color3 = "Black";
                  }
    if (RedMana > 0) {
                Color4 = "Red";
                }
    if (GreenMana > 0) {
                  Color5 = "Green";
                  }
    
    // Set of 'if' statements that ouput the colors if the previous 'if' statements counted them   
    if (Color1 == "White") {
               cout<<Color1;
               ColorNumber++;
               }
    
    if (Color2 == "Blue") {
               if (ColorNumber > 0) {
                        cout<<" and "<<Color2<<"";
                        ColorNumber++;
                        } 
                        else {
                             cout<<Color2;
                             ColorNumber++;
                             }
    
    if (Color3 == "Black") {
               if (ColorNumber > 0) {
                        cout<<" and "<<Color3<<"";
                        ColorNumber++;
                        } 
                        else {
                             cout<<Color3;
                             ColorNumber++;
                             }
    
    if (Color4 == "Red") {
               if (ColorNumber > 0) {
                        cout<<" and "<<Color4<<"";
                        ColorNumber++;
                        } 
                        else {
                             cout<<Color4;
                             ColorNumber++;
                             }
    
    if (Color5 == "Green") {
               if (ColorNumber > 0) {
                        cout<<" and "<<Color5<<"";
                        ColorNumber++;
                        } 
                        else {
                             cout<<Color5;
                             ColorNumber++;
                             }
    
    //Checking for correct reply to test if the program worked.
    int CorrectReply = 0;
    int ErrorMessage = 0;
    int Useless_Counter = 0;
    
    cout<< "Is this correct?\n Press 1 for yes, press 2 for no.\n";
    cin>> CorrectReply;
    cin.ignore();
    
    for (ErrorMessage = 0; ErrorMessage == 1; Useless_Counter++) {
        if (CorrectReply = 1) {
                     cout<< "Thank you for using this program.\n";
                     ErrorMessage = 0;
                     cin.get();
                     }
        else {
                     if (CorrectReply = 2) {
                                      cout<< "Please return to your code editor and redo the code.\n";
                                      ErrorMessage = 2;
                                      cin.get();
                                      }
                     else {
                                      cout<< "Error, wrong input. Please enter 1 for yes or 2 for no.\n";
                                      ErrorMessage = 1;
                                      cin>> CorrectReply;
                                      cin.ignore();
                                      }
            }
        }
    }   
    }}
    }}

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    What input do you give it?

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    The cin line at the query for a casting cost stores the string given as the string CastingCost, which is then used for the '.find' lines.

    I have tried to enter several mana costs, such as:
    3BG
    BG
    R G

    All returned the same result: immediate closure of the program.
    I have the feeling it's just a little stupid detail I've overlooked or something -_-'

    edit: oh, I also tried lowercase like 'bg', but to no effect either .
    Last edited by Athildur; 04-03-2007 at 06:00 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    I mean, what exactly do you type for the whole program when it fails? What do you enter as the Spell Name?

    Also, put a simple cout statement after each of your for loops to see how far it gets.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Erm, I entered Spiritmonger the first time, for the Spellname.

    The other times, I just gave it some random letters out of frustration :-p. Like xy, fh, or things like that.

    Oh, the cout thing is a pretty smart thing. I hadnt considered that. Thanks, I'll give that a try .

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Update:

    Okay, so I just did this one simple thing to test:
    I entered 'Lantern' as a name, then 'W' as cost.

    For the code, I updated it with a simple cout line after the 'W' counting loop, and then a cin.get()

    This worked. So, there's nothing wrong with the 'W' counting loop.
    The other counting loops are literally copies of that one, except I changed variable names to match the color it is counting for.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    The best idea is to use a debugger. If you have VC++ it has an excellent and relatively easy to use debugger. gdb is another debugger that might be available with gcc and IDE's that use gcc, although I'm not entirely sure how user-friendly it is.

    If you don't have a debugger, then you have to use debugging statements like the cout lines.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Update2:

    Just added the same cout line for all color testers. They all output.
    One slight problem (duh): The code I implemented makes it so that the .find loops don't actually count the amount of 'W's or 'R's. Instead, I just get an output number equal to the number of chars it checked after it checked that appropriate letter -_-'.

    Edit: Oh, ok. I'll get to looking for a debugger. Thanks .

    Any chance you might be able to tell me how to fix the problem earlier in this post? I copied the catfinder from the tutorial...so I don't get why that one does work and this one doesnt (assuming that one indeed does work).

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Update:

    Erm...ok, so Dev-C++ has a debugger...I couldn't tell you what it told me because It's just a heap of confusing mumbo-jumbo to me...

    this is what I got:
    Starting Program: (address)
    frames-invalid
    frames-invalid
    frames-invalid
    frames-invalid
    frames-invalid
    starting
    frames-invalid
    exited 0
    Program exited normally.
    frames-invalid
    stopped
    pre-prompt
    (gdb)
    prompt

    ...I dunno what this is supposed to tell me lol (this was with input of Spiritmonger and 3GB)

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    Yeah, that's gdb. I haven't used gcc much since I was in college and I didn't understand gdb much back then, so I can't help you with how to use it.

    It might be something simple, but I am still having a hard time visualizing what is happening. It appears that if you type 'W' for the casting cost, and your cout after the for loop appears, then it should also be doing a cout << Color1; which is "White".

    Can you give full input and output (copy and paste the console window). If the console is disappearing, open a command prompt and run it from there.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    actually, I have news.

    I decided I was being utterly silly...I actually made my own code more efficient! :P.

    You can remember the following code and it's cousin for the other colors, right?
    Code:
    if (Color1 == "White") {
               cout<<Color1;
               ColorNumber++;
               }
    Well, I threw them all out, deciding proper grammar isnt important since this won't be a public program anyway...and so, I just added the following:

    Code:
    cout<< "The colors of this spell are: "<<Color1<<" "<<Color2<<" "<<Color3<<" "<<Color4<<" "<<Color5<<"";
    cin.get();
    I figured, since the strings of Color# are basically set at an empty space until a color is actually counted in the cost, I might as well do it like this .
    This has proven to work!

    The only problem remaining now is the following: It still doesn't count the way I want it to.
    Might you have a suggestion that would allow me to 'count' the number of 'W', 'U', 'B', 'R' and 'G's from my input string? Because, at the moment, that's the only thing I really need worked out.

    Thanks so much for your help Daved. You're my C++ hero :-D.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    Your way of counting seems to work fine for me. I ran this test program and it correctly outputted 4. In your code above you never use the Mana count anyway, so I can't tell why you think it is not loading correctly.
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string CastingCost = "WGWWGW";
        int White_Counter = 0;
        int WhiteMana = 0;
        for(White_Counter = CastingCost.find("W", 0);
            White_Counter != std::string::npos;
            White_Counter = CastingCost.find("W", White_Counter))
        {
            WhiteMana++;      
            White_Counter++;  // Move past the last discovered instance to avoid finding same string
        }
        std::cout << WhiteMana << '\n';
    }

  13. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Hrm, how strange...well, I will just have to tinker with it until it comes out right for me :-P.

    Thank you for your help, you've made life a lot easier for me .

  14. #14
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Daved, I think I know why something was wrong...

    It must have had to do with you prefixing std:: before the string declaration and before the string::npos and the cout...because that's the only difference I see and when I change it in my own code to that, it does work :-P.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    You don't need std:: when you have
    Code:
    using namespace std;
    It's the same thing. std:: or using namespace std.

    GDB tutorial: http://www.cprogramming.com/gdbtutorial.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 03-26-2003, 03:57 PM
  2. argh.... why is the percent field always 0.00?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 02-24-2002, 05:27 PM
  3. Argh... I need help with strings = |
    By rmullen3 in forum C++ Programming
    Replies: 10
    Last Post: 01-28-2002, 06:05 PM
  4. ARGH! (argc and argv too!) HD Space!!!
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 12-05-2001, 03:05 AM
  5. argh! (argc and argv too!)
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 11-11-2001, 03:01 PM

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