|
-
February 5th, 2005, 02:25 PM
#1
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. :/
-
February 5th, 2005, 02:43 PM
#2
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.
-
February 5th, 2005, 02:51 PM
#3
Re: C++ Newbie - "char matching"
One suggestion, instead of using "#include <stdlib.h>", use "#include <cstdlib>".
-
February 5th, 2005, 03:10 PM
#4
Re: C++ Newbie - "char matching"
Ah, thanks for that doy
-
February 6th, 2005, 05:17 AM
#5
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.
-
February 6th, 2005, 05:43 AM
#6
Re: C++ Newbie - "char matching"
 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[])
{
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;
}
-
February 6th, 2005, 05:54 AM
#7
Re: C++ Newbie - "char matching"
 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[])
{
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;
}
-
February 7th, 2005, 10:10 AM
#8
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
-
February 24th, 2005, 01:59 PM
#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?
-
February 24th, 2005, 02:39 PM
#10
Re: C++ Newbie - "char matching"
I noticed that you include <string.h>. Make sure you include <string>.
-
February 24th, 2005, 03:39 PM
#11
Re: C++ Newbie - "char matching"
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|