Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Linux
Can anyone help me in inputting strings on gcc compiler?
i want to input a string using string class
i used getline() but it didn't work.
what can i use to do so?
and also i want to input lines of strings to calculate no. of lines
so how can i do that?
i m new to gcc compiler please tell me what can i do?
thanks in advance....
 
Here is the sample code my original code is in my college i used the functions as follows:
using namespace std;
#include "iostream"
#include "string"
void temp()
{
    string s;
    getline(cin,s);//this didn't worked, it was skipped
    cout<<s; 
}
 
int main()
{
        string s;
        getline(cin,s);   //This function worked
        temp();
        return 0;
}
this is just the sample code when i call getline(cin,s) inside main() it works and allow me to input a sentence
but problem is when i call it inside any function other than main() it doesn't work.
Posted 17-Feb-13 23:15pm
Edited 18-Feb-13 23:21pm
v4
Comments
Fredrik Bornander at 18-Feb-13 5:50am
   
Show us how you used getline() so that we can tell you why it didn't work.
enhzflep at 18-Feb-13 7:00am
   
If you want to use the std::string class, you should be using cin.
I.e
string yourStringVar;
cout << "Enter a string: ";
cin >> yourStringVar;
cout << "you entered: " << yourStringVar;
Member 8536324 at 18-Feb-13 9:58am
   
it accepts only one word sir
Member 8536324 at 18-Feb-13 9:56am
   
Sir, I used getline like getline(cin,Stringvariable);
Member 8536324 at 18-Feb-13 9:58am
   
Actually it worked but only in main() if i use it inside any member function of class it doesn't work and not even in member functions but also in non-member functions it didn't work.
YvesDaoust at 18-Feb-13 12:24pm
   
A silly question: did you ever... call these functions or methods ?
And if yes, did you ever... pass some arguments ?
Member 8536324 at 19-Feb-13 5:17am
   
Yes i called those functions but the getline() was skipped at that time
YvesDaoust at 19-Feb-13 5:21am
   
How do you know it was skipped ?
Member 8536324 at 19-Feb-13 5:22am
   
sir it didn't ask me to input the string
YvesDaoust at 19-Feb-13 5:28am
   
As far as I know, getline does not "ask" anything. It is an input-only function and does not emit any message. What makes you think the behavior differs when called from main() ?
Member 8536324 at 19-Feb-13 5:30am
   
i know sir it does not emit any message but it can be used for input as i used that statement was skipped
Member 8536324 at 19-Feb-13 5:31am
   
suppose if i write like
cout<<"Enter any string";
getline(cin,s); //this statement was skipped
Member 8536324 at 19-Feb-13 5:32am
   
i don't know what happened but the getline was skipped when i call it inside any function. you can try it
Member 8536324 at 19-Feb-13 5:24am
   
i am just trying to input the string inside the function and display it there in that function only
Member 8536324 at 19-Feb-13 5:25am
   
ok is there any other way that i can use to input the string?
i used cin but it accepts only one word
YvesDaoust at 19-Feb-13 5:31am
   
You should use the third argument to getline and specify the newline character as the string delimiter, otherwise space will be taken by default.
Member 8536324 at 19-Feb-13 5:33am
   
ok and what if i want to input lines of strings
Member 8536324 at 19-Feb-13 5:34am
   
this will allow me to input only one line
Maximilien at 18-Feb-13 11:14am
   
Show us the code
(edit your original question).
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Your function temp accepts a string into a temporary variable. Then, when it returns to main the temporary variable s goes out of scope and is destroyed. You code should be:
string temp()
{
    string s;
    getline(cin,s);//this didn't worked, it was skipped
    return s;
}
 
int main()
{
    string s;
    s = temp();   //This function should now work
    return 0;
}
  Permalink  
Comments
Member 8536324 at 19-Feb-13 5:35am
   
ok i will try
thanks a lot
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Use getline(cin, s, '\n');
 
do
{
    getline(cin, s, '\n');
 
    // Process the line
    ....
}
while (s.length() > 0);
  Permalink  
v2
Comments
Member 8536324 at 19-Feb-13 5:35am
   
thanks a lot sir
Member 8536324 at 19-Feb-13 5:35am
   
i will try

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 6,212
1 OriginalGriff 5,498
2 CPallini 2,473
3 Richard MacCutchan 1,627
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100