read an entire line in a C++ string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • debjani
    New Member
    • Sep 2007
    • 2

    read an entire line in a C++ string

    hi ..
    suppose there is a line at the command prompt with variable number of spaces . How can i scan it in a C++ string ??
    a) cin cannot be used as it cannot scan spaces ...and scanf,gets cannot be used as it is a C++ string
    If the line was in a file , i could have used
    ifstream in("input.txt") ;
    int main(){
    string line
    getline(in,line );
    ------------------
    }
    but if the line is at the command prompt then how do i scan it ??
    debjani
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Okay, I'm not entirely sure what you're asking here. If you are talking about a 'console' window, or the 'command line'.

    The first one is the input/output window, the default cin/cout window. You can get the line by using

    Code:
    getline(cin, line);
    and specifying 'cin' as the source will allow you to pull that whole line.

    However, if you are talking about the command line - where the program is instantiated, it's different. I'm using gcc, so my program binary is compiled as a.out, so as an example (so the ./a.out is me executing my program):

    myPrompt# ./a.out paramater1 paramater2 paramater3

    where you want paramaters[1-3], you have to "build them into a program" and have your main declaration look like this:

    Code:
    int main (int argc, char *argv[])
    Then you know how many parameters are there, and they are stored in the char* argv[] slots.

    Is that clear, or have I confused you on something?

    Comment

    • debjani
      New Member
      • Sep 2007
      • 2

      #3
      no ..
      that is fine ...
      i got it ...
      thanks !!!!

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        No problem, please post again if you get stuck.

        Comment

        • Ani123
          New Member
          • Nov 2008
          • 1

          #5
          Originally posted by sicarie
          No problem, please post again if you get stuck.
          Here i am facing the same issue but i dont want to use gcc then how we can accompolish the task for windows command prompt ?

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Everything in this thread is standard C++ and will work with any C++ compiler, you are not limited to gcc

            Comment

            Working...