While Loop trouble.

This is a discussion on While Loop trouble. within the C++ Programming forums, part of the General Programming Boards category; Well, since my program is so short, I'm just going to put it all here. I am sure that there ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    While Loop trouble.

    Well, since my program is so short, I'm just going to put it all here.
    I am sure that there is something that I'm not understanding here.
    When I run this particular program, I'm wanting it to take a character that is input by the user, and return a number by calling the threatmult function. However when I run the program it simply gets stuck in the while loop in the main function. I am missing something simple here, and I know it. So I'm basically asking for someone to spell out what I'm doing wrong in simple terms, because I'm just not understanding it. Also, if there's a better way to do the things that I'm trying to do, please let me know. If someone feels really kind today, they also might want to consider helping me know about error detection. I'm trying to make sure they can enter in upper or lower case letters : 'h' 'l' and 'n'. Oh, and I can't seem to get a new line after the
    cin >> threatentry ;
    Well, thanks in advance!


    Code:
    #include <iostream.h>
    
    double threatmult(char threat)
    {
    	double multiplier;
        if (threat == 'H')
            multiplier = 1;
        else if (threat == 'h')
            multiplier = 1;
        else if (threat == 'L')
            multiplier = .5;
        else if (threat == 'l')
            multiplier = .5;
        else 
            multiplier = .33333;
    		
        return (multiplier);
    }
    
    int main()
    {
        char threatentry;
        
        cout << "Please enter a valid threat level." << endl;
        cout << "Valid entries are the following letters (upper or lower case): " ;
        cout << "H,L,N" << endl;
        cin >>  threatentry ;
        cout << "You entered : " << threatentry ;       	
        
        while (threatentry != (('H' || 'h') || ('L' || 'l') || ('N' || 'n')))
            {
                cout << "Invalid entry, enter either H, L, or N." << endl;
                cin >> threatentry ;
            }
    	
    	return 0;	
    }

  2. #2
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    but from ur code above, u are never using your function "threatmult()" anywhere in ur program!

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    You have to compare threatentry to each character individually, in your while loop.

    To shorten the while loop and make the code more readable, I would suggest you do it like this:
    Code:
    #include <iostream>
    using namespace std;
    
    bool isValidEntry(char a) {
    	return (a == 'H' || a == 'h' || a == 'L' || a == 'l' || a == 'N' || a == 'n');
    }
    
    int main()
    {
        char threatentry;
        
        cout << "Please enter a valid threat level." << endl;
        cout << "Valid entries are the following letters (upper or lower case): " ;
        cout << "H,L,N" << endl;
        cin >>  threatentry ;
        cout << "You entered : " << threatentry ;       	
        
    	while (!isValidEntry(threatentry)) {
                cout << "\nInvalid entry, enter either H, L, or N.";
                cin >> threatentry ;
    	}
    
    	return 0;	
    }
    Last edited by defcon; 10-03-2005 at 04:05 PM.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    Furthering the plot.

    Ok, so my code is below. I've tested it, and it works just fine. I have some questions on it, because I couldn't get mine to work, and there are some things that I should be understanding here. The questions are put in as comments below. And, again, is there any way to refine the code? Just one more thing, I had been searching online for hours for a solution until I found this forum, and my question was answered very quickly, so I am very grateful for the help that people on this forum give.

    Code:
    #include <iostream>
    using namespace std;//What is this line used for?
    
    bool isValidEntry(char a) {
    	return (a == 'H' || a == 'h' || a == 'L' || a == 'l' || a == 'N' || a == 'n');//I simply don't understand how this line works.  Hopefully someone can spell it out in simple terms for me. 
    }
    
    double threatmult(char a) {
        if (a == 'H' || a == 'h')
            return (1.0);
        else if (a == 'L' || a == 'l')
            return (0.5);
        else 
            return (0.33333);
    }
    
    int main()
    {
        char threatentry;
        double multiplier;
        
        cout << "Please enter a valid threat level." << endl;
        cout << "Valid entries are the following letters (upper or lower case): " ;
        cout << "H,L,N" << endl;
        cin >>  threatentry ;
        cout << "You entered : " << threatentry << endl;      	
        
    	while (!isValidEntry(threatentry)) {
                cout << "\nInvalid entry, enter either H, L, or N.";
                cin >> threatentry ;
    	}
        
        multiplier = threatmult(threatentry);
        cout << "The threat multiplier is " << multiplier << endl;
        
    	return 0;	
    }

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    Quote Originally Posted by Aterxerxes
    Ok, so my code is below. I've tested it, and it works just fine. I have some questions on it, because I couldn't get mine to work, and there are some things that I should be understanding here. The questions are put in as comments below. And, again, is there any way to refine the code? Just one more thing, I had been searching online for hours for a solution until I found this forum, and my question was answered very quickly, so I am very grateful for the help that people on this forum give.
    using namespace std; allows you to use cout and cin.

    If you did not have this line then you would have to put std::cout and std::cin in your code in place of cout and cin. In some cases this is a good idea if you are only using a small part of the std. I suggest you google for tutorials on namespaces.

    The function that I created, isValidEntry has to return a bool value agree?

    Lets look at this:
    pretend the character passed into this function was ... G

    G == H is false right?
    G == h is false right?
    ...
    G == n is false right?
    as you can see, all of the logical expressions equate to false.

    when you have (false || false) it evaluates to false.

    so when G is passed into my function, false is returned.

    now we have (back inside int main()) while (!isValidEntry('G'))

    we know that the function returned false, so !false is true.

    since the expression in the while loop is true, it will execute the body of the while loop..

    That should explain it.. I think.. Ask to clearify something if needed.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    Gratitude

    Thanks for all of those who posted replies, my confusion is over ... at least for this phase of my program learning. I simply couldn't understand when reading the tutorials, and there's something about someone coming to a specific problem that YOU have, and telling you exactly how it works, and what needs to be changed. Thanks to you defcon especially, your explanation was perfect. That is to say, it took the cloud of confusion from my vision, and let me see with new clarity. Poetic, eh? Anyway, thanks again, I'll definitely come back here if I need any more help, and I'll try and contribute to helping others in the future.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    Hi, glad to of helped. I am new to these forums too, as you can see from my post date

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 03:20 PM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 03:45 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. trouble with a for loop with an if statement nested in
    By phoenix-47 in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2005, 04:24 PM
  5. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM

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