Thread: Can someone help me see why this simple program is NOT working please?

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    118

    Can someone help me see why this simple program is NOT working please?

    Here is my program, but I dont get why it doesent work

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char code[6] = "abcde";
    	char icode[6];
    	char letter;
    	char opcion;
    
    	bool playagain = false;
    
    	int i = 0;
    
    	do
    	{
    		cout << "Try to guess a code I have thought of:\n";
    
    		for (i = 0; i < 5; i++)
    		{
    			cin >> letter;
    
    			icode[i] = letter;
    		}
    	} while (icode != "abcde");
    
    	return 0;
    }
    Once this works I will make it generate a random code of course.

  2. #2
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    why don't you just do:
    Code:
    cin >> icode;
    you can always input a string as a whole.
    and then you do comparing like this:
    Code:
    strcmp(code, icode); //you should include <cstring>
    and that should make it work fine.
    none...

  3. #3
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    It might be because code is actually 'a','b','c','d','e','\0' (null terminated) and when you check for icode, its 'a','b','c','d','e' (without the null terminator) and therefore it doesn't think it's the same. Try including the code in single commas so it doesn't have null termination.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Thanks alot
    But I did it like that because in the future I'm going to do something like this
    Code:
    if (icode[i] == code[0])
    {
        correct++; 
    }
    And like that to tell the user how many correct letters he got.
    Why drink and drive when you can smoke and fly?

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Thanks everyone, I finally made ir work, here it is if anyone cares
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int main()
    {
    	char code[6] = "abcde";
    	char icode[6];
    	char letter;
    	char opcion;
    
    	bool playagain = false;
    
    	int i = 0;
    
    	do
    	{
    		icode[5] = '\0';
    		cout << "Try to guess a code I have thought of:\n";
    
    		for (i = 0; i < 5; i++)
    		{
    			cin >> letter;
    
    			icode[i] = letter;
    		}
    	} while (strcmp(code, icode));
    
    	return 0;
    }
    Thanks again everyone
    Why drink and drive when you can smoke and fly?

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Ok, Ok I have made it much better, only problem is, I cant think of a way to make a random code that has only the letters "abcde" and that does not repeat them, Any ideas?
    BTW this what I have now:

    Code:
    #include <iostream>
    #include <cstring>
    #include <ctime>
    using namespace std;
    
    int main()
    {
    
    	char code[6] = "abcde";
    	char icode[6];
    	char letter;
    	char opcion;
    
    	bool playagain = false;
    
    	int i = 0;
    	int correct = 0;
    
    	do
    	{
    		do
    		{
    			icode[5] = '\0';
    			cout << "Try to guess a code I have thought of:\n";
    
    			for (i = 0; i < 5; i++)
    			{
    				cin >> letter;
    
    				icode[i] = letter;
    
    				if (icode[i] == code[i])
    				{
    					correct++;
    				}
    			}
    			cout << "You had " << correct << " correct letters!\n";
    
    			if (!strcmp(code, icode))
    			{
    				cout << "You guessed the code!\n";
    			}
    			correct = 0;
    
    		} while (strcmp(code, icode));
    
    		cout << "Do you want to play again? (y/n)\n";
    		cin >> opcion;
    		playagain = (opcion == 'y' ? true : false);
    
    	} while (playagain);
    
    	return 0;
    }
    Yes I know its a horrible code, but I make functions and all that to make it look better latter...
    Why drink and drive when you can smoke and fly?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  2. Replies: 10
    Last Post: 09-07-2008, 11:38 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM
  5. unable to get simple program working
    By toom in forum Linux Programming
    Replies: 1
    Last Post: 10-11-2003, 05:05 AM