CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2005
    Posts
    9

    C++ Newbie - "char matching"

    Right, I'm new to C++, and I've been having trouble with the following code.
    A dumbed down version (especially the if bracket):

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        char name1[256], name2[256];
    
        cout << "What is your name? "; gets(name1);
    
        cout << "\nWelcome "<< name1 <<endl;
        cin.get();
    
        cout << "\nPlease insert your name: ";
        cin.getline ( name2, 256 );
        cin.get();
        
        if ( name1 == name2  ) {
        cout << "\nHi!";
        cin.get();
        return 0;
    }
        else {
        cout << "\nBye.";
        cin.get();
        return 0;
    }
    The part I'm having trouble with is the if bracket. How would I code that if name1 matches name2 it'd say "Hi", and if they didn't match, it'd say "Bye". I've been having trouble with that for the last couple of days. :/

  2. #2
    Join Date
    Jul 2004
    Location
    Caracas, Venezuela
    Posts
    32

    Re: C++ Newbie - "char matching"

    Use the strcmp function. Here´s a sample. Hope this helps.

    Edit: Almost forgot, when you use == to compare strings or chars what it does is it compares the location of the variables on the memory, or something like that at least that's what happens in Java. The strcmp function returns 0 is the strings are the same.


    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string.h>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        char name1[256], name2[256];
    
        cout << "What is your name? "; 
        cin >>name1;
    
        cout << "\nWelcome "<< name1 <<endl;
    
        cout << "\nPlease insert your name: ";
        cin >>name2;
        cin.get();
        
        if (strcmp(name1,name2) == 0) {
        cout << "\nHi!";
        system("pause");
        return 0;
    }
        else {
        cout << "\nBye.";
        system("pause");
        return 0;
    }
    }
    Last edited by doy2001; February 5th, 2005 at 02:46 PM.

  3. #3
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238

    Re: C++ Newbie - "char matching"

    One suggestion, instead of using "#include <stdlib.h>", use "#include <cstdlib>".
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  4. #4
    Join Date
    Feb 2005
    Posts
    9

    Re: C++ Newbie - "char matching"

    Ah, thanks for that doy

  5. #5
    Join Date
    Feb 2005
    Posts
    9

    Re: C++ Newbie - "char matching"

    btw, now that I'm here, might as well ask about the file system..
    Below, I've got the code at the moment. I've bolded the part that I don't know how to code.

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <fstream>
    #include <string.h>
    
    using namespace std;
    
    #pragma hdrstop
    #pragma argsused
    
    int main(int argc, char* argv[])
    {
        
        char Name[256], File[256];
    
        FILE *Test = fopen("test.txt", "a+");
    
        cout << "Name: ";
        cin >> Name;
        cout << "File: ";
        cin >> File;
    	
        cin.get();
    	
        fprintf(Test, "%s\n", Name);
        fprintf(Test, "%s\n", File);
        
        fclose(Test);
        
        FILE *OtherFile = fopen("<< File <<endl;.txt", "w");
        fprintf(OtherFile, "%s\n", Name);
        
        fclose(OtherFile);
        return 0;
    }
    As you see, I'd like the person to be able to name a file in which his name is inserted into. Before that though, both parts will be inserted into test.txt as well. I've thought about using a database but it's just a teeny bit too complicated atm.. Any ideas?
    Last edited by Samppa-vaa; February 6th, 2005 at 05:42 AM.

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: C++ Newbie - "char matching"

    Quote Originally Posted by Samppa-vaa
    The part I'm having trouble with is the if bracket. How would I code that if name1 matches name2 it'd say "Hi", and if they didn't match, it'd say "Bye". I've been having trouble with that for the last couple of days. :/
    Well...the problerm is simply that old-style character arrays cannot be compared directly using the '==' operator. You would need to use a comparison function such as 'strcmp()'. However, this is basically not necessary...change the character arrays to the standard string class...
    Code:
    #include <iostream>
    #include <string>
    
    int main(int argc, char* argv&#091;&#093;)
    {
      std::string name1;
      std::string name2;
    
      std::cout << "What is your name? ";
      std::cin >> name1;
    
      std::cout << "\nWelcome "<< name1 << std::endl;
      std::cin.get();
    
      std::cout << "\nPlease insert your name: ";
      std::cin >> name2;
        
      if(name1 == name2)
        std::cout << "\nHi!";
      else
        std::cout << "\nBye.";
    
      cin.get();
      return 0;
    }

  7. #7
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: C++ Newbie - "char matching"

    Quote Originally Posted by Samppa-vaa
    btw, now that I'm here, might as well ask about the file system..
    Below, I've got the code at the moment. I've bolded the part that I don't know how to code.
    Again....use the C++ features...
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    int main(int argc, char* argv&#091;&#093;)
    {
      std::string Name;
      std::string File;   
    
      std::ofstream Test("test.txt", std::ios::out | std::ios::app);
      if(Test)
      {
        std::cout << "Name: ";
        std::cin >> Name;
        std::cout << "File: ";
        std::cin >> File;
    
        Test << Name << "\n" << File << "\n";
        Test.close();
    
        std::ofstream OtherFile((File + ".txt").c_str());
        if(OtherFile)
        {
          OtherFile << Name << "\n",
          OtherFile.close();
        }
        else
          std::cerr << "Could not open other file);
      }
      else
        std::cerr << "Could not open test file);
    
      return 0;
    }

  8. #8
    Join Date
    Feb 2005
    Posts
    9

    Re: C++ Newbie - "char matching"

    Right-y-o, looks like I have some code to play around with for the next few days.. Thanks guys

  9. #9
    Join Date
    Feb 2005
    Posts
    9

    Re: C++ Newbie - "char matching"

    Hmmm... So how would I get a file to open for reading when the name is input. I mean, when I write "File", the file "File.txt" or whatever would be opened for reading. It's been doing my head in for a week, and the net has nothing on it. Anyone have an idea?

  10. #10
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: C++ Newbie - "char matching"

    I noticed that you include <string.h>. Make sure you include <string>.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  11. #11
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: C++ Newbie - "char matching"

    Quote Originally Posted by Samppa-vaa
    Hmmm... So how would I get a file to open for reading when the name is input. I mean, when I write "File", the file "File.txt" or whatever would be opened for reading. It's been doing my head in for a week, and the net has nothing on it. Anyone have an idea?
    Something like this:
    Code:
    std::string filename;
    std::cin >> filename;
    filename += ".txt"
    
    std::ifstream file(filename.c_str());
    It would make more sense to have the user enter the extention as well though, since that is usually what you do when an application asks you for a filename.
    Insert entertaining phrase here

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured