[C++] howto put a stringline in a string var
Hello people,
First I would like to say that I am Dutch. so if my English Grammar is terrible or you dont understand me please say it. ;)
I am working on teach myself basics of C++ with the beginners tutorials from Cprogramming.
I try to make a program that stores a string value and return the length of it. I succeeded this by handle strings, what is called "c-style"(using cstring header). but now I want to do it "C++ style" (using string header)because I think it is not necessary to declare a maximum to each string variable in this program.
I made this code:
Code:
#include <iostream> // for cout.
#include <string> // for string functions.
using namespace std;
int main()
{
position.
string plaintext; // will be stored in here.
int plntxtlenght; // the lenght of the plaintext-string will be stored here.
plaintext = "abcdefghijklmnopqestuvwxyz";
plntxtlenght = plainchars.lenght ; // reads the string of plaintext and store the length of characters in plntxtlenght.
cout<<plntxtlenght <<endl; // print the value of plntxtlenght in the screen.
cin.get(); // waits for keypress event to exit.
}
but my compiler(g++) return this error:
Quote:
stringlenght.cpp: In function ‘int main()’:
stringlenght.cpp:17: fout: no matching function for call to ‘std::basic_istream<char, std::char_traits<char> >::getline(std::string&)’
/usr/include/c++/4.3/istream:598: note: kandidaten zijn: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:409: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]
stringlenght.cpp:19: fout: ‘struct std::string’ has no member named ‘lenght’
jelte@jelte-desktop:/media/DATA/Jelte/Programming/C++/Projecten/Ceasar-rotatie/oefeningen/stringlenght$
jelte@jelte-desktop:/media/DATA/Jelte/Programming/C++/Projecten/Ceasar-rotatie/oefeningen/stringlenght$ g++ stringlenght.cpp -o stringlenght
stringlenght.cpp: In function ‘int main()’:
stringlenght.cpp:17: fout: no matching function for call to ‘std::basic_istream<char, std::char_traits<char> >::getline(std::string&)’
/usr/include/c++/4.3/istream:598: note: kandidaten zijn: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:409: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]
stringlenght.cpp:19: fout: argument of type ‘size_t (std::basic_string<char, std::char_traits<char>, std::allocator<char> >::)()const’ does not match ‘int’
I cant see what I am doing wrong?
Thanks in advance
Jelte,