Thread: Stickygoo... I'm stuck again

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

    Question Stickygoo... I'm stuck again

    just a little noob problem. I stopped learning C++ for a while but have just got back into it, so I thought I'd do a little program to check I'm ok with basic stuff.

    Anyway here it is, it's just a small maths game, but when I compile it (Borland Compiler) it says the variables 'guess' and 'answer' arn't used in the function main... When I'm pretty sure they are


    Well, here's the code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int answer = 1, guess = 1, x = 1;
    	
    	cout << "\n\n-----------------------------------\nSelect Difficulty\n1 - Easy\n2 - Medium\n3 - Hard\n4 - Quit\n\nChoice: ";
    	cin >> x;
    	
    		switch(x)
    		{
    			case 1:
    				answer = 20;
    				while(guess ! = answer)
    				{
    					cout << "(27 + 13) / 2 = ";
    					cin >> guess;
    					if(guess ! = answer)
    						cout << "Wrong!";
    				}
    				cout << "Correct!";
    				break;
    					
    			case 2:
    				answer = 170;
    				while(guess ! = answer)
    				{
    					cout << "(18 + 36) * 3 + 8 = ";
    					cin >> guess;
    					if(guess ! = answer)
    						cout << "Wrong!";
    				}
    				cout << "Correct!";
    				break;
    				
    			case 3:
    				answer = 360;
    				while(guess ! = answer)
    				{
    					cout << "(84 + 23 - 7) * 3.5 + (36 / 6 + 4) = ";
    					cin >> guess;
    					if(guess ! = answer)
    						cout << "Wrong!";
    				}
    				cout << "Correct!";
    				break;
    			
    			case 4:
    				cout << "End of program.";
    				break;
    				
    			default:
    				cout << "Error: That is not an option";
    		}
    }

    Thanks if anyone can show me where I'm going wrong

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    i get 27 errors when i compile - the problem being the space
    between the ! and the = in you conditions - delete all them so
    the read "!=" as opposed to "! =" and it should be fine
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    20
    You have to replace "! =" with !=.

    There is a space in between those characters.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    20
    !!!

    Of course!

    Sorry for any time wasted, but thanks for your help...


    I really should have seen that


    Thanks again!

    ------------------

    EDIT::

    Another problem ... I wanted to make it so my program would loop, until the user selected option 4, so I added:

    Code:
    int main()
    {
    	int answer, guess = 1, x = 1, looper;
    	
    	looper = 1;
    	while(looper = 1)
    	{
    		cout << "\n\n-----------------------------------\nSelect Difficulty\n1 - Easy\n2 - Medium\n3 - Hard\n4 - Quit\n\nChoice: ";
    		cin >> x;
    To the script.

    And to case 4 I added

    Code:
    case 4:
    						cout << "End of program.";
    						looper = 2;
    						break;
    But I'm getting the error that looper is assigned a value that is used in function main (line 60) and there is possibly an incorrect assignment (line 8). But I'm pretty sure It's all correct...

    Anyone know what's wrong?
    Thanks in advance!
    Last edited by StickyGoo; 05-25-2006 at 05:01 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Code:
    while(looper = 1)
    Do you see anything strange about what you wrote?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    20
    I hope it's a good sign if I dont ...

    The only thing I really see wrong with it is that there's no space between the while and (looper = 1)...But I dont think that matters too much.

    EDIT!!!

    Yay!!!

    Thanks laserlight... it was the "=" to "=="



    Big thanks!

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    12
    Tip: in equality checking expretions put the value (if any) on the left.
    10 == var because on error typing '==' like 10 = var -> ERROR! lvalue required.... but var = 10 is a legal assigment expression

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Tip: in equality checking expretions put the value (if any) on the left.
    10 == var because on error typing '==' like 10 = var -> ERROR! lvalue required.... but var = 10 is a legal assigment expression
    Debatable, since if you consciously remember to make the change, you will consciously remember not to use the wrong operator. If you rely on this technique to spot mistakes, you may shoot yourself in the foot when both expressions are lvalues. Some people find (var == 10) more natural than (10 == var). Oh, and modern compilers may just warn you about it anyway.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM