Write a program to read a set of floating point numbers from user and save the n
ID: 3715639 • Letter: W
Question
Write a program to read a set of floating point numbers from user and save the numbers in a file (output.txt) formatted as shown below. After the file is saved, notify the user and ask if next set of numbers are to be entered. Repeat until user enters -1 for number count.
How many numbers to enter (Enter -1 to quit)? 5
Enter numbers: 1.5 0.23 5.55 2.56 3.7 Numbers saved to disk.
How many numbers to enter (Enter -1 to quit)? 2
Enter numbers: 10.2 33.6 Numbers saved to disk.
How many numbers to enter (Enter -1 to quit)? -1
output.txt 1.5,0.23,5.55,2.56,3.7 10.2,33.6
Note: Each number in output.txt should be separated by a comma symbol.
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream file;//creating variable to write to a file
file.open("output.txt",fstream::app);//opening file in apped mode
int c;
int l=0;
while(1)
{
int n;
//prompting input
cout<<"How many numbers to enter (Enter -1 to quit)?";
cin>>n;
if(n==-1)break;//breaking loop
float a[n];//creating array..of floating point numbers
//reading numbers
int i=0;
cout<<"Enter numbers: ";
while(i<n)
{
cin>>a[i];
if(l!=0){
file<<",";
}
file<<a[i];//writing to file..
i++;
l++;
}
cout<<"Numbers saved to disk. ";
l++;
}
file<<" ";
file.close();
return 0;
}
/*
input
How many numbers to enter (Enter -1 to quit)?5
Enter numbers: 1.5 0.23 5.55 2.56 3.7
Numbers saved to disk.
How many numbers to enter (Enter -1 to quit)?2
Enter numbers: 10.2 33.6
Numbers saved to disk.
How many numbers to enter (Enter -1 to quit)?-1
Process exited normally.
Press any key to continue . . .
output.txt
1.5,0.23,5.55,2.56,3.7,10.2,33.6
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.