strings with spaces

This is a discussion on strings with spaces within the C++ Programming forums, part of the General Programming Boards category; how would i input a string 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,088
    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, 02: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, 08:57 AM
  5. accepting long strings with spaces
    By trang in forum C Programming
    Replies: 2
    Last Post: 01-05-2004, 03:17 PM

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