consider the following program

#include<iostream.h>
#include<fstream.h>

void main()
{
int no;
ifstream fin;
ofstream fout;
fout.open("create,dat",ios::app);
fout<<"What is your fav. number";
fin>>no;
fout<<no;
fout.close();
}

NOW HERE IS THE PROBLEM WHILE THE PROGRAM IS RUNNING PERFECTLY AND THE NUMBER IS BEING STORED IN A .DAT FILE BUT THE NUMBER IS NOT GETTING DISPLAYED IN THE NORMAL OUTPUT MODE (WHICH COMES UP WHEN YOU COMPILE AND RUN A PROGRAM,CTR+F9)

Also i dont want to write cout followed by fout because that will double the number of lines nd make my program redundant though i know that it will work but still i dont like it....

CAN ANYONE PLEASE HELP ME IMPROVE ME CODE SO THAT I CAN DISPLAY THE NUMBER IN THE OUTPUT MODE TOO(without making the program redundant and repetitive).....

Recommended Answers

All 2 Replies

You neglected to open fin.

I think you want something like so:

#include <iostream>
#include <fstream>
int main(){
  using namespace std;

  ofstream fileOutput("data.dat",ios::app); //open for appending
  cout << "What is your favorite number: "; //print to standard output the question
  int favNum; //create variable to store number
  cin >> favNum; //read response from user from standard input
  fileOutput << favNum; //save number into the file 
  return 0;
}

Notice I use the correct header as well the correct main function(i.e int main not void main)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.