Thread: Quick help

  1. #1
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483

    Quick help

    I am trying to get a whole line from user and write into an array of char's

    but it doesn't work.

    what do i use?

    i know 'cin' stops as soon as there is a space in the string..

    i've tried:

    Code:
    getline(cin, variable);
    
    // I GET AN ERROR^

  2. #2
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    /**************************************************  ****
    
        Get a whole line and write to an array
      
    **************************************************  ****/
    
    
    #include <iostream>
    #include <string.h>
    #include <math.h>
    #include <ctype.h>
    
    int print_out(char[],int);
    
    using namespace std;
    
    int main()
    {
        char array[81];
        char write_array[81]; // the new array you wanna write to
        cin.getline(array,81);
        int size_of=strlen(array);
        
        for (int a=0; a<size_of; a++)
        {
            write_array[a]=array[a];
        }
            
        print_out(write_array,size_of);
         
        int stop;
        cin>>stop;
        
    }
    
    
    //Declare function print out
    int print_out(char write_array[],int size_of)
    {   
        cout<<"The array is:";
        for (int a=0; a<size_of; a++)
        {
            cout<<write_array[a];
        }
            
       
    }

  3. #3
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by mrafcho001
    I am trying to get a whole line from user and write into an array of char's

    but it doesn't work.

    what do i use?

    i know 'cin' stops as soon as there is a space in the string..

    i've tried:

    Code:
    getline(cin, variable);
    
    // I GET AN ERROR^
    Have you actually declared 'variable' as an std::string or char array?

  4. #4
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Lithorein Yes

    I got it working.. i wasn't sure what it was..

    cin.getline() works perfectly thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  3. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  4. Quick Sort Help
    By NavyBlue in forum C Programming
    Replies: 1
    Last Post: 03-02-2003, 10:34 PM
  5. Replies: 0
    Last Post: 04-30-2002, 07:24 PM