First important point, Dev-CPP is *not* a compiler that provides or denies functionality - it is an IDE that runs the GCC compiler. This is important to realize, because it enables you to frame and search on questions..
Next. Your code did not work because of some typos and an error. First, C++ is case sensitive, so
mytest
is not the same as
myTest
but you used them interchangably. Next
cout >> myTest();
makes no sense. What you want to do is take the output of myTest() and put it out into the stream, which is done as follows
cout << myTest();
You also left out
using namespace std;
or equivalent.
This is why you have to be careful when coding a new concept...there was nothing wrong with your design, the implementation was just careless (something I have done on more than one occasion...in the last hour). The following code compiles and runs per your design...Dev 4.9.8.1/Windows XP
#include <iostream>
#include <string>
using namespace std;
string myTest(void);
int main(void)
{
cout << myTest();
system("pause");
return 0;
}
string myTest(void)
{
return "Happy\n";
Wayne
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
It is always a good idea to post your full compile log with your code, it can help if you have some other problem...post the full log, not just any errors you might see..
Wayne
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2003-07-24
Thank you very much!I did solve the problem.
My main problem is to put "using namespace std;"
after the declairation of the function. I'm sorry
that I mistyped the source code when I post this
thread. And thanks a lot to Wayne that you gave
me many advices. Thank you both!
WayRe
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I was very tired last night when I wrote the post above and I didn't notice the errors.
I'm sorry if you find that I "kick your butt on debugging". That is not what I meant. I just want to give a hand when I can and have time. That's all. Excuse me if I offended you.
Liviu
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm sorry I didn't understand correctly what you wanted to say. I am not a native english speaker and my knowledge of every day english is not very good.
Thank you for your compliments. I really appreciate it coming from you.
Liviu
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You do fine with English, and are great with helping with C++!
Sometimes the help folks give get taken for granted, I am trying to improve on recognizing the good work that you and others do out of the goodness of your heart, and the skill of your brain.
Have a great day!
Wayne
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I live in Florida in the USA, so its a little warm this time of year...
:-)
Wayne
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2003-07-25
Hello Wayne and everybody,
I have another question maybe someone could help me. I want to output a formatted string from keyboard input, as below:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main(void)
{
string str;
cout<<"Input a string : "<<endl;
cin>>str;
printf("%20s\n",str);
system("pause");
return 0;
}
But I get warning message during compiling :
F:/test2.cpp: In function `int main()':
F:/test2.cpp:11: warning: cannot pass objects of non-POD type `struct std::string' through `...'
Since this had been successfully compiled,I tried to run it but I got strange output......Thanks a lot!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
For your specific problem, the answer is:
printf("%20s\n",str.c_str());
The c_str() function of the class std::string returns a const char* to the contained string.
You must use that because the printf function takes as arguments only simple C types: char, int, float, etc. or pointers.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2003-07-25
Wow, it works! Thank you so much nliviu. Everybody here are so kind and so cool.
Both of you are my leading card. *^_^*
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Dear all,
I'd like to return a string from a defined function but I just get failed to do it. For example:
#include <iostream>
#include <string>
string mytest(void);
int main(void)
{
cout >> myTest();
system("pause");
return 0;
}
string mytest(void)
{
return "Happy";
}
Does DEV-Cpp provide "string" data type?
Or I just missed something so that it failed?
Thanks a lot.
There are 2 ways to use standard C++ types ( such as string, vector, list, etc.).
Either you add "using namespace std;" ( without the quotes) after including the headers, or use the qualified names of the objects you want to use.
Ex. 1:
#include <iostream>
#include <string>
using namespace std;
string mytest(void);
int main(void)
{
cout >> myTest();
system("pause");
return 0;
}
string mytest(void)
{
return "Happy";
}
Ex. 2
#include <iostream>
#include <string>
std::string mytest(void);
int main(void)
{
std::cout >> myTest();
system("pause");
return 0;
}
std::string mytest(void)
{
return "Happy";
}
First important point, Dev-CPP is *not* a compiler that provides or denies functionality - it is an IDE that runs the GCC compiler. This is important to realize, because it enables you to frame and search on questions..
Next. Your code did not work because of some typos and an error. First, C++ is case sensitive, so
mytest
is not the same as
myTest
but you used them interchangably. Next
cout >> myTest();
makes no sense. What you want to do is take the output of myTest() and put it out into the stream, which is done as follows
cout << myTest();
You also left out
using namespace std;
or equivalent.
This is why you have to be careful when coding a new concept...there was nothing wrong with your design, the implementation was just careless (something I have done on more than one occasion...in the last hour). The following code compiles and runs per your design...Dev 4.9.8.1/Windows XP
#include <iostream>
#include <string>
using namespace std;
string myTest(void);
int main(void)
{
cout << myTest();
system("pause");
return 0;
}
string myTest(void)
{
return "Happy\n";
Wayne
Liviu,
Didn't notice the other errors in the code eh?
:-)
Is OK, most of the time you kick my butt on debugging.
Wayne
Way,
It is always a good idea to post your full compile log with your code, it can help if you have some other problem...post the full log, not just any errors you might see..
Wayne
Thank you very much!I did solve the problem.
My main problem is to put "using namespace std;"
after the declairation of the function. I'm sorry
that I mistyped the source code when I post this
thread. And thanks a lot to Wayne that you gave
me many advices. Thank you both!
WayRe
WayRe,
**NEVER** type code into a post. Copy and paste the exact code that you are trying to compile. It is far easier and avoids misunderstandings.
Wayne
Wayne,
I was very tired last night when I wrote the post above and I didn't notice the errors.
I'm sorry if you find that I "kick your butt on debugging". That is not what I meant. I just want to give a hand when I can and have time. That's all. Excuse me if I offended you.
Liviu
No, Liviu, I was *complimenting* you for your debugging skills! (Telling you yours are better than mine)
In pointing out the other errors, I was trying to be humorous, but obviously I did it quite badly...
My turn to apologize,
Wayne
Wayne,
I'm sorry I didn't understand correctly what you wanted to say. I am not a native english speaker and my knowledge of every day english is not very good.
Thank you for your compliments. I really appreciate it coming from you.
Liviu
You do fine with English, and are great with helping with C++!
Sometimes the help folks give get taken for granted, I am trying to improve on recognizing the good work that you and others do out of the goodness of your heart, and the skill of your brain.
Have a great day!
Wayne
Thank you again, Wayne.
BTW, it's almost night here in Romania :-)
Liviu
Good evening then!
:-)
I am about to take a late lunchtime bike ride...
I am toooo fat
Wayne
Made it through a low (~ 16 mph), 12.75 miles...
I live in Florida in the USA, so its a little warm this time of year...
:-)
Wayne
Hello Wayne and everybody,
I have another question maybe someone could help me. I want to output a formatted string from keyboard input, as below:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main(void)
{
string str;
cout<<"Input a string : "<<endl;
cin>>str;
printf("%20s\n",str);
system("pause");
return 0;
}
But I get warning message during compiling :
F:/test2.cpp: In function `int main()':
F:/test2.cpp:11: warning: cannot pass objects of non-POD type `struct std::string' through `...'
Since this had been successfully compiled,I tried to run it but I got strange output......Thanks a lot!
It is not a very good idea to mix C and C++ functions.
But if you do that, you must use the proper arguments.
You might want to check this link:
http://www.cplusplus.com/ref/cstdio/printf.html.
For your specific problem, the answer is:
printf("%20s\n",str.c_str());
The c_str() function of the class std::string returns a const char* to the contained string.
You must use that because the printf function takes as arguments only simple C types: char, int, float, etc. or pointers.
Wow, it works! Thank you so much nliviu. Everybody here are so kind and so cool.
Both of you are my leading card. *^_^*