Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi. Easy Question For Good Programmers! 2

Status
Not open for further replies.

Only1Abhi

Technical User
Jan 26, 2003
77
GB
hi Everyone.

I am new to this C++ forum and have recently made a basic MS DOS Promopt program by coding in Visual C++.

I have a problem... and I need urgent replies!! I appreciate anything and everything people do for me!

Problem:
My program says: "Please type your mother maiden name:"
Therefore, I have a variable called "mmname". But I declared the variable using "char mmname". So When I type:
cin << mmname;
All I get is the first letter of the name I typed. I know this is because I used CHAR... What am I supposed to use instead?

Many Thanks in advance.

Regards,
Only1Abhi!
 
Try using the STL string class
Code:
#include <string>
using namespace std;

string mmname;
cin >> mmname;

-pete
 
Thanks for your reply Pete.

For some reason, MS Visual C++ doesn't seem to recognise string (like it recognises CHAR).

Here's what I got.

#include<iostream.h>
#include<string>

int main()
{
// Here's where I declare the string value
string mmname;

cout << &quot;please enter your mother's maiden name&quot;
cin >> mmname;

cout << &quot;You entered&quot; << mmname;

return -1;
}

Can u please tell me where I've gone wrong?
Thanks for ya help again.




 
You left out the
using namespace std;

thingie.

Or you could write it as
std::string mmname;


/Per
[sub]
if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
[/sub]
 
Thanks again for ya help:

Here's what I typed:

#include<iostream.h>
#include<string>

int main()
{
std::string mmname;

And then the rest (as above)!

But I still get errors... this is what I get (2 errors):


1. error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,cl
ass std::allocator<char> >' (or there is no acceptable conversion)

2. error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,cl
ass std::allocator<char> >' (or there is no acceptable conversion)

Many thanks again ppl!
 
#include<iostream> // Note: No .h

You need std:: before your cout now...




/Per
[sub]
if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
[/sub]
 
...and std:: before cin as well...



/Per
[sub]
if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
[/sub]
 
:D :D :D :D :D :D :D
THANK U VERY MUCH
:D :D :D :D :D :D :D

Thanks a lot.
It Works!!!

Hey /Per, u seem like an expert.
Do u have a hotmail address I can contact u on for later projects?

Many thanks!!!

P.S. DO u use MSN Messenger?

Regards,
Only1Abhi
 
> MSN Messenger?

perfnurt@hotmail.com

But don't expect immediate answers, as I do have some work to do :)


/Per
[sub]
if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
[/sub]
 
Only1Abhi

Your mixing the old stream library and STL. It will work but your complicating your life so i recommend not doing that. If you don’t know what I am talking about then you are a beginner and your trying to learn C/C++ though guessing and message based communications.

That is just not going to work.

Get yourself a book that targets beginners, they all cover these basic fundamental issues that you are struggling with. There are many more issues just like this and again the books will cover them. You will learn much faster with good information from a book and lots of time and work rather than trying to use a forum to learn the basics.

Now for your current problem follow this code and don't leave anything out!
Code:
#include <iostream>
#include <string>

using namespace std;

string mmname;
cin >> mmname;
cout << &quot;you entered &quot; << mmname << endl;

Now that's fine you have code obtained from someone here that works, big surprise. However unless you understand what it is and why it works what good does it do you.


-pete
 
Per, Thanks!
MSN Messenger is like a chat service. Download it. Search it on google. It's quite cool.


Palbano,

Thank you for all that information! I'll make note of it!!!
Do u also have a hotmail address?
Please only give it if u use MSN Messenger!
Many Thanks!

Thanks to both!
Great Help!


Best Regards,
Abhi!
 
Dietel and Dietel write good C++ books. The Teach yourself Visual C++ in 21 days is only good for MFC applications. There is also a Teach Yourself C++, that I don't remember the author, but it goes through alot of the C++ commands (std:: and such).

 
>> Do u also have a hotmail address?

Yes i do. However using direct discussions in email circumvents the entire purpose of Tek-Tips forums. For example the question you just asked has been asked and answered a hundred times in this forum and those threads could be found using the Keyword Search feature. Now this thread will also be available to other users to help them with this same problem.

For this reason I limit using my email to specific instances that need email like for file attachments etc.


-pete
 
(Palbano) ok m8 I understand...
Thanks for ya help anyway!


Thanks for the advice minifiredragon... I already have SAMS Teach Yourself Visual C++ in 21 Days.

Regards,
Abhi
 
Only1Abhi:
>MSN Messenger is like a chat service

Yes yes. I'm familiar with it, thank you.

palbano:
>Now this thread will also be available to other users to help them with this same problem.

If they stumble across in the next couple of days that it.
It seems that the same questions get asked over and over again quite often...

/Per
[sub]
if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
[/sub]
 
Yes, experienced members might occasionally point out the Search and FAQ utilities to new members, especially for these basic type questions.

If I had a C++ FAQ for every C++ FAQ I have written, I’d have…. ZERO [lol]

Too little time… too many FAQs

FAQs are there many… none I have written [yoda]

I’m on a roll

Anyway… it’s not right for me to complain about the lack of members using FAQs since I have not written any.


-pete
 
I don't think the lack of FAQs is the problem, but rather the fact that people don't look in them... :cool:

/Per
[sub]
if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
[/sub]
 
Hmmmm.... exactly! Didn't know about the keyword search feature... anyways, thanx both!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top