Answer to Question #180759 in C++ for RAMAKRISHNA

Question #180759

Write a program to permanently store the data entered by the user. When user is done with Input, then print the confirmation message as "Content saved successfully" along with the number of character stored.


1
Expert's answer
2021-04-13T02:40:34-0400
#include <bits/stdc++.h>
#include <fstream>
using namespace std;


int main()
{


    string str = "";
    int count = 0;
    cout<<"Enter the string:\n";
    getline(cin, str);
    char arr[str.length() + 1];


    strcpy(arr, str.c_str());
    //String to char array conversion
    for (int i = 0; i < str.length(); i++)
    {
        //Avoid counting empty characters
        if(arr[i] != ' ')
        {
            count ++;
        }


    }


    // Create and open a text file
    ofstream file("save.txt");
    // Write string to the file
    file << str;


    // Close the file
    file.close();
    cout <<"Content saved successfully "<<endl;
    cout<<"The number of characters are: "<<count<<endl;


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS