Thread: Istringstreams and string manipulation.

  1. #1
    Registered User
    Join Date
    Apr 2012

    Thumbs down Istringstreams and string manipulation.


    My main problem currently is what to do exactly for this program. I have no idea on where to go or what to add or change. I am trying to read information from a file, and then change the format of the information. For example, I need to change the name format to have the middle name have just the first letter and a period. Such as "Chris Jones Johnson" to become "Johnson, Chris J." I also need to format time given as "10:10 PM" to military time. I also need to encrypt a message from the file. Here is the code I have so far. Please tell me what I need to delete or add or change.

    Thank you in advance,

    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <vector>
    #include <iomanip>
    using namespace std;
    string mtime(string time, ofstream &date);
    int main() {
        ifstream blog;
        ofstream outblog;
        char file[30];
        char o, e, i;
        int key, b;
        string originalChar, echar, tempchar    ;
        istringstream Name, Date, Code;                //Used to sort data from the file
        string fname, lname, mname, time, name[3];
        cout << "Please enter the name of a blog file:" << endl;
        cin >> file;
        cout << "Please enter an encryption key between 1 and 100" << endl;        //Used to encrypt the file
        cin >> key;
        if (key < 1 || key > 100) {
            cout << "The key entered was not between 1 and 100" << endl;        //Makes sure the user inputs within the range
            return 0;
        if ( {            //Makes sure the file was opened sucessfully
        getline(blog, name[3]);    
        while (Name >> name[3]) {            //Supposed to read data into the given variables.
            Name >> fname >> mname >> lname;
            mname.substr(0, 2);
            mname.insert(1, ".");
        getline(blog, time);
        while (Date >> time)    {
            Date >> time;
            b = time.find(":");
            time.erase(b, 1);        
            time.insert(b, " ");
        getline(blog, originalChar);
        Code >> originalChar;
        for (i = 0; i < originalChar.length(); i++) {
            tempchar = originalChar;
        if ( + key > 126) {        //Encrypts the message from the blog file.
   = 32 + ((o + key ) - 127);
        else {
   = (o + key);
        echar = originalChar;
        cout << echar << endl;
    string mtime(string time, ofstream &date) {    //Switches around the time format to military time
        while (date << time) {
            int b, hours, minutes;                        
            string Day;
            b = mtime.find(":");
            mtime.erase(b, 1);        
            mtime.insert(" ");        
            mtime >> hours >> minutes >> "hours";
            if (time.find("PM")) {
                hours = hours + 12;
            istringstream ntime;
            date << setw(2) << hours << minutes << Day;
            cout << date;

  2. #2
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    First try to implement everything as best as you can. Once you get stuck somewhere at that point then post here for advice. Beyond that, I would say put everything into functions; modular code is much easier to understand and maintain. Also, NEVER use the "exit" function in a C++ program; it undermines the proper destruction of objects. Instead, just throw an exception; if it get's caught somewhere up the line, fine. Otherwise, the program will just terminate BUT your destructors will still be invoked properly, so all will be well.

    Good luck!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with string manipulation.
    By newbc in forum C Programming
    Replies: 5
    Last Post: 02-08-2011, 03:36 PM
  2. string vector to string pointer manipulation
    By stanlvw in forum C++ Programming
    Replies: 11
    Last Post: 07-16-2008, 01:43 AM
  3. string manipulation
    By apsonline in forum C++ Programming
    Replies: 7
    Last Post: 08-30-2006, 04:22 AM
  4. Some help with string manipulation
    By InvertedSaint in forum C Programming
    Replies: 2
    Last Post: 08-09-2006, 06:32 AM
  5. String manipulation
    By casanova0o7 in forum C++ Programming
    Replies: 2
    Last Post: 01-23-2002, 10:45 PM

Tags for this Thread