Answer to Question #180847 in C++ for Ramakrishna Reddy

Question #180847

 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:55-0400
#include <bits/stdc++.h>
#include <fstream>
using namespace std;

int main(){


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


    strcpy(arr, input.c_str());
    //Convert the string to array
    for (int j = 0; j < input.length(); j++){
        //Exclude empty characters
        if(arr[j] != ' '){
            count ++;
        }

    }


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


    // Close the file after writing
    fp.close();
    cout <<"Content saved successfully."<<endl;
    cout<<"The number of characters are stored is: "<<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