Like Tree3Likes
  • 1 Post By laserlight
  • 1 Post By laserlight
  • 1 Post By AndrewHunter

Input whole String

This is a discussion on Input whole String within the C++ Programming forums, part of the General Programming Boards category; I want to ask: in C/C++, for example: Code: #include<string> using namespace std; int main(){ string str; cin<<str; //Input: New ...

  1. #1
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    Input whole String

    I want to ask: in C/C++, for example:
    Code:
    #include<string>
    using namespace std;
    int main(){
        string str;
        cin<<str; //Input: New Problem
        cout>>str; //Output: New
        return(0);
    }
    It's mean, when input, It will terminate when see space character and maybe literal character,too. So, how can It will take all of character that I have typed.

    thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,394
    Use std::getline to read a line into a std::string object.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    Question

    Oh, I have search getline and test, It's OK, but not as you say, <readline> read a line and put into char array in C. this is my example:
    Code:
    #include<iostream>
    using namespace std;
    int main(){
       char str[10];
       cin.getline(str,10);
       cout<<str;
       return(0);
    }
    So, we use char array in C again !!! (but many people have said to me, that not recommended in C++). So, please give an idea, please.

    thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,394
    You should read carefully.
    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main() {
        string str;
        getline(cin, str); // Input: New Solution
        cout << str << endl; // Output: New Solution
        return 0;
    }
    hqt likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by hqt View Post
    Oh, I have search getline and test, It's OK, but not as you say, <readline> read a line and put into char array in C. this is my example:
    Code:
    #include<iostream>
    using namespace std;
    int main(){
       char str[10];
       cin.getline(str,10);
       cout<<str;
       return(0);
    }
    So, we use char array in C again !!! (but many people have said to me, that not recommended in C++). So, please give an idea, please.

    thanks
    You used the wrong getline, note Laser said to use std::getline. Here:
    Code:
    #include <iostream>
    #include <string>
    
    int main(void){
    
    	std::string line;
    
    	std::cout<<"Enter a line: ";
    	std::getline(std::cin,line); //this is the correct getline to use
    
    	std::cout<<"You entered: "<<line;
    
    	return(0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    Oh, so great !!! But I have read this page: (the first link on google when I type: C++ std::getline)
    istream::getline - C++ Reference
    So, maybe have some different between <std::getline> and <istream::getline>
    ************************************************** ******
    <add for more information>
    Ahh, and know I see what <getline> that laserlight have said(this <getline> in string library, and above <getline> in iostream libray, so interesting )

    http://www.cplusplus.com/reference/string/getline/
    Last edited by hqt; 09-15-2011 at 09:42 PM.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by hqt View Post
    Oh, so great !!! But I have read this page: (the first link on google when I type: C++ std::getline)
    istream::getline - C++ Reference
    So, maybe have some different between <std::getline> and <istream::getline>
    istream::getline (e.g. cin.getline)
    std::getline

    They are two different functions.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    Question

    But, hmmm, sorry, maybe I have another questions please (just for learning purpose), that I want to know, how can do this(read a whole line) in C. (because, in C, there no iostream and string library).

    I have one solution use fget(), to take each character and processing it, but I think it isn't beautiful so much. So, who know this problem, tell me, please.

    thanks for tons

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,394
    Quote Originally Posted by hqt
    But, hmmm, sorry, maybe I have another questions please (just for learning purpose), that I want to know, how can do this(read a whole line) in C. (because, in C, there no iostream and string library).
    I have one solution use fget(), to take each character and processing it, but I think it isn't beautiful so much. So, who know this problem, tell me, please.
    fgets
    hqt likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by hqt View Post
    But, hmmm, sorry, maybe I have another questions please (just for learning purpose), that I want to know, how can do this(read a whole line) in C. (because, in C, there no iostream and string library).

    I have one solution use fget(), to take each character and processing it, but I think it isn't beautiful so much. So, who know this problem, tell me, please.

    thanks for tons
    You would use fgets:
    Code:
    #include <stdio.h>
    
    
    #define BUFSIZE 50
    
    int main(void){
    
    	char line[BUFSIZE];
    
    	printf("Enter a line: ");
    	/*in C you would use fgets*/
    	fgets(line,BUFSIZE,stdin);
    
    	printf("You entered: %s",line);
    
    	return(0);
    }
    Last edited by AndrewHunter; 09-15-2011 at 09:59 PM. Reason: Too slow for Laser
    hqt likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input a string in gcc
    By yogicoder in forum C Programming
    Replies: 5
    Last Post: 06-08-2008, 07:10 AM
  2. String input
    By -JM in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2005, 12:53 PM
  3. About string input in C++
    By KingZoolerius66 in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 07:54 PM
  4. get input string
    By m_g in forum C Programming
    Replies: 7
    Last Post: 02-23-2003, 10:11 PM
  5. CGI input string.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-13-2002, 11:55 AM

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