Thread: strings with spaces

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    33

    strings with spaces

    how would i input a string with spaces?

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You might want to post your code and your attempt, but here is code I posted in a thread earlier today:
    Originally posted by jlou

    Preferred using standard string:
    Code:
    #include<iostream>
    #include<string>
    
    int main()
    {
        std::string string_a;
    
        std::cout<<"What is your string: ";
        std::getline(std::cin, string_a);
    
        std::cout<<" The length of \""<<string_a<<"\" is " << string_a.length()<<std::endl;
    
        return 0;
    }
    And using c-style strings:
    Code:
    #include<iostream>
    #include<cstring>
    
    int main()
    {
        char string_a&#091;100&#093;;
    
        std::cout<<"What is your string: ";
        std::cin.getline(string_a, 100);
    
        std::cout<<" The length of \""<<string_a<<"\" is " << strlen (string_a)<<std::endl;
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading strings with spaces
    By dezz101 in forum C Programming
    Replies: 10
    Last Post: 10-29-2008, 06:02 AM
  2. reading strings with spaces using sscanf
    By kotoko in forum C Programming
    Replies: 2
    Last Post: 06-14-2008, 05:06 PM
  3. Strings allowing spaces?
    By Dan17 in forum C++ Programming
    Replies: 13
    Last Post: 02-04-2006, 03:15 PM
  4. Getting scaf to accept strings with spaces in them?
    By Wiretron in forum C Programming
    Replies: 4
    Last Post: 11-12-2005, 09:57 AM
  5. accepting long strings with spaces
    By trang in forum C Programming
    Replies: 2
    Last Post: 01-05-2004, 04:17 PM