String help!!

This is a discussion on String help!! within the C++ Programming forums, part of the General Programming Boards category; i wrote this programme that will read a sentence from standard input and output the sentence with spacing corrected and ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    21

    Unhappy String help!!

    i wrote this programme that will read a sentence from standard input and output the sentence with spacing corrected and letters corrected for capitalization. in other words if the input sentence has spaces between words composed of more than one blank in the ouput sentence these spaces should be compressed into onto one single blank. the sentence should start with uppercase letter and then contain no other uppercase letter.
    But im not able to store the whole sentence?only one word is converted.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       
       
        string str;
       cin >> str;
        cout << "Before: " << str << endl;
    
        str[0] = toupper(str[0]);   // Capitalize first letter
        if (str[str.size()-1] != '.') str += '.';   // Add period at end of string
    
        for(size_t i = 1; i < str.size(); i++)
            str = tolower(str);   // Lower case all letters other than the first.
    
        cout << "After : " << str;
    
        return 0;
    
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    cin is only reading the first word. You have to use one of the getline() functions.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    21

    Thumbs down

    i did this now but still problems??i duno where the problem is??

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main()
    {
        
    	
    	string str;
    	getline(cin, str,'\n');
        cout << str << endl;
    
        str[0] = toupper(str[0]);   // Capitalize first letter
        if (str[str.size()-1] != '.') str += '.';   // Add period at end of string
    
        for(size_t i = 1; i < str.size(); i++)
            str = tolower(str);   // Lower case all letters other than the first.
    
        cout << "After : " << str;
    
        return 0;
    
    }

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code:
    str = tolower(str);   // Lower case all letters other than the first.
    Code:
     In function `int main()':
    17: error: no matching function for call to `tolower(std::string&)'
    note: candidates are: int tolower(int)
    Change to:

    Code:
    str[i] = tolower(str[i]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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