|
-
Nov 6th, 2001, 08:56 PM
#1
-
Nov 6th, 2001, 09:02 PM
#2
-
Nov 6th, 2001, 09:04 PM
#3
PowerPoster
First, try putting the header file like this:
PHP Code:
#include <iostream.h>
-
Nov 6th, 2001, 09:06 PM
#4
I still got the same error.
-
Nov 6th, 2001, 09:07 PM
#5
PowerPoster
What compiler are you using?
-
Nov 6th, 2001, 09:09 PM
#6
-
Nov 6th, 2001, 09:10 PM
#7
Member
Use "double quotes" for strings longer than one char, use 's'ingle quotes for one char or for escape chars like '\n'.
-
Nov 6th, 2001, 09:14 PM
#8
PowerPoster
aaahhhh...did not notice that. Maybe, time to sleep
-
Nov 6th, 2001, 09:16 PM
#9
it works filburt1, but whatever i type ( include ILO) , it produce Ugly. why ?
-
Nov 6th, 2001, 09:22 PM
#10
PowerPoster
How about this:
PHP Code:
#include "iostream.h"
void main()
{
char name[25];
cout << "Enter Your Name ";
cin >> name;
cout<<IIF(name == "ILO","Handsome","Ugly");
return 0;
}
-
Nov 6th, 2001, 09:26 PM
#11
I got error C2065: 'IIF' : undeclared identifier
-
Nov 6th, 2001, 09:43 PM
#12
Hyperactive Member
Re: Help in string
Code:
#include <iostream.h>
#include <string.h>
void main()
{
char name[25];
cout << "Enter Your Name ";
cin >> name;
if (strcmp(name,"ILO")==0)
{
cout << "Handsome";
}
else
{
cout << "Ugly";
}
}
I usually do it this way. If you want to do it (if (name=="ILO"))this way , you got to use a string class, I think. I never use a string class, I am lazy, I just use the easiest knowledge available(i.e. my brain)
By the way, is your surname "Wong"?
-
Nov 6th, 2001, 09:52 PM
#13
Member
Originally posted by abdul
How about this:
PHP Code:
#include "iostream.h"
void main()
{
char name[25];
cout << "Enter Your Name ";
cin >> name;
cout<<IIF(name == "ILO","Handsome","Ugly");
return 0;
}
Almost LOL, Iif is only in one language: VB. There's that funky inline if deal in C++ (condition ? true : false or condition : false ? true, I forget which).
-
Nov 6th, 2001, 09:54 PM
#14
Thank's transcendental.
I will try it.
that's not My surename, it just a nick name. is this have special mean for you ?
-
Nov 6th, 2001, 09:58 PM
#15
transcendental analytic
Cstrings don't have an overloaded operator == to compare them.
Immediate if is equivalent with ternary operator ?:
I suggest you learn the language basics first before you continue:
Here's a good tutorial, Sam's teach yourself C++ in 21 days:
http://newdata.box.sk/bx/c/
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 6th, 2001, 10:01 PM
#16
Hyperactive Member
Originally posted by WongEdan
that's not My surename, it just a nick name. is this have special mean for you ?
Curious just asking.
-
Nov 6th, 2001, 10:08 PM
#17
Hyperactive Member
Originally posted by kedaman
Cstrings don't have an overloaded operator == to compare them.
Immediate if is equivalent with ternary operator ?:
I suggest you learn the language basics first before you continue:
Here's a good tutorial, Sam's teach yourself C++ in 21 days:
http://newdata.box.sk/bx/c/
Hi, kedaman, does the ANSI C++ have a string class? Curious. When I say string class, I am not refering to MFC's CString class.
Practical C++ book uses it all the time. small capital s in string class. #include<string>
-
Nov 6th, 2001, 10:11 PM
#18
transcendental analytic
yeah, there's a class template basic_string which string is instantiated from, i've never used it though, Cstrings have been good enough for me since i don't do anything advanced with them.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 6th, 2001, 10:34 PM
#19
PowerPoster
Originally posted by filburt1
Almost LOL, Iif is only in one language: VB. There's that funky inline if deal in C++ (condition ? true : false or condition : false ? true, I forget which).
Totally totally messed up...damn I have seen this "ratio" if in javascript too but I was thinking of vb one
-
Nov 7th, 2001, 02:03 AM
#20
Hyperactive Member
This code below compiles and works with Dev-C++ and Free Borland compiler.
Code:
#include<string>
#include<iostream>
using namespace std;
int main(void)
{
string holder;
holder="myname";
if (holder=="myname")
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
return 0;
}
This is working VC code below.
Code:
CTest_CStringDlg::OnOK()
{
// TODO: Add extra validation here
CString holder;
holder="myname";
if (holder=="myname")
MessageBox("Yes");
else
MessageBox("No");
CDialog::OnOK();
}
Last edited by transcendental; Nov 7th, 2001 at 02:13 AM.
I'm a VB6 beginner.
-
Nov 7th, 2001, 07:09 AM
#21
Oh my god............................................
Immediate if is
condition ? true : false;
C-Strings don't have a == operator. To use == you must either use string from the header <string> or the MFC CString (what you do ONLY in MFC apps). To compare C-strings, always use strcmp. It returns 0 if the strings are equal. It is declared in <strings.h> or (real C++) <cstring>. If you use the == operator with C-strings you compare the MEMORY ADDRESSES!
the string class is ANSI C++.
Use
#include <iostream>
using namespace std;
this is ANSI C++, iostream.h may disappear someday.
the <string> header also contains a class named wstring for UNICODE strings.
trans: the reason why the codes works is because the == operator is overloaded in CString and string.
But even this code would work:
Code:
char* str = "Hello":
if(str == "Hello") // ...
I'm not 100% sure, but I think this would work, because:
In every exe, the hardcoded strings are stored at a specific region. Every string is stored only once, if you use exactly the same string twice, both will refer to the same location.
If you write:
char* str = "Hello";
the address of the string "Hello" in the exe is assigned to str. Now you can work with the string.
If you later write:
if(str == "Hello")
you compare the memory adderss in str and the address of the string "Hello". Those are the same, so the statement returns true. I'm not sure, but I think this code will still enter the if:
Code:
char* str = "Hello";
str[2] = t; // string is now "Hetlo"
if(str == "Hello")
{ // I think execution will get here...
}
keda or parksie, do you think this works? I'm too lazy to try...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 7th, 2001, 08:00 AM
#22
transcendental analytic
You're right, a string is char* and pointer aritmethic applies on all pointers, except for str[2] = t; the code compiles
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 7th, 2001, 08:07 AM
#23
does it only compile or does the if fire too?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 7th, 2001, 08:24 AM
#24
transcendental analytic
It fires, and i guess the optimizer placed both reference to "Hello"at the same place
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 7th, 2001, 08:28 AM
#25
I think every compiler does it, not only when optimized, but also when in debug mode.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 7th, 2001, 08:33 AM
#26
OK it should be
str[2] = 't';
and this produces an access violation because I'm trying to change the exe, which is write-protected.
But if I remove that, it even works in debug mode.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 7th, 2001, 08:43 AM
#27
transcendental analytic
It's in the exe of course, since it's a constant, otherways it wouldn't have optimized it to use the same string.
I got an access violation
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 7th, 2001, 08:47 AM
#28
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
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
|