Using 'if' with char arrays or string objects

This is a discussion on Using 'if' with char arrays or string objects within the C++ Programming forums, part of the General Programming Boards category; How do you use the if statement with char arrays or a string object? Here is the code I have ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    48

    Using 'if' with char arrays or string objects

    How do you use the if statement with char arrays or a string object? Here is the code I have now but it doesnt' seem to work and I think I've heard somewhere that you can't use 'if' with anything more than one character. If not, then what do you use?

    Code:
    	if(hang.spaces==hang.word)
    	{
    		MessageBox(NULL, "You Won!", hang.word, MB_OK);
    		hang.loopvar=false;
    	}
    Other than that, my hangman "engine" is pretty much done and when I finish learning Win32 API I'm going to port it to that. Thx 4 all your help, and this is probably my last question... I hope
    Hey, you gotta start somewhere

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    if(strcmp(String1, String2) == 0)
    {
       cout << "The strings are equal!";
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    The question shouldn't be if you can use if, rather, can you use == to compare strings.

    You can't use == to compare char*, (well, you can, but it's going to test for identity rather than equality, not something you want). However, you can use strcmp from <cstring>, it returns 0 if two strings are the same. That said, I'd recommenend learning how to use the

    Code:
    #include <cstring>
    #include <iostream>
    using namespace std;
    
    int main() {
        char s[] = "hello";
        if (strcmp(s, "hello")==0)  cout << s << " equals " << hello << endl;
        else cout << "not equal" << endl;
        return 0;
    }
    That said, using the string class is so much easier and less error prone than char* for strings, as it supports the operators you are used to for comparasion and assignment from the native types.

    Code:
    #include <string>   // notice, no c
    #include <iostream>
    using namespace std;
    
    int main() {
        string s = "hello";
        if (s == "hello")  cout << s << " equals hello" << endl;
        else cout << "not equal" << endl;
        return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    48
    It won't let me use 'string' in the clss
    Hey, you gotta start somewhere

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    591

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Why not...

    Originally posted by c++_n00b
    It won't let me use 'string' in the clss

    Why not ..
    Each of the codes that these guys wrote is great...
    Why you can't use strings? Can you tell us...
    C++
    The best

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    48
    yeah, actually i can tell you. It gives me about 26 or so errors . Is that a pretty good reason? It still isn't working right for me though and btw I'm using Visual C++ 6.0.

    For example, when I make my class like this:
    Code:
    extern struct hangman
    {
    	bool loopvar;
    	string word;
    	int length;
    	char letter;
    	char* spaces;
    
    	hangman();
    	~hangman();
    
    	void LoopProgram();
    	void PickWord();
    	void BlankSpaces();
    	void Guess();
    	void Check();
    }hang;
    I get this
    Code:
    --------------------Configuration: Hangman - Win32 Debug--------------------
    Compiling...
    Hangman.cpp
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(4) : error C2146: syntax error : missing ';' before identifier 'word'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(4) : error C2501: 'string' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(4) : error C2501: 'word' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(15) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(50) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(53) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(56) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(59) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(62) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(65) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(68) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(71) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(74) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(77) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(80) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(83) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(86) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(89) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(92) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(95) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(126) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(133) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.cpp(135) : error C2039: 'word' : is not a member of 'hangman'
            c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(2) : see declaration of 'hangman'
    Main.cpp
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(4) : error C2146: syntax error : missing ';' before identifier 'word'
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(4) : error C2501: 'string' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\myprojects\hangman\hangman.h(4) : error C2501: 'word' : missing storage-class or type specifiers
    Error executing cl.exe.
    
    Hangman.exe - 26 error(s), 0 warning(s)
    I hope this can tell you why I can't use 'string' in my class NANO. Thx 4 all those who have tried 2 help though I appreciate it.
    Last edited by c++_n00b; 06-05-2002 at 05:20 PM.
    Hey, you gotta start somewhere

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    317
    ok try :

    Code:
    #include<string.h>
    That will let you use string

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    48
    um... that won't work in a header file. Thx anywayz though

    ... *sigh*...
    Hey, you gotta start somewhere

  10. #10
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Again. WHY won't that work in a header file? Is your compiler not letting you import files? What?

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    317
    ok, why exactly the extern declaration? Is this struct being defined in a seperate file from your main file? If so is the string header associated with that file?

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    48
    well, before I was having problems linking the two files (hangman.h and hangman.cpp) so someone told me to use 'extern' and then define 'struct hangman hang;' in the hangman.cpp file. And yes, <string.h> is included in hangman.cpp. So what am i doing wrong?
    Hey, you gotta start somewhere

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    48

    here's the complete code

    here's the complete code so you guys don't have to wonder what i have in it anymore

    main.cpp
    Code:
    #include <iostream>
    #include <windows.h>
    #include <string.h>
    #include "Hangman.h"
    
    using namespace std;
    
    int main()
    {
    	cout<<"|-| /-\\ |\\| G |\\/| /-\\ |\\|"<<endl;
    
    	hang.LoopProgram();
    
    	return 0;
    }
    hangman.cpp
    Code:
    #include <iostream.h>
    #include <string.h>
    #include <windows.h>
    #include "Hangman.h"
    
    struct hangman hang;
    
    
    //Hangman Constructor
    hangman::hangman()
    {
    	hang.loopvar=true;
    
    	hang.PickWord();
    	hang.length=strlen(hang.word);
    
    	hang.spaces = new char[ hang.length ];
    	for(short x=0; x<hang.length; x++)
    	{
    		spaces[x]='_';
    	}
    }
    
    //Hangman Destructor
    hangman::~hangman()
    {
    	delete[] spaces;
    }
    
    //Function that loops the program until told to stop
    void hangman::LoopProgram()
    {
    	while(hang.loopvar!=false)
    	{
    		hang.BlankSpaces();
    		hang.Guess();
    		hang.Check();
    	}
    }
    
    //Function that picks a random word
    void hangman::PickWord()
    {
    	srand(GetTickCount());
    	short random=rand()%15;
    	
    	switch(random)
    	{
    	case 0:
    		strcpy(hang.word, "corndog");
    		break;
    	case 1:
    		strcpy(hang.word, "computer");
    		break;
    	case 2:
    		strcpy(hang.word, "telephone");
    		break;
    	case 3:
    		strcpy(hang.word, "electric");
    		break;
    	case 4:
    		strcpy(hang.word, "stereo");
    		break;
    	case 5:
    		strcpy(hang.word, "oreo");
    		break;
    	case 6:
    		strcpy(hang.word, "microphone");
    		break;
    	case 7:
    		strcpy(hang.word, "cat");
    		break;
    	case 8:
    		strcpy(hang.word, "moose");
    		break;
    	case 9:
    		strcpy(hang.word, "internet");
    		break;
    	case 10:
    		strcpy(hang.word, "cow");
    		break;
    	case 11:
    		strcpy(hang.word, "potato");
    		break;
    	case 12:
    		strcpy(hang.word, "card");
    		break;
    	case 13:
    		strcpy(hang.word, "oven");
    		break;
    	case 14:
    		strcpy(hang.word, "speakers");
    		break;
    	default:
    		strcpy(hang.word, "earthquake");
    	}
    }
    
    //Function that draws all of the spaces
    void hangman::BlankSpaces()
    {
    	for(short x=0; x<hang.length; x++)
    	{
    		system("cls");
    		cout<<spaces[x]<<" ";
    	}
    
    }
    
    //Function that gets the user's guess
    void hangman::Guess()
    {
    	cout<<endl<<"What's your guess?: ";
    	cin>>hang.letter;
    }
    
    //Function that checks to see if the letter is in the word, if the word is complete, or the... man is hanged 
    //or however you want to put it. Also adds another number to the NumGuesses variable.
    
    void hangman::Check()
    
    {
    	//Check to see if letter guess is in word
    	for(short x=0;x<hang.length;x++)
    	{
    		if(hang.word[x]==hang.letter)
    		{
    			hang.spaces[x]=hang.letter;
    		}
    	}
    
    	//Check to see if the word is complete
    	if(strcmp(hang.spaces,hang.word)==0)
    	{
    		MessageBox(NULL, "You Won!", hang.word, MB_OK);
    		hang.loopvar=false;
    	}
    }
    hangman.h
    Code:
    extern struct hangman
    {
    	bool loopvar;
    	char word[10];
    	int length;
    	char letter;
    	char* spaces;
    
    	hangman();
    	~hangman();
    
    	void LoopProgram();
    	void PickWord();
    	void BlankSpaces();
    	void Guess();
    	void Check();
    }hang;
    Hope this helps... oh wait, I'm the one who needs help here. NM...
    Hey, you gotta start somewhere

  14. #14
    Registered User
    Join Date
    May 2002
    Posts
    317
    I'm sorry, I really don't know. I copied the entire prog you posted into all the seperate files you specified and it compiled fine for me.
    Are you sure you have all your include files?

  15. #15
    Registered User
    Join Date
    Apr 2002
    Posts
    48
    no, the code I posted does compile fine. It's jsut that when I add <string.h> and change char word[10] to string word it gives me all of those errors.
    Hey, you gotta start somewhere

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

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  2. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  3. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM

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