Rozkład lini tekstu na wyrazy i sprawdzanie i długości

0

Chcę rozłożyć linie tekstu zebraną z 5 wyrazów na wyrazy i sprawdzić ich długość. Mam taki kod

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
using namespace std;
int main(){
		string line;
ifstream ana ("anagram.txt");
if (ana.is_open())
  {
    while (ana.good() )
    {
      getline(ana,line);
	  char * pch,* cstr;
	  int length,l=0;
	  cstr = new char [line.size()+1];
  strcpy (cstr, line.c_str());
	  pch = strtok (cstr," ,.-");
  while (pch != NULL)
  {
	 
if (l==0){ length=pch.size();
l++;}
else {
	if(length==pch.length()){
    pch = strtok (NULL, " ,.-");
	l++;
	}
	else {break;}}

}
  if (l==5) cout<<line<<endl;
  
    }
	ana.close();
}	 else cout << "Unable to open file"; 


getch();}

Wyrzuca ma takie bład w

error C2228: left of '.length' must have class/struct/union
1> type is 'char *'

Wie ktoś może jak naprawić ten błąd??

0

pch to c-string - on nie ma metod, to zwykła tablica. Użyj strlen do obliczania jego długości.

0

pch jako zmienna typu znakowego nie posiada metody length(). Taką metodę posiadaja klasa std::string</code>. Czegoś w Twoim kodzie nie rozumiem. Dlaczego zamiast używać przyjemnej i funkcjonalnej klasy std::string operujesz na tablicach znaków? Zostań przy stringach, przeczytaj o metodach <code>substr() i find().

0

Po wczytaniu linii:

  1. Po pierwsze użyj funkcji Trim do obcięcia spacji z przodu i z tyłu
  2. Zamień wszystkie znaki interpunkcyjne (jak np.: ,.?!()<>+= ...) na pusty znak jak ""
  3. na tak obrobionym stringu zastosuj funkcję split() - jako znak rozdzilający zapisz spację (" ")
  4. a później już na elementach tablicy, którą zwróci Ci funkcja Split stosujesz funkcję Len() do obliczenia długości

Mogłem nieznacznie pomylić nazwy funkcji, bo pisze z pamięci ;)

0
int main()
  {
   ifstream ana("anagram.txt");
   string line;
   while(getline(ana,line))
     {
      stringstream ss(line);
      string tb[5];
      for(int i=0;i<5;++i) ss>>tb[i];
      if(ss) for(int i=0;i<5;++i) cout<<tb[i]<<" "<<tb[i].length()<<endl;
      else cout<<"cos nie tak z wierszem: "<<line<<endl;
     }
   cin.sync();
   cin.get();
   return 0;
  }
0

for(int i=0;i<5;++i) ss>>tb[i];
if(ss) for(int i=0;i<5;++i) cout<<tb[i]<<" "<<tb[i].length()<<endl;

Mógłby ktoś mi wyjaśnić metode działania tych fukcji?

po wrzucenie tego programu na gorze mam taka mase błedów

i:\vs\matura\matura\matura.cpp(14): error C2079: 'ss' uses undefined class 'std::basic_stringstream<_Elem,_Traits,_Alloc>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1>i:\vs\matura\matura\matura.cpp(14): error C2440: 'initializing' : cannot convert from 'std::string' to 'int'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>i:\vs\matura\matura\matura.cpp(16): error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'int'
1> i:\msvs\vc\include\string(458) : see declaration of 'std::operator >>'
1>i:\vs\matura\matura\matura.cpp(16): error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &&' from 'int'
1> i:\msvs\vc\include\string(344) : see declaration of 'std::operator >>'
1>i:\vs\matura\matura\matura.cpp(16): error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &&,_Ty &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &&' from 'int'
1> i:\msvs\vc\include\istream(1128) : see declaration of 'std::operator >>'
1>i:\vs\matura\matura\matura.cpp(16): error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'int'
1> i:\msvs\vc\include\istream(1118) : see declaration of 'std::operator >>'
1>i:\vs\matura\matura\matura.cpp(16): error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'int'
1> i:\msvs\vc\include\istream(1111) : see declaration of 'std::operator >>'
1>i:\vs\matura\matura\matura.cpp(16): error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'int'
1> i:\msvs\vc\include\istream(1104) : see declaration of 'std::operator >>'
1>i:\vs\matura\matura\matura.cpp(16): error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'int'
1> i:\msvs\vc\include\istream(1097) : see declaration of 'std::operator >>'
1>i:\vs\matura\matura\matura.cpp(16): error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'int'
1> i:\msvs\vc\include\istream(1090) : see declaration of 'std::operator >>'
1>i:\vs\matura\matura\matura.cpp(16): error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem *)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'int'
1> i:\msvs\vc\include\istream(1082) : see declaration of 'std::operator >>'
1>i:\vs\matura\matura\matura.cpp(16): error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &&,unsigned char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &&' from 'int'
1> i:\msvs\vc\include\istream(1074) : see declaration of 'std::operator >>'
1>i:\vs\matura\matura\matura.cpp(16): error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &&,unsigned char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &&' from 'int'
1> i:\msvs\vc\include\istream(1067) : see declaration of 'std::operator >>'
1>i:\vs\matura\matura\matura.cpp(16): error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &&,signed char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &&' from 'int'
1> i:\msvs\vc\include\istream(1060) : see declaration of 'std::operator >>'
1>i:\vs\matura\matura\matura.cpp(16): error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &&,signed char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &&' from 'int'
1> i:\msvs\vc\include\istream(1053) : see declaration of 'std::operator >>'
1>i:\vs\matura\matura\matura.cpp(16): error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &&,_Elem &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &&' from 'int'
1> i:\msvs\vc\include\istream(1028) : see declaration of 'std::operator >>'
1>i:\vs\matura\matura\matura.cpp(16): error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &&,_Elem *)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &&' from 'int'
1> i:\msvs\vc\include\istream(987) : see declaration of 'std::operator >>'
1>i:\vs\matura\matura\matura.cpp(16): error C2677: binary '>>' : no global operator found which takes type 'std::string' (or there is no acceptable conversion)
1>

0

Dołącz pliki:
string
fstream
iostream

0

Mam dołączone i mam ort! pierwszy bład wystepuje przy tworzeniu stringa ss

1 użytkowników online, w tym zalogowanych: 0, gości: 1