Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Program output file stays empty, no results

+6 votes
asked Nov 9 by Dovilė Blažukienė (210 points)
Hello, i'm learning to use external files in c++. I've created file duom.txt for initial data and file ats.txt for results data. My simple program manages to read an initial number from duom.txt, but for some reason it does not make an output to results file ats.txt. Maybe i've ,made some error, i can't find it... Please help :)

here is my code:

#include <cmath>
#include <fstream>

using namespace std;

int main()
    float skaicius;
    float Saknis;
    ifstream fd ("duom.txt");
    fd >> skaicius;
    Saknis = sqrt (skaicius);
    ofstream fr ("ats.txt");
    fr << Saknis;
    return 0;

Console says:

Reading symbols from a.out...
(gdb) run
Starting program: /home/a.out
[Inferior 1 (process 3326) exited normally]

Thanks for help :)

1 Answer

0 votes
answered Nov 9 by Peter Minarik (63,320 points)
selected Nov 9 by Dovilė Blažukienė
Best answer
Your code works fine for me.

I copied your code into a new C++ project. Added a duom.txt for it and put some number in the file (256.0). Then ran the program, which created the ats.txt with the square root (16 in my case).
commented Nov 9 by Dovilė Blažukienė (210 points)
Thank you very much for an answer. I'm so very new at this :) today i've found at last, why i did not get this to work. I was debugging with Debug button and then Run button was inactive, i was just using Start at the bottom, near console and something went wrong then. If i debug, then Stop, and then Run, everything goes OK! Thanks again for your time. Cheers!
commented Nov 9 by Peter Minarik (63,320 points)
I'm glad you're sorted! :)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.