Thread: *shakes head* I need help

  1. #16
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    Code:
    if ( input == 'a' ) {
    }
    That means if input is the same as a(they typed in a) then do the following right?
    The Pristine Angel lives on!

  2. #17
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You should define input as a char though not an int. There is no implicit cast in those if statements so reading in a character to an integer fails the stream.
    Last edited by SlyMaelstrom; 12-01-2005 at 11:12 PM.
    Sent from my iPad®

  3. #18
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    define it as a char.. ok tutorials here I come, need to find out how to do that.. assumedly char input;
    The Pristine Angel lives on!

  4. #19
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes.
    Sent from my iPad®

  5. #20
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    WAHOO! Declaring it a char made all the difference... it also makes it possible for me to.. wow that helps alot, thanks!
    The Pristine Angel lives on!

  6. #21
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    Help with my while loop please, I obviously don't understand it.. I can't get it to start the program over for the life of me. at least not without making it just like the for loop in the sense that it goes to a certain number and then stops

    Code:
    #include <iostream>
     
    using namespace std;
     
    int main()
    {
            	int books;
            	char input;
            	char book;
            	int i = 0;
            	while ( i != 200 )
            	{
                      cout<< i <<endl;
                      i++;
                      
                
    	        cout<<"How many books have you read this last year?\n";
    	        cin>> books || book;
    	        cin.ignore();
    	        cout<<"You read "<< books <<"?\n";
     
    		if ( books >= 90 )
    		{
                 cout<<"Listed below are your options:\n";
                 cout<<" a : 42' Tv Set!\n";
                 cout<<" b : MP3 Player!\n";
                 cout<<" c : Sony Cd Player!\n";
                 cout<<" d : Book Subscription!\n";
                 cout<<" e : A peice of Candy!\n";
                 cin>> input;
                 cin.ignore();
                       if ( input == 'a' )
                       {
                            cout<<"Wow! Congratulations! You've won a 42' Tv Set!\n";
                            cin.get();
                            }
                       else if ( input == 'b' )
                       {
                          cout<<"Good Job! You've won an MP3 player!\n";
                          cin.get();
                          }
                       else if ( input == 'c' )
                       {
                            cout<<"The work paid off! You've won a Sony CD player!\n";
                            cin.get();
                            }
                       else if ( input == 'd' )
                       {
                            cout<<"This'll help you boost your score! Your very own National Book Club Subscription!\n";
                            cin.get();
                            }
                       else if ( input == 'e' )
                       {
                          cout<<"Here is some candy, maybe it will boost your brain power!\n";
                          cin.get();     
                          }
                       else if ( input != 'a' || input != 'b' || input != 'c' || input != 'd' || input != 'e' )
                       {
                            cout<<"Error. Please enter a, b, c, d, or e\n";             
    		}
    		cout<<"Hint: Press enter\n";
    		cin.get();
        }
    		else if ( books >= 75 )
    		{
                 cout<<"Listed below are your options:\n";
                 cout<<" b : MP3 Player!\n";
                 cout<<" c : Sony Cd Player!\n";
                 cout<<" d : Book Subscription!\n";
                 cout<<" e : A peice of Candy!\n";
                 cin>> input;
                 cin.ignore ();
                 if ( input == 'b' )
                       {
                          cout<<"Good Job! You've won an MP3 player!\n";
                          cin.get();
                          }
                       else if ( input == 'c' )
                       {
                            cout<<"The work paid off! You've won a Sony CD player!\n";
                            cin.get();
                            }
                       else if ( input == 'd' )
                       {
                            cout<<"This'll help you boost your score! Your very own National Book Club Subscription!\n";
                            cin.get();
                            }
                       else if ( input == 'e' )
                       {
                          cout<<"Here is some candy, maybe it will boost your brain power!\n";
                          cin.ignore();     
                          }
                           else if ( input != 'b' || input != 'c' || input != 'd' || input != 'e' )
                       {
                            cout<<"Error. Please enter a, b, c, d, or e\n";
                            cin.get();             
    		}
            cout<<"Hint: Press enter\n"; 
            cin.get();     
    		}
    		else if ( books >= 50 )
    		{
                 cout<<"Listed below are your options:\n";
                 cout<<" c : Sony Cd Player!\n";
                 cout<<" d : Book Subscription!\n";
                 cout<<" e : A peice of Candy!\n";
                 cin>> input;
                 cin.ignore();
                 if ( input == 'c' )
                       {
                            cout<<"The work paid off! You've won a Sony CD player!\n";
                            cin.get();
                            }
                       else if ( input == 'd' )
                       {
                            cout<<"This'll help you boost your score! Your very own National Book Club Subscription!\n";
                            cin.get();
                            }
                       else if ( input == 'e' )
                       {
                          cout<<"Here is some candy, maybe it will boost your brain power!\n";
                          cin.ignore();     
                          }
                       else if ( input != 'c' || input != 'd' || input != 'e' )
                       {
                            cout<<"Error. Please enter a, b, c, d, or e\n";
                              cin.get();           
    		}
    		cout<<"Hint: Press enter\n";
    		cin.get();
    		}
    		else if ( books >= 25 )
    		{
                 cout<<"Listed below are your options:\n";
                 cout<<" d : Book Subscription!\n";
                 cout<<" e : A peice of Candy!\n";
                 cin>> input;
                 cin.ignore();
                 if ( input == 'd' )
                       {
                            cout<<"This'll help you boost your score! Your very own National Book Club Subscription!\n";
                            cin.ignore();
                            }
                       else if ( input == 'e' )
                       {
                          cout<<"Here is some candy, maybe it will boost your brain power!\n";
                          cin.ignore();     
                          }
                       else if ( input != 'd' || input != 'e' )
                       {
                            cout<<"Error. Please enter a, b, c, d, or e\n";
                             cin.get();            
    		}
    		cout<<"Hint: Press enter\n";
    		cin.get();
    		}
    		else if ( books >= 1 )
    		{
                 cout<<"Listed below are your options:\n";
                 cout<<" e : A peice of Candy!\n";
                 cin>> input;
                 cin.ignore();
                 if ( input == 'e' )
                       {
                          cout<<"Here is some candy, maybe it will boost your brain power!\n";
                          cin.get();    
                          }
                          else if ( input != 'e' )
                          {
                               cout<<"Error. Please enter your only option next time, e\n";
                               cin.get();
    		}
    		else if ( books == 0 )
    		{
                 cout<<"Listed below are your options:\n";
    			cout<<"Sorry, Start reading and maybe you'll win something next time!\n";
    		}
            else if ( books < 0 )
            {
                 cout<<"Error. Please enter a positive integer.\n";
           	     cin.get();
                 }
    }
            else if ( book )
            {
                 cout<<"Oops! Please inter a positive integer!\n";
                 cin.get();
                 }
                 cout<<"Input 1 to continue, 200 to close out.\n";
                 cin>> i;
                 cin.ignore ();
                 return 0;
    }
    }
    The Pristine Angel lives on!

  7. #22
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Code:
            	while ( i != 200 )
            	{
                      cout<< i <<endl;
                      i++;

    Code:
                 return 0;
    }
    }
    Change the while statement to while(1), which means the while loop is always true, and therefor going to repeat. Remove the cout, and i variable. Then add the ability to quit the program somehow (case or if statement), and all you do there is break; the loop.

    Also move the return 0; down under the end of the while loop, so it isn't just getting to the end of the while loop and exiting. So it should be between the two curly braces.

    Code:
            	char choice = 'n';
            	while (1)
            	{
                      cout<< "quit?" <<endl;
                      cin >> choice;
                      if(choice = 'y')
                                        break;
    Code:
    }
                 return 0;
    }
    BTW continue; is useful too. It will start the loop over again from the top, from where ever its encountered.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  8. #23
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    Oh ok, thank you much
    The Pristine Angel lives on!

  9. #24
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You shouldn't make explicitly infinite loops and you shouldn't use the break keyword. Inao the idea behind this while loop is not to be count controlled, you should have it's condition be based on something in the progam and asking the user to input a seemingly random number at the end is not the way to do that.

    Don't worry about the loop for now, that was extra credit, remember.
    Sent from my iPad®

  10. #25
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    Code:
           else if ( book )
            {
                 cout<<"Oops! Please inter a positive integer!\n";
                 cin.get();
                 }
    Shouldn't this, since book is defined as a character, mean if they type any characters it will give this message?
    The Pristine Angel lives on!

  11. #26
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by SlyMaelstrom
    You shouldn't make explicitly infinite loops and you shouldn't use the break keyword. Inao the idea behind this while loop is not to be count controlled, you should have it's condition be based on something in the progam and asking the user to input a seemingly random number at the end is not the way to do that.

    Don't worry about the loop for now, that was extra credit, remember.
    As if anyone new to learning programming doesn't learn these techniques in order to grasp concepts and actually get somewhere? I'd love to have seen how many people would quit if easier techniques for learning things were abolished. I'm pretty sure I've seen infinite loops in most tutorials I've ever read. Rare cases that they are problems don't tend to occur until the programmer is making programs advanced enough to already know more about them. I believe they are fine for learning/testing purposes. I also remember seeing one of your payrolled bot programmers using one yesterday.

    Just base the while statement on the choice variable then. If its equal to 'y', then it will continue, otherwise it doesn't.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  12. #27
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You don't teach someone things that you know are bad. Even if you expect them to know better when they get experienced. Bad teaching leads to bad habits.

    It's like teaching a kid to throw something they broke under the sofa. Yes, they'll probably know that's bad when they're older, but it will lower their standards of the difference between what is good and what is bad.
    Sent from my iPad®

  13. #28
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    Alright, I'm tired and making more mistakes by the minute. I'm going to crash and work on it some more tomorrow (I'd hope to have this in tonight, if you want what I have just tell me, I'll wait for a response to this post before I go) Also, how long is it going to take for the chat server to be back up? is it now?
    The Pristine Angel lives on!

  14. #29
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by SlyMaelstrom
    You don't teach someone things that you know are bad. Even if you expect them to know better when they get experienced. Bad teaching leads to bad habits.
    I don't agree that while(1) is so bad that it cant be used while learning. Its like using namespace std. Its just easier, and anyone who could get to programs where while(1) actually becomes a problem should easily be able to stop the "habit". I don't know where people get that bad habits die hard when it comes to programming. I've never have a problem with using std::, initializing variables, declaring const, etc. when I program for real and not to test. Maybe thats me, or maybe I don't notice it. I believe the learning benefit is greater than the risk. You should be familar with bad code anyway, as it carries through languages in different forms. Thats just my opinion.

    However, I should have mentioned they are bad practice. Either or both of us should at least explain why they are bad. Infinite loops like this one could result in the program eating up computer memory. For example if you entered in something that was not accepted by cin, like if an int was expected, and a char was put used instead, that would result in an infinite loop. More: http://www.cis.nctu.edu.tw/chinese/d....html#faq-15.2 Maybe you use an infinite loop to recieve input from a file, but a problem occurs half way through and it doesn't reach the end like you expected, so you're stuck in the loop. Generally something unexpected might happen in your program and you might have no way of exiting the loop. Obviously. Sometimes they are necessary though, but definetly not in your case. In which case you should check for anything you suspect might result in a true infinite loop and prevent it. Probably even something to prevent too many loops for occurring too. Theres an even higher risk if someone else is using your code, or its a library of some sort for someone to use. Any time of infinite loop is suspicious, because you can't always know what will happen in order to prevent true infinite loops. I'm sure theres more too.

    So.. avoid infinite loops, and btw, dont use using namespace std. Its still my opinion that using an infinite loop for quick and easy learning isn't that bad. I used and learned about goto's, but don't use them anymore because I know of better ways.

    Edit: Before I go to bed, I've just decided I agree, whatever, people have bad habits. I don't find them hard to change, but whatever. I haven't been programming long enough to talk.

    On a side note, just found this o.0: http://www.cs.rice.edu/~ian/Manifest...C++isBad.shtml

    ¹ Java, while much better than C++, shares this same weakness: the smallest Java program requires about 12 keywords, each replete with meaning; a beginner must be told "put these words in your program in just this right order, else it won't work". I've seen many students needlessly frustrated because it takes 30min to figure out their non-working program resulted from only inscribing eleven of the dozen necessary arcane glyphs. They may understand conceptually exactly what they want to do, but the arbitrary details of excessive syntax take out all the interest. (Some studies suggest that the prevalent teaching mode — encouraging arbitrary tinkering with little direction or meaning just trying to get it to work — is one reason for the prounounced gender bias seen in the field of computer science.)
    Seriously.. they have their purposes. It doesn't mean the language is bad, it just means its not the right choice.
    Last edited by Dae; 12-02-2005 at 01:31 AM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  15. #30
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    can someone toss me a link on exceptions?
    The Pristine Angel lives on!

Popular pages Recent additions subscribe to a feed