[C++] howto put a stringline in a string var

This is a discussion on [C++] howto put a stringline in a string var within the C++ Programming forums, part of the General Programming Boards category; Hello people, First I would like to say that I am Dutch. so if my English Grammar is terrible or ...

  1. #1
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55

    [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:

    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,

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,817
    Well, you have a stray word in your source code ("position."), and it looks like the plntxtlenght variable is unnecessary, and the plainchars variable does not exist.

    Perhaps you could use this as a starting point:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string plaintext = "abcdefghijklmnopqestuvwxyz";
    
        cout << plaintext.length() << endl;
    
        cin.get(); // waits for keypress event to exit.
    }
    If you want to read the text from a line from the user, consider using the version of std::getline() for std::string, e.g.,
    Code:
    getline(cin, plaintext);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    141
    stringlenght.cpp:19: fout: ‘struct std::string’ has no member named ‘lenght’
    You also spelled length wrong. It looks like a typo.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    plntxtlenght
    It may not seem to matter (and it certainly doesn't to the compiler), but spelling variables right is very useful to make sure that others can modify/add to your existing code.

    Length ends with th, not ht.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    mmhzz indeed i did a lot of typos :S sorry about that. All of your comment are very usefull i think, Thanks . now Im gonna sleep (at this moment it is midnight in Holland :P)

    I will report if i make some progress

    Bye

    Jelte

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  2. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  3. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM
  4. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21