View previous topic :: View next topic |
Author |
Message |
Pseudo Xero I post too much
Reputation: 0
Joined: 16 Feb 2007 Posts: 2607
|
Posted: Wed Aug 22, 2007 1:23 am Post subject: C++ Question. |
|
|
Blegh, started C++ not too long ago.
Only problem is I don't know how to use a if command to see if the user input == "somestring".
I already know how to use the if command in general, but I get compiling errors if I use if (inputvariable == "something") {.
It might be something simple... but i couldn't find anything about it on google.
Thanks...
By the way, I'm using Visual C++ 6.
_________________
haxory' wrote: | can't VB do anything??
windows is programmed using VB right? correct me if im wrong.
so all things in windows you have like the start menu is a windows form too. |
|
|
Back to top |
|
 |
Robotex Master Cheater
Reputation: 0
Joined: 05 Sep 2006 Posts: 378 Location: The pizza country!
|
Posted: Wed Aug 22, 2007 3:04 am Post subject: |
|
|
if(!strcmp(a1,a2))
_________________
ASM/C++ Coder
Project Speranza lead developer |
|
Back to top |
|
 |
Pseudo Xero I post too much
Reputation: 0
Joined: 16 Feb 2007 Posts: 2607
|
Posted: Wed Aug 22, 2007 3:50 am Post subject: |
|
|
Robotex wrote: | if(!strcmp(a1,a2)) |
Thanks.
eh, I don't really get strcmp...
I haven't noticed any bad habits, but might be because of a previous language I learned.
Here's the code I have, obviously there's compiling errors...
Code: | #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main ()
{
char *a1 = Input
char *a2 = "k\o!}RmR=944QuzpFd~@`{fDF7n3g{A-"
int Input
cout << "Password: ";
cin >> Input;
if (!strcmp(a1, a2)) {
cout << "Correct password...\n";
else
cout << "Wrong password, try again...\n";
}
return 0;
} |
Anyone mind putting me in the right direction?
_________________
haxory' wrote: | can't VB do anything??
windows is programmed using VB right? correct me if im wrong.
so all things in windows you have like the start menu is a windows form too. |
|
|
Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Wed Aug 22, 2007 4:07 am Post subject: |
|
|
Using std::string allows you to do "if" compares.
Code: | #include <iostream>
#include <string>
int main(int argc, char* argv[])
{
std::string input;
std::string compare("asdasd");
std::getline(std::cin, input);
if(input==compare)
std::cout << "Hurray!" << std::endl;
else
std::cout << "Blah." << std::endl;
return EXIT_SUCCESS;
} |
|
|
Back to top |
|
 |
TheSorc3r3r I post too much
Reputation: 0
Joined: 06 Sep 2006 Posts: 2404
|
Posted: Wed Aug 22, 2007 7:56 am Post subject: |
|
|
you don't need to use std::string to compare strings.
xenephobe, you're forgetting semicolons, you can't declare a string without "".
Code: | #include <stdlib.h>
#include <iostream>
using namespace std;
int main()
{
char input[256];
cout << "Password pl0x: ";
cin >> input;
if (!strcmp(input, "asdasd")) cout << "Good job!\n";
else cout << "Faiil\n";
cin.get();
return 0;
} | something like that
_________________
Don't laugh, I'm still learning photoshop! |
|
Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Wed Aug 22, 2007 8:19 am Post subject: |
|
|
TheSorc3r3r wrote: | you don't need to use std::string to compare strings. | Sigh. I wasn't saying that you need 'em, but you can use 'em.
IMO using char [] is old fashioned and that's why I prefer strings. Why to learn first the old way of doing stuff and then move to modern? There's no point.
char [] also is more like C syntax than C++ syntax, I think. strcmp is also a C command. It's from cstdlib, isn't it? And C stands for C :)
And btw.. You should be including cstdlib, not stdlib.h.
These are my thoughts, you don't have to agree them. There's no need to tell me, if you don't agree.
|
|
Back to top |
|
 |
zart Master Cheater
Reputation: 0
Joined: 20 Aug 2007 Posts: 351 Location: russia
|
|
Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Wed Aug 22, 2007 8:49 am Post subject: |
|
|
isnt strcmp from the String library? ....
_________________
|
|
Back to top |
|
 |
TheSorc3r3r I post too much
Reputation: 0
Joined: 06 Sep 2006 Posts: 2404
|
Posted: Wed Aug 22, 2007 3:28 pm Post subject: |
|
|
lurc, the string library is part of the C library.
idk, Jani, char arrays are the way I prefer; I never actually understood strings in memory until I used char arrays/pointers. Doesn't really matter though
_________________
Don't laugh, I'm still learning photoshop! |
|
Back to top |
|
 |
zart Master Cheater
Reputation: 0
Joined: 20 Aug 2007 Posts: 351 Location: russia
|
|
Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Wed Aug 22, 2007 4:21 pm Post subject: |
|
|
TheSorc3r3r wrote: | lurc, the string library is part of the C library. |
i know that.... the poster above said that it was part of the stdlib.h or cstdlib
THERE THE SAME THING
_________________
|
|
Back to top |
|
 |
Pseudo Xero I post too much
Reputation: 0
Joined: 16 Feb 2007 Posts: 2607
|
Posted: Wed Aug 22, 2007 4:55 pm Post subject: |
|
|
Thanks for the help, everyone.
I didn't even understand the basic form of a C++ app.
I got it to compile with no trouble, hopefully I'll be able to find what I need through google now.
_________________
haxory' wrote: | can't VB do anything??
windows is programmed using VB right? correct me if im wrong.
so all things in windows you have like the start menu is a windows form too. |
|
|
Back to top |
|
 |
Robotex Master Cheater
Reputation: 0
Joined: 05 Sep 2006 Posts: 378 Location: The pizza country!
|
Posted: Wed Aug 22, 2007 11:50 pm Post subject: |
|
|
use arrays when you are sure the code won't exceed the limit or you need high speed performance (in this case you would "reinvent the wheel" yourself, adapting it to your needs)
it's all a matter of programming needs
_________________
ASM/C++ Coder
Project Speranza lead developer |
|
Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Thu Aug 23, 2007 3:22 am Post subject: |
|
|
@TheSorc3r3r: C++ is an object oriented programming languange. "char" strings aren't objects unlike std::string's.. Try: Code: | char test[] = "test";
std::cout << test.length(); | and Code: | std::string test("test");
std::cout << test.length(); | :P
lurc wrote: | i know that.... the poster above said that it was part of the stdlib.h or cstdlib
THERE THE SAME THING | I do know that. I just told TheSorc3r3r to include cstdlib instead of stdlib.h, because cstdlib is the "c++ version" of it.
Robotex got the point anyway. It depends on the needs. I guess that the thread starter isn't in a competition where speed matters :P
|
|
Back to top |
|
 |
|