*shakes head* I need help

This is a discussion on *shakes head* I need help within the C++ Programming forums, part of the General Programming Boards category; I'm currently working on a code to use imbedded if functions.... Its kicking my butt now, I lost about an ...

  1. #1
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32

    Question *shakes head* I need help

    I'm currently working on a code to use imbedded if functions....
    Its kicking my butt now, I lost about an hours worth of tweaking and now I'm... bumbling around like an idiot. Let me show you my errors and then We'll move on to my mess of a code.

    In function 'Int main()'
    expected ) before ! token --- line 160

    [ C:\WINDOWS\Desktop\MUD\Dev-Cpp\G__~1.EXE If Challenge Help.o: No such file or directory.

    C:\WINDOWS\Desktop\MUD\Dev-Cpp\G__~1.EXE no input files ] x2

    Code:
    #include <iostream>
     
    using namespace std;
     
    int main()
    {
    	for (int i = 0; i < 10; i++ )
    	{
            	int books;
            	int input;
                
    	        cout<<"How many books have you read this last year?\n";
    	        cin>> books;
    	        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;
                       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";
                            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;\
                 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();             
    		}       
                          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;
                 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();             
    		} 
                          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;
                 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();             
    		}
                          cin.get();  
    		}
    		else if ( books >= 1 )
    		{
                 cout<<"Listed below are your options:\n";
                 cout<<" e : A peice of Candy!\n";
                 cin>> input;
                 if ( input == 'e' )
                       {
                          cout<<"Here is some candy, maybe it will boost your brain power!\n";
                          cin.get();     
                          }
                          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 && books !> 0 || books < 0 )
            {
                 cout<<"Error. Please enter a positive integer.\n";
           	     cin.get();
    	}
     
    	cin.get();
    I know its a mess, Ever since I lost my work, My brain has been .. jumbled and I can't figure out how I had it worked out
    The Pristine Angel lives on!

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Your error is here:

    Code:
    else if ( books != 0 && books !> 0 || books < 0 )
    There is no such thing as a "not greater than" operator.

    Well there is, but it's called a "less than or equal to" operator ( <= )
    Sent from my iPad®

  3. #3
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    Update:
    1. When I enter a b c d or e to choose my prize, it gives me... I think its the error message, but cin.get(); isn't registering properly or something because its not pausing for me to see it, or looping... but I'm going to try to switch to a while loop assuming it works out

    here it is currently:
    Code:
    #include <iostream>
     
    using namespace std;
     
    int main()
    {
    	for (int i = 0; i < 10; i++ )
    	{
            	int books;
            	int input;
                
    	        cout<<"How many books have you read this last year?\n";
    	        cin>> books;
    	        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;
                       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";
                            cin.get();             
    		}
    		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();             
    		} 
            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();             
    		}
    		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();             
    		}
    		cin.get();
    		}
    		else if ( books >= 1 )
    		{
                 cout<<"Listed below are your options:\n";
                 cout<<" e : A peice of Candy!\n";
                 cin>> input;
                 if ( input == 'e' )
                       {
                          cout<<"Here is some candy, maybe it will boost your brain power!\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 && books != 1-320000 || books < 0 )
            {
                 cout<<"Error. Please enter a positive integer.\n";
           	     cin.get();
    }
     cin.get();
     return 0;
    }
    }
    Thanks
    The Pristine Angel lives on!

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You might also want to look into the use of a switch statement: it will obviate the need for long if conditionals like this:

    if ( input != 'b' || input != 'c' || input != 'd' || input != 'e' )

  5. #5
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    Its not the lesson I'm on, It was a challenge to do it using the if statement, imbeding if statements inside if statements... I was trying to avoid getting ahead of myself and going into switch statements
    The Pristine Angel lives on!

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    There are other ways to shorten that code though. Try to remove things that you appear to do in every condition and do those globally outside of the statements.
    Sent from my iPad®

  7. #7
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    I don't want to shorten it, Just make it work *kicks at the prog*
    The Pristine Angel lives on!

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    If you haven't taken a break on it since I assigned it to you, then you should do so now.

    Sometimes time away from code helps you gather your thoughts and write better when you get back to it.
    Sent from my iPad®

  9. #9
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    ok, good Idea, I'll write a few easy progs and build on things with stuff I know that works... Get my mind off of it.... For some reason I just know that I'm missing something blatantly obvious... or some miniscule detail that shouldn't have been there in the first place... I implemented a while loop...
    The Pristine Angel lives on!

  10. #10
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Ok, first off, your if statements.

    You have if greater than 90, and your second if is greater than 75
    Well, 91 is greater than both 75 and 90 (and every other if) so it eval’s true for everything.
    Range it

    If >= 90

    If >=75 && < 90

    If >= 50 && < 75

    Ect…

    Code:
    else if ( books != 0 && books != 1-320000 || books < 0 )
    Ok, that might make sense to you. But what are you trying to do, run something when it is less than but not 0.

    If (books < 0)
    That is it man!

    Now, onto the problem in your code!
    Formatting and be consistent!
    And remember how I told you I like to put my {} on lines all by them selves?
    This is your problem that is keeping you from compiling (the others will just give you errors in your output). You are missing one simple little “}”.

    With consistence coding format, putting {} on separate lines (and indenting the closing the same as the opening) it would be really easy to find, how you have it, it is not so easy.

    You never close your first if loop. Which means when you think you are closing your for-loop you are actually just closing your first if!

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by Enahs
    If >= 90

    If >=75 && < 90

    If >= 50 && < 75
    Think about your logic here.

    If a statement doesn't evaluate true on the if >= 90 statement, and it evaluates true on the >= 75 statement, then that would automatically assume that it's less than 90. Two bounds are a waste unless for readability purposes.
    Sent from my iPad®

  12. #12
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    First off, The definition of else is if the previous if isn't true, therefore the && declaration thingies isn't neccesary. Next I was trying to make it if they user input something other than a number, they would still receive the message. Lastly.. Ya I do need to work on my coding.. Let me throw up my current code though instead of that.. I changed loop types
    here it is:
    Code:
    #include <iostream>
     
    using namespace std;
     
    int main()
    {
            	int books;
            	int input;
            	int i;
            	while ( i != 32000 )
            	{
                      cout<< i <<endl;
                      i++;
                      
                
    	        cout<<"How many books have you read this last year?\n";
    	        cin>> books;
    	        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;
                       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";
                            cin.get();             
    		}
    		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();             
    		} 
            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();             
    		}
    		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();             
    		}
    		cin.get();
    		}
    		else if ( books >= 1 )
    		{
                 cout<<"Listed below are your options:\n";
                 cout<<" e : A peice of Candy!\n";
                 cin>> input;
                 if ( input == 'e' )
                       {
                          cout<<"Here is some candy, maybe it will boost your brain power!\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 && books != 1-320000 || books < 0 )
            {
                 cout<<"Error. Please enter a positive integer.\n";
           	     cin.get();
    }
                 cout<<"Input any key to continue, type quit to close out.\n";
                 cin>> i;
                 cin.ignore ();
                 cin.get();
                 return 0;
    }
    }
    Ok, let me know where my logic is wrong, thanks
    The Pristine Angel lives on!

  13. #13
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Because 91 is greater than both 90 and 75

    Code:
    #include<iostream>
    
    
    using namespace std;
    
    int main()
    {
     
        const int test = 10;
    
    
    	if ( test >= 5) cout << "Greater than 5\n";
    
    	if (test >= 1 ) cout << "Greater than 1\n";
    
    
    return 0;
    }
    Will output both Greater than 5 and Greater than 1 because both are true.

    *edit*
    Though, if I bothered to learn to read in 1st grade I would have noticed he had else-if statements and not just if statements....so anyway....

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Why don't you look at his code again. He uses else if.

    *edit* Just read your *edit* so ummm... well this is ackward.
    Sent from my iPad®

  15. #15
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Ok, let me know where my logic is wrong, thanks
    Your return 0; is in your loop and not at the end of main.
    Also, you use i in your while loop, but you do not initialize it to anything. So the first time around ‘i’ has nothing in it or garbage.
    Try int i = 0;

    *Edit*, also you are going to have trouble because you are inputing char's into a type of int.
    Make that
    Code:
     char input;
    And except for that list else-if wacky statement it should work.
    Last edited by Enahs; 12-01-2005 at 10:00 PM.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

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