C++中如何取得控制台的第某一行的第某个字符。
2个回答
展开全部
#include<iostream>
#include<string>
#include<vector>
#include<assert.h>
#define MAXWORDS 100
#define MAXLINES 100
using std::string;
using std::vector;
using std::cin;
using std::cout;
using std::endl;
int getlines(string *lines);
int main()
{
vector<string> lines;
for (int i = 0; i < MAXLINES; ++i){
cout << "输入一行回车结束,结束输入按EOF" << endl;
string s;
if (getlines(&s) == 0)
break;
lines.push_back(s);
}
unsigned int nlines, n; //第nlies行,第n个字
cout << "输入第n行第n个字母,中间空格" << endl;
cin >> nlines >> n;
assert(nlines <= lines.size() && n <= lines[nlines-1].size());//输入不合法用来报警的
cout << lines[nlines - 1][n - 1];
}
int getlines(string *lines)
{
char s[MAXWORDS];
int i;
for (i = 0; i < MAXWORDS && (s[i] = getchar()) != '\n' && s[i]!= EOF; ++i)
;
if (s[i] == '\n'){
s[i] = '\0';
*lines = s;
return 1;
}
else
return 0;
}
#include<string>
#include<vector>
#include<assert.h>
#define MAXWORDS 100
#define MAXLINES 100
using std::string;
using std::vector;
using std::cin;
using std::cout;
using std::endl;
int getlines(string *lines);
int main()
{
vector<string> lines;
for (int i = 0; i < MAXLINES; ++i){
cout << "输入一行回车结束,结束输入按EOF" << endl;
string s;
if (getlines(&s) == 0)
break;
lines.push_back(s);
}
unsigned int nlines, n; //第nlies行,第n个字
cout << "输入第n行第n个字母,中间空格" << endl;
cin >> nlines >> n;
assert(nlines <= lines.size() && n <= lines[nlines-1].size());//输入不合法用来报警的
cout << lines[nlines - 1][n - 1];
}
int getlines(string *lines)
{
char s[MAXWORDS];
int i;
for (i = 0; i < MAXWORDS && (s[i] = getchar()) != '\n' && s[i]!= EOF; ++i)
;
if (s[i] == '\n'){
s[i] = '\0';
*lines = s;
return 1;
}
else
return 0;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询