Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 24th, 2004, 04:02 PM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Basic char array and cin query.

Please look at the following:
Code:
#include <iostream.h> 
void GetInput(char name[15]); 
int main() 
{ 
  char word[15] = "Peter"; 
  cout << "Original word:" << word << endl; 
  GetInput(word); 
  cout << "Finally:" << word << endl; 
  return 0; 
} 
void GetInput(char name[15]) 
{ 
  cout << "Enter a name :"; 
  cin >> name; 
  name = "John"; 
  cout << "In function:" << name << endl; 
}
The output is :
Quote:
quote:
Original word:Peter
Enter a name:Fred
In function:John
Finally:Fred
So why did Fred persist and John didn't ?

(I'm very new to C++ so please be gentle)


Gill BC
__________________
Gill BC
Reply With Quote
  #2 (permalink)  
Old October 24th, 2004, 08:15 PM
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

In the GetInput function the line of code name = "John";
will not assign the string literal "john" into an array (unless you are initializing a declaration of a char array).
the '=' is for assignment of values.

you can use the c style string library for string manipulation:

# include <string.h>

and use the strcpy function in the following formats strcpy(string 1,string 2);, strcpy(string 1,"string literal");

the function copies the contents of string 2 to string 1;


Sample code:


# include <iostream.h>
# include <string.h> //C-style string library


void main()
{

    // Variable definition
    char string1[50]="something";
    char string2[50]="nothing";

    strcpy(string1,string2);
    cout<<string1<<endl;
    strcpy(string2,"New String");
    cout<<string2<<endl;

    return;
}

OUTPUT 1: nothing
OUTPUT 2: New string.







Mike
Reply With Quote
  #3 (permalink)  
Old October 24th, 2004, 10:53 PM
Authorized User
 
Join Date: Oct 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Gill,

Things get a lot easier if you start using the C++ string class instead of C-style character arrays. With the string class, assignment works the way you would expect it to. Since you're passing it into a function, you'd need to pass by reference to get the changes to appear outside the function. The new GetInput() would look like this:

void GetInput(string& name)
{
  cout << "Enter a name :";
  cin >> name;
  name = "John";
  cout << "In function:" << name << endl;
}


----
Scott J. Kleper
Author, "Professional C++"
(Wrox, 2005)
Reply With Quote
  #4 (permalink)  
Old October 24th, 2004, 11:16 PM
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

Gill,

you would want to declare a string object in main also

#include <iostream.h>
#include <string> // C++ standard string class


void GetInput(string &); // Function Prototype

int main()
{
// sting object definition, initialized with Peter
string word (Peter);

cout << "Original word:" << word << endl;
GetInput(&word);
cout << "Finally:" << word << endl;
return 0;
}
Reply With Quote
  #5 (permalink)  
Old November 4th, 2004, 07:44 PM
Registered User
 
Join Date: Nov 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,Gill
I think your example is really confusing for me at beginning, you know , I am also quite new
to C++, but I love it and would like to spend time on it.
The first thing you should know is whether your function GetInput is pass-by-value or pass-by-refernce.(If you don't know what's the difference between them, look it up in google....)
In fact,your example gave me a deeper understanding of them,thanks.I wrote a test program as below:
#include <iostream.h>
#include <cstring>

void GetInput(char *name)
{
name="Irene";
}

void GetInput2(char **name)
{*name="Jessie";
}

int main()
{
  char *word = "Peter";
  cout<<"original: "<<word<<endl;
  word ="Ken";
  cout<<"middle : "<<word<<endl;
  GetInput(word);
  cout<<"finally: "<<word<<endl;
  GetInput2(&word);
  cout<<"finally: "<<word<<endl;
}

The output is :

original: Peter
middle : Ken
finally: Ken
finally: Jessie

The reason is acually simple , just because GetInput use pass-by-value whilst GetInput2 use pass-by-reference.

Although the formal argument in GetInput is a pointer,it isn't the address of the variable word(word itself is a pointer!).So, in GetInput , you acually only modified the copy version of word.
But why can CIN work in your fucntion? I am not very sure, I think that's because the copy version is only a temporary copy,if cin put our input things in this copy one, it will soon disapear,so it have to store it in the real version...anyway, it's about the internal facility
of CIN....





Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Big challenge here! How to convert char* to char^? samiswt Visual C++ 2005 1 November 30th, 2007 09:09 PM
Basic SQL query john2007 Beginning VB 6 2 January 18th, 2007 04:39 PM
Invalid conversion from 'char*' to 'char' deuxmio C++ Programming 3 December 8th, 2006 07:56 AM
Char array cin query (additional) gillianbc C++ Programming 3 October 29th, 2004 03:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.