Simple program

This is a discussion on Simple program within the C++ Programming forums, part of the General Programming Boards category; I'm getting back into programming now that I have some stuff to do with it so I'm trying to regain ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    465

    Simple program

    I'm getting back into programming now that I have some stuff to do with it so I'm trying to regain my skills, and I can't figure out the problem with this. It is not supposed to keep looping if the user doesn't input 'y' or 'n'.
    Code:
    #include <iostream>
    using namespace std;
    int main(){
    char holla;
    do{
    		cout<<"You wanna holla at the world?\n";
    		cin>>holla;
    	switch (holla){
    		case 'y':
    			cout<<"Hello World\n";
    		break;
    		case 'n':
    			cout<<"Why the hell not?\n";
    		break;
    		default:
    			cout<<"not valid yo\n";
    		break;
    }
    }
    while(holla!='y'||'n');
    return 0;
    }
    My computer is awesome.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Code:
    while(holla != 'y' && holla != 'n')
    Last edited by SlyMaelstrom; 12-07-2005 at 09:13 PM.
    Sent from my iPad

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I changed the code and it still continues to loop regardless of input. I want it to quit after only one of them input so I don't need the &&
    Last edited by cerin; 12-07-2005 at 09:27 PM.
    My computer is awesome.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    3
    i think i got it right i think.

    Code:
    while(holla =='y'|| holla == 'n');

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Quote Originally Posted by djhabeeb
    i think i got it right i think.

    Code:
    while(holla =='y'|| holla == 'n');
    No, that's the opposite of what he wants.

    That says loop while it's 'y' or 'n' and he wants it to loop while it isn't 'y' or 'n'

    Look at my post, you copied something wrong.
    Sent from my iPad

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    *sigh* no I don't want it to continue while y or n are input. I want it to quit after one is input, but still loop if there is no input or invalid input.

    Edit: I have fixed it, but I didn't want to have to add the return 0's. I guess it will work.

    Code:
    #include <iostream>
    using namespace std;
    int main(){
    char holla;
    do{
    		cout<<"You wanna holla at the world?\n";
    		cin>>holla;
    	switch (holla){
    		case 'y':
    			cout<<"Hello World\n";
    				return 0;
    		break;
    		case 'n':
    			cout<<"Why the hell not?\n";
    				return 0;
    		break;
    		default:
    			cout<<"not valid yo\n";
    		break;
    }
    }
    while(holla!='y'||holla!='n');
    return 0;
    }
    Last edited by cerin; 12-07-2005 at 09:35 PM.
    My computer is awesome.

  7. #7
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    no I don't want it to continue while y or n are input. I want it to quit after one is input, but still loop if there is no input or invalid input.

    That is fine, and you can do it without the return statements. That just ends your program, it will hardly be useful to only figure out control statements that works by ending the program.

    Think about it:
    Code:
    while(holla!='y'||holla!='n')
    When you type in any other char both of those will be true and loop.
    But if it is one, the other will always have to be true.
    If it is y then not n has to be true.
    If it is n then not y has to be true.


    Slys code does exactly what you want, loops until a y or n is entered and stops then.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Quote Originally Posted by Enahs
    But if it is one, the other will always have to be true.
    If it is y then not n has to be true.
    If it is n then not y has to be true.
    Wrong, I am afraid the ||(OR) operator constitutes that either condition can be true and it will accept.

    Edit: OR I am not quite sure I understand you, but I don't need operators explained to me.
    Last edited by cerin; 12-08-2005 at 06:21 PM.
    My computer is awesome.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Heeeeyyyy.... jack ass.

    Code:
    while(holla != 'y' || holla != 'n')
    Why don't you spend 2 seconds actually thinking about this. Loop while a variable doesn't equal 'y' or doesn't equal 'n'... Do you know some special thing that I don't that would make a variable equal 'y' and 'n' so this loop will exit? THIS WILL ALWAYS RETURN TRUE. I gave you the answer yesterday. It's been there for you to take but you continue to argue about nothing.

    ...it seems you did need operators explained to you.
    Sent from my iPad

  10. #10
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Wrong, I am afraid the ||(OR) operator constitutes that either condition can be true and it will accept.
    Ding ding ding

    OR I am not quite sure I understand you, but I don't need operators explained to me.
    Yes you do.
    You have a character, one single character.

    If it is NOT y the loop will continue OR if it is not n the loop will continue.
    Please tell me how it is possible to make one character to be both y and n. That way it will evaluate false for not y and not n and stop the loop.

    Again, Slys code does what you stated you want it to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 02:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 03:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM

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