在gcc编译器上输入字符串 [英] Inputting strings on gcc compiler

查看:415
本文介绍了在gcc编译器上输入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我在gcc编译器上输入字符串吗?

i希望使用字符串类输入字符串

i使用getline()但是它没用。

我可以用它来做什么?

我也想输入字符串来计算不行。线条

所以我怎么能这样做?

im gcc编译新手请告诉我该怎么办?

提前感谢.. ..



以下是我原来代码在我大学的示例代码我使用了以下函数:

using namespace std; < br $>

 #includeiostream
#includestring
void temp()
{
string s;
getline(cin,s); //这没有用,它被跳过
cout<< s;
}

int main()
{
string s;
getline(cin,s); //这个函数工作
temp();
返回0;
}



这只是我在main()中调用getline(cin,s)时的示例代码,它允许我输入一个句子

但问题是当我在main()以外的任何函数内调用它时它不起作用。

解决方案

你的函数 temp 将字符串接受为临时变量。然后,当它返回 main 时,临时变量 s 超出范围并被销毁。你的代码应该是:

 string temp()
{
string s;
getline(cin,s); // 这不起作用,它被跳过
return s;
}

int main()
{
string s;
s = temp(); // 此功能现在应该可以使用
return 0 ;
}


使用 getline(cin,s,''\ n'');



  
{
getline(cin,s,' \ n');

// 处理行
....
}
(s.length()> 0 );


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.

解决方案

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;
}


Use getline(cin, s, ''\n'');

do
{
    getline(cin, s, '\n');

    // Process the line
    ....
}
while (s.length() > 0);


这篇关于在gcc编译器上输入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆