Click to See Complete Forum and Search --> : C++ Newbie - "char matching"
Samppa-vaa
February 5th, 2005, 01:25 PM
Right, I'm new to C++, and I've been having trouble with the following code.
A dumbed down version (especially the if bracket):
#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. :/
doy2001
February 5th, 2005, 01:43 PM
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.
#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;
}
} :)
YourSurrogateGod
February 5th, 2005, 01:51 PM
One suggestion, instead of using "#include <stdlib.h>", use "#include <cstdlib>".
Samppa-vaa
February 5th, 2005, 02:10 PM
Ah, thanks for that doy :)
Samppa-vaa
February 6th, 2005, 04:17 AM
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.
#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?
Andreas Masur
February 6th, 2005, 04:43 AM
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...
#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
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;
}
Andreas Masur
February 6th, 2005, 04:54 AM
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...
#include <iostream>
#include <fstream>
#include <string>
int main(int argc, char* argv[])
{
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;
}
Samppa-vaa
February 7th, 2005, 09:10 AM
Right-y-o, looks like I have some code to play around with for the next few days.. Thanks guys :)
Samppa-vaa
February 24th, 2005, 12:59 PM
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?
cilu
February 24th, 2005, 01:39 PM
I noticed that you include <string.h>. Make sure you include <string>.
wien
February 24th, 2005, 02:39 PM
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: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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.