C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
help with pointers Posted by gemacjr on 14 Nov 2006 at 3:55 PM
I can't get this to work

void main()
{
char string[20];
char *aString = string;
function (aString);
}
void function(char *name)
{
cout << "enter name";
cin >> *name;
cout << name;
}
Report
Re: help with pointers Posted by gautam on 14 Nov 2006 at 4:17 PM
Hi,

Your cin >> *name <---- this is incorrect - by doing this you are actually modifying only the first character of the string. What you want is cin >> name;


: I can't get this to work
:
: void main()
: {
: char string[20];
: char *aString = string;
: function (aString);
: }
: void function(char *name)
: {
: cout << "enter name";
: cin >> *name;
: cout << name;
: }
:

Report
Re: help with pointers Posted by gemacjr on 14 Nov 2006 at 4:23 PM
: Hi,
:
: Your cin >> *name <---- this is incorrect - by doing this you are actually modifying only the first character of the string. What you want is cin >> name;
:
:
: : I can't get this to work
: :
: : void main()
: : {
: : char string[20];
: : char *aString = string;
: : function (aString);
: : }
: : void function(char *name)
: : {
: : cout << "enter name";
: : cin >> *name;
: : cout << name;
: : }
: :
:
: I thought so, I thought they were trying to trick me

Thanks so much

Report
Re: help with pointers Posted by stober on 14 Nov 2006 at 5:38 PM
: : Hi,
: :
: : Your cin >> *name <---- this is incorrect - by doing this you are actually modifying only the first character of the string. What you want is cin >> name;
: :
: :
: : : I can't get this to work
: : :
: : : void main()
: : : {
: : : char string[20];
: : : char *aString = string;
: : : function (aString);
: : : }
: : : void function(char *name)
: : : {
: : : cout << "enter name";
: : : cin >> *name;
: : : cout << name;
: : : }
: : :
: :
: : I thought so, I thought they were trying to trick me
:
: Thanks so much
:
:


you don't need pointer aString. Just pass variable string like this:
   function (string);


Report
Re: help with pointers Posted by bilderbikkel on 15 Nov 2006 at 1:29 AM
main() is of return type int. See www.codepedia.com/1/CppMain for references.
bilderbikkel

Report
Re: help with pointers Posted by bluj91 on 16 Nov 2006 at 3:37 PM
: main() is of return type int. See www.codepedia.com/1/CppMain for references.
: bilderbikkel
:
:

In C++, try using the class std::string and the function getline().



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.