Thread: RE: [Dev-C++] I am new to C ++ and need some help
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Bruce Y. <y2...@ho...> - 2000-12-11 00:46:05
|
<html><DIV>
<P>be sure to include the: #include <string></P>
<P><BR>#include <iostream.h><BR>#include <stdlib.h><BR>#include <string></P>
<P>int main()<BR>{<BR> string name;</P>
<P> do<BR> { cout << "\nPlease enter your name: ";<BR> cin >> name;</P>
<P> cout << "\nName = " << name << endl;</P>
<P> if (name == "John" || name == "john")<BR> cout << "\nThank you!!\n";<BR> else<BR> cout << "\nNo. Try again";</P>
<P> }<BR> while(name != "John" || name != "john");</P>
<P> </P>
<P> system("PAUSE");<BR> return 0;<BR>}</P></DIV><br clear=all><hr>Get more from the Web. FREE MSN Explorer download : <a href="http://explorer.msn.com">http://explorer.msn.com</a><br></p></html>
|
|
From: <pra...@ya...> - 2000-12-11 09:50:28
|
hey u used the wrong logic!
you cannot use the statement
if(char c=='bruce')
{cout<<"Thankk You";
you will have to use the following commands:-
#include<string>
char c[25];
cout<<"enter your name";
if(strcmp(c,"bruce")==0)
cout<<"Thank You";
else
cout<<"You are not granted access";
exit(0);
}
--- Bruce Young <y2...@ho...> wrote:
<HR>
<html><DIV>
<P>be sure to include the: #include
<string></P>
<P><BR>#include <iostream.h><BR>#include
<stdlib.h><BR>#include <string></P>
<P>int main()<BR>{<BR> string
name;</P>
<P> do<BR> { cout <<
"\nPlease enter your name:
";<BR> cin >>
name;</P>
<P> cout <<
"\nName = " << name << endl;</P>
<P> if (name == "John"
|| name ==
"john")<BR>
cout << "\nThank
you!!\n";<BR>
else<BR>
cout << "\nNo. Try again";</P>
<P> }<BR> while(name != "John" ||
name != "john");</P>
<P> </P>
<P>
system("PAUSE");<BR>
return 0;<BR>}</P></DIV><br clear=all><hr>Get more
from the Web. FREE MSN Explorer download : <a
href="http://explorer.msn.com">http://explorer.msn.com</a><br></p></html>
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
http://lists.sourceforge.net/mailman/listinfo/dev-cpp-users
__________________________________________________
Do You Yahoo!?
Yahoo! Shopping - Thousands of Stores. Millions of Products.
http://shopping.yahoo.com/
|
|
From: Ioannis V. <no...@ya...> - 2000-12-12 03:34:01
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...]On Behalf Of
> prakyath pai
> Sent: Monday, December 11, 2000 11:47 AM
> To: dev...@li...
> Subject: RE: [Dev-C++] I am new to C ++ and need some help
>
>
> hey u used the wrong logic!
> you cannot use the statement
> if(char c=='bruce')
> {cout<<"Thankk You";
> you will have to use the following commands:-
>
> #include<string>
> char c[25];
> cout<<"enter your name";
> if(strcmp(c,"bruce")==0)
> cout<<"Thank You";
> else
> cout<<"You are not granted access";
> exit(0);
> }
In fact the following stuff can be used in C++ only.
char s1[20], s2[20];
...
if(s1==s2)
...
The overloaded string operator == accepts 2 strings (objects) and the
C-style strings are "upgraded" to temporary strings so as the comparison to
work.
But the better is string s1, s2; to be used instead (including the
<string>).
Ioannis
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
|
|
From: Nick <en...@ho...> - 2000-12-12 07:48:37
|
Hello,
> In fact the following stuff can be used in C++ only.
>
> char s1[20], s2[20];
> ...
>
> if(s1==s2)
> ...
>
> The overloaded string operator == accepts 2 strings (objects) and the
> C-style strings are "upgraded" to temporary strings so as the
> comparison to work.
You can use == as long as one of the arguments is a (C++) string
object. In your example above, it does not use the string operator.
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main(){
char temp1[20], temp2[20];
string temp3;
strcpy(temp1,"John");
strcpy(temp2,"John");
temp3 = "John";
if (temp1 == temp2) cout << "temp1 == temp2" << endl;
else cout << "temp1 != temp2" << endl;
if (temp3 == temp1) cout << "temp3 == temp1" << endl;
else cout << "temp3 != temp1" << endl;
if (temp2 == temp3) cout << "temp2 == temp3" << endl;
else cout << "temp2 != temp3" << endl;
system("pause");
return(0);
}
> But the better is string s1, s2; to be used instead (including the
> <string>).
Agreed.
--
regards,
Nick
|
|
From: Ioannis V. <no...@ya...> - 2000-12-12 11:17:13
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...]On Behalf Of Nick > Sent: Tuesday, December 12, 2000 9:48 AM > To: dev...@li... > Subject: RE: [Dev-C++] I am new to C ++ and need some help > > > > Hello, > > > In fact the following stuff can be used in C++ only. > > > > char s1[20], s2[20]; > > ... > > > > if(s1==s2) > > ... > > > > The overloaded string operator == accepts 2 strings (objects) and the > > C-style strings are "upgraded" to temporary strings so as the > > comparison to work. > > You can use == as long as one of the arguments is a (C++) string > object. In your example above, it does not use the string operator. Oops, you are absolutely right, it is pointer comparison. Ioannis __________________________________________________ Do You Yahoo!? Talk to your friends online with Yahoo! Messenger. http://im.yahoo.com |
|
From: Chad S. <ho...@ho...> - 2000-12-11 16:37:11
|
>From: prakyath pai <pra...@ya...>
>Reply-To: dev...@li...
>To: dev...@li...
>Subject: RE: [Dev-C++] I am new to C ++ and need some help
>Date: Mon, 11 Dec 2000 01:47:06 -0800 (PST)
>
>hey u used the wrong logic!
>you cannot use the statement
>if(char c=='bruce')
>{cout<<"Thankk You";
>you will have to use the following commands:-
>
>#include<string>
>char c[25];
>cout<<"enter your name";
>if(strcmp(c,"bruce")==0)
>cout<<"Thank You";
>else
>cout<<"You are not granted access";
>exit(0);
>}
If you bothered to look at the post, you'd see that he didn't use a char in
the comparison. Nor did he use a C style string (char *). He instead used a
C++ string class, which does support the == operator to another string for
evaluation.
So to finally kill this thread...
If you are programming in C you'd use:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char Name[5];
puts("What is your name??");
fgets(Name, 5, stdin);
if(!strcmp(Name, "John") || !strcmp(Name, "john")
|| !strcmp(Name, "JOHN"))
{
puts("Hello John.");
}
else
puts("Your name isn't John, go away.");
return 0;
}
If you are using C++ use:
int main()
{
string Name;
cout << "What is your name??" << endl;
cin >> Name;
if((Name == "John") || (Name == "JOHN") || (Name == "john"))
{
cout << "Hello John" << endl;
}
else
cout << "Your name isn't John, go away. << endl;
return 0;
}
Chad Simmons
_____________________________________________________________________________________
Get more from the Web. FREE MSN Explorer download : http://explorer.msn.com
|
|
From: Chad S. <ho...@ho...> - 2000-12-11 17:14:11
|
>From: "Chad Simmons" <ho...@ho...>
>Reply-To: dev...@li...
>To: dev...@li...
>Subject: RE: [Dev-C++] I am new to C ++ and need some help
>Date: Mon, 11 Dec 2000 08:37:06 -0800
> else
> cout << "Your name isn't John, go away. << endl;
The above should read
else
cout << "Your name isn't John, go away." << endl;
_____________________________________________________________________________________
Get more from the Web. FREE MSN Explorer download : http://explorer.msn.com
|