Reading and Writing Strings

This is a discussion on Reading and Writing Strings within the C++ Programming forums, part of the General Programming Boards category; Hi, i want to read some number of strings(say 4 names) , and write them when i wanted later in ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Location
    India
    Posts
    3

    Post Reading and Writing Strings

    Hi, i want to read some number of strings(say 4 names) , and write them when i wanted later in the program. I tried by the following code. but it did'nt worked out. i'm getting the output of the last name. Please go through the code.

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
            char name[6],*p[3];  //declaring names and pointer to hold those names.
    	
    	for(int i=0; i<=2; i++)
    	{
    		for(int j=0; j<=5;j++)
    		{
    			cin>>name[j];   //used to get names 
    		}
    
    		p[i]=name;
    				
    	}
    
    	 for(int i=0; i<=2; i++)
    	 {
    		 cout<<p[i]<<"\n";
    	 }
    
    	 return 0;
      }
    
    Input:
    --------
    God 
    Is
    Good
    
    Output:(i want to get)
    -----------------------------
    God 
    Is 
    Good
    
    Output: (i'm getting)
    ---------
    Good
    Good
    Good
    Please help me on this. Plzzz tell me where i'm doing the mistake. Is that logic totally wrong?? If that logic is completely wrong plzzz give me the correct solution.
    Thanks .

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Because you're not storing strings, but chars.

    Code:
    char name[3][6];
    cin >> name[i];
    p[i] = name[i];
    You can eliminate the 'j' loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Location
    India
    Posts
    3

    Thank you

    Thanks Salem, i tried that 2-D arrays , got the desired output.
    But can you tell me how can i get the same kind of output without using 2-D arrays. I wann to do it by using pointers. Plzzzzzzz help me.

    Thanks again.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    1
    you problem is your re-using the name array each time, so p[0] points to name, p[1] points to name and p[2] points to name.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    p[i]=name;
    Pointers store addresses, so in that line you are assigning the address of name to p[i]. Does the address of name ever change? No. What's at the address of name changes when you read something into name, but name's address doesn't change.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Location
    India
    Posts
    3
    Hi 7stud, thank you very much. Its damn clear now. thank you very much. Thank you everybody who replied.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 02:14 AM
  2. Quick check on reading in + using strings
    By advancedk in forum C Programming
    Replies: 2
    Last Post: 12-08-2008, 09:12 PM
  3. problem with reading and writing
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2006, 03:38 PM
  4. accessing my com port, writing and reading data
    By shoobsie in forum C Programming
    Replies: 7
    Last Post: 09-16-2005, 03:29 PM
  5. file writing and reading
    By Micko in forum C Programming
    Replies: 8
    Last Post: 01-13-2004, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21