Суть задания в том, чтобы вывести на экран предложение с заданным количеством слов со строки, введенной с клавиатуры
программу я написала, но не выводит то, что нужно. уверена, что есть глупая ошибка, но в упор ее не вижу
вот код:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i=0, k=0;
string s;
int a;
getline(cin, s);
cin>>a;
for(i=0;i=115;i++)
{
while((s[i]!='.')||(s[i]!='!'))
{
if ((s[i]==' '))
{
k++;
}
}
if (k==a)
{
for(i;;i++)
{
cout<<s[i];
}
}
else
{
k=0;
}
}
return 0;
}
C/C++
Помощь с написанием программы
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <regex>
using namespace std;
void punct(vector<string>& tokens, const string& text) {
for (auto& token : tokens) {
auto pos = text.find(token);
if (pos != string::npos) {
token += text[pos + token.length()];
}
}
}
void trim(string& line) {
line = regex_replace(line, regex(" +"), " ");
line.erase(0, line.find_first_not_of(" "));
line.erase(line.find_last_not_of(" ") + 1);
}
void trim(vector<string>& tokens) {
for (auto& token : tokens) trim(token);
}
vector<string> split(const string& text) {
const regex re(R"([.!?]+)");
const sregex_token_iterator iter{ text.begin(), text.end(), re, -1 };
vector<string> box = { iter, {} };
punct(box, text);
trim(box);
return box;
}
vector<vector<string>> split(const vector<string>& tokens) {
vector<vector<string>> box;
for (const auto& token : tokens) {
istringstream iss(token);
string word;
vector<string> words;
while (iss >> word) words.emplace_back(word);
box.emplace_back(words);
}
return box;
}
int main() {
cout << "Text: ";
string text;
getline(cin, text);
auto tokens = split(text);
auto words = split(tokens);
cout << "Words: ";
size_t count;
cin >> count;
for (auto i = 0U; i < words.size(); ++i) {
if (count == words[i].size()) {
cout << tokens[i] << '\n';
}
}
}
#include <string>
#include <sstream>
#include <vector>
#include <regex>
using namespace std;
void punct(vector<string>& tokens, const string& text) {
for (auto& token : tokens) {
auto pos = text.find(token);
if (pos != string::npos) {
token += text[pos + token.length()];
}
}
}
void trim(string& line) {
line = regex_replace(line, regex(" +"), " ");
line.erase(0, line.find_first_not_of(" "));
line.erase(line.find_last_not_of(" ") + 1);
}
void trim(vector<string>& tokens) {
for (auto& token : tokens) trim(token);
}
vector<string> split(const string& text) {
const regex re(R"([.!?]+)");
const sregex_token_iterator iter{ text.begin(), text.end(), re, -1 };
vector<string> box = { iter, {} };
punct(box, text);
trim(box);
return box;
}
vector<vector<string>> split(const vector<string>& tokens) {
vector<vector<string>> box;
for (const auto& token : tokens) {
istringstream iss(token);
string word;
vector<string> words;
while (iss >> word) words.emplace_back(word);
box.emplace_back(words);
}
return box;
}
int main() {
cout << "Text: ";
string text;
getline(cin, text);
auto tokens = split(text);
auto words = split(tokens);
cout << "Words: ";
size_t count;
cin >> count;
for (auto i = 0U; i < words.size(); ++i) {
if (count == words[i].size()) {
cout << tokens[i] << '\n';
}
}
}
Ошибка во вложенном цикле while, который зацикливается из за отсутствия приращения переменной " i " внутри него. У вас идет главный цикл for, который задает переменную i. Далее идет while (s[i]...) {....}. Внутри него i неизменна, а значит условие будет всегда true.
Похожие вопросы
- Помощь с написанием программы
- C++.Нужна помощь в написании кода.
- Помогите с написанием программы в компиляторе на С++
- Нужна помощь с написанием кода на языке "С"
- Программирование C++, написание программ
- Помогите с написанием программы в С++ (Пожалуйста!)
- Написание программы на c++
- Написание программы C++ Массивы
- Прошу, помогите с написанием программы на С++
- Нужна помощь в написании кода С++, пожалуйста.(Структуры)
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i=0, k=1;
string s;
int a;
getline(cin, s);
cin>>a;
for(i=0;i=116;)
{
while((s[i]!='.')||(s[i]!='!'))
{
if ((s[i]==' '))
{
k++;
}
i++;
}
if (k==a)
{
cout<<s[i];
i++;
}
else
{
k=0;
i++;
}
}
return 0;
}
#include < sstream >
using namespace std;
int main()
{
string s;
short n;
getline(cin, s);
cin >> n;
for (int i = 0; i < s.length(); i++)
{
if (s[i] == '!' || s[i] == '.') break;
if (s[i] == ' ' && i != 0 && s[i - 1] != ' ') n--;
if (!n) break;
cout << s[i];
}
}