Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3272862
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:52:15+00:00 2026-05-17T18:52:15+00:00

#include<iostream> #include<fstream> #include<cstdlib> #include<string> using namespace std; **int main() { double write(); double read();

  • 0
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>

using namespace std;

**int main()
{
    double write();
    double read();
    string choice;

    while(1)
    {
      cout<<"Enter read to read a file and write to write a file.\n";
      cin>>choice;
      if (choice == "read")
        cout<< read();
      if (choice == "write")
        cout<< write();
    }
}

double read()
{    
    const int size = 60;
    ifstream inFile;
    char filename[size];
    cout<<"Enter the name of the file you want to read \n";
    cin.getline(filename, size);
    inFile.open(filename);**

    if(!inFile.is_open())
    {
      cout<<"could not open  "<<filename<<endl<<"program terminating";
      exit(EXIT_FAILURE);
    }

    double value;
    double sum = 0.0;
    int count = 0;
    inFile >> value;
    while(inFile.good()) 
    {
      cout<<value<<"\n";
      ++count;
      sum += value;
      inFile >> value;
    }

    if (inFile.eof())
      cout<<"End of file reached. \n";
    else if (inFile.fail())
      cout<<"Input Terminated by data mismatch.\n";
    else
      cout<<"input terminated for unknown reason";

    if (count == 0)
      cout<<"no data processed";
    else 
    {
      cout<<"Items read: "<<count<<endl;
      cout<<"Sum: "<<sum<<endl;
      cout<<"Average: "<<sum / count << endl;
    }
    inFile.close();
    return 0;
}

double write() 
{
    char type[81];
    char filename[81];

    cout<<"this program is more or less pointless, any text edtor on earth is better than this for writing"<<endl;
    cout<<"files; However This is the first step in learning how to create my file tranfer program."<<endl;
    cout<<"Enter the name of a file you want to create.\n";
    cin>>filename;
    ofstream outFile;
    outFile.open(filename);

    outFile<<fixed;
    outFile.precision(2);
    outFile.setf(ios_base::showpoint);
    while(!cin.fail()){
         cin.getline(type,81);
         outFile<<type<<endl;
    }

    outFile.close();
}

The problem seems to be that when I type in “read” the program does what its supposed to until it gets to cin>>filename; at which point I think it assigns the value of choice to filename because the program skips to if(!inFile.is_open()){ after I type in “read”.(the write function of my program works fine.

could someone please tell me how to solve this problem or another way for the computer to decide weather to choose from functions read or write based on text input.

I am new to C++ so I would appreciate it if the answer is simple, thanks.

P.S. I,m on ubuntu if that makes a difference.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:52:15+00:00Added an answer on May 17, 2026 at 6:52 pm

    You just have to reconcile your use of “cin>>” versus “getline” when you want to read in a string. Their behavior is slightly different.

    Remember that when using cin to read from the console, every keystroke that the user touches goes into the cin buffer including the \n for the return key.

    So when your user types in “read” and hits return to enter their choice, the contents of cin at that moment are:

    read\n

    Then you do:

    string choice;
    cin >> choice;
    

    The problem is that the >> operator is defined to read up until it hits whitespace, so it will read in “read”, but it will leave \n still sitting in the cin buffer.

    So after your cin >> choice statement, the contents of cin are:

    \n

    Then you go to do this:

    char filename[size];
    cout<<"Enter the name of the file you want to read \n";
    cin.getline(filename, size);
    

    The problem here is that the >> operator is defined to read up until whitespace (like \n), but getline is defined to read up until it hits a \n and it will read everything on the line up until it sees a \n and then also read in the \n and discard it.

    Well your issue is that when you get to your cin.getline(filename,size) statement, there’s still a \n character sitting in the cin buffer and so getline immediately sees the \n and discards it, storing the empty string into filename.


    You have a few choices here.

    If you made filename a string and changed your filename read to:

    cin >> filename;
    

    It will work properly because the >> operator also ignores leading whitespace.

    You could also change your initial read of choice to be like this (using getline from the string header here):

    string choice;
    getline(cin,choice);
    

    And that will work because getline won’t leave the stray \n character sitting in the cin buffer.

    You could use the cin.ignore function to wipe the cin buffer clean. You don’t want to just blindy use ignore if you don’t know what you’re doing, but we know in this case that you’re leaving a stray \n on cin, so if you want to get rid of it, you can do this:

    #include <limits>
    
    string choice;
    cin >> choice;
    cin.ignore(std::numeric_limits<int>::max(),'\n');
    

    That ignore statement essentially wipes the remaining data in cin by saying “discard the next INT_MAX characters or everything up until and including a \n.”

    Hope that clears up what’s going on for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> using namespace std; int main() { double u = 0; double w
#include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000,
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
New code.... #include <cstdlib> #include <iostream> #include <fstream> #include <iomanip> using namespace std; void
Say I do this (a contrived example): #include <iostream> #include <fstream> using namespace std;
#include<iostream> using namespace std; class A { int a; int b; public: void eat()
#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
I have the following code: //Comp454 program 2 #include <iostream> #include <string> #include <fstream>
#include iostream class A { private: int a; public : A(): a(-1) {} int
#include iostream #include vector class ABC { }; class VecTest { std::vector<ABC> vec; public:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.