Completed the program to read data from grades.txt file and store only positive number in positive.txt file.
Below is the implementation and output attatched.
(To run this program create grades.txt file and positive.txt file int the same directory where you place this program file)
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream grades;
ofstream output;
//Opening grades.txt and positive.txt files
output.open("positive.txt");
grades.open("grades.txt");
//if gradex.txt file fail to open
if (grades.fail())
{
cout << "Input file Couldn't open !" << '\n';
return 0;
}
//if positive.txt file fail to open
if (output.fail())
{
cout << "Output file Couldn't open !" << '\n';
return 0;
}
string line;
while (!grades.eof())
{
//getting each line from grades.txt
getline(grades, line);
//calculating and storing result in ans string if the number present in grades.txt is positive
string ans = "", sudo = "";
for (int i = 0; i < line.length(); i++)
{
//when we encounter ' ' in a line means a number is stored in variable sudo, after space (' ') another number starts
if (line[i] == ' ')
{
float num = std::stof(sudo); //stof function is used to convert string to float type.
if (num > -1)
{
ans += sudo + ' '; //append particular number to ans variable if num is positive
}
sudo = "";
}
sudo += line[i];
}
//for the last number in a line
float num = std::stof(sudo);
if (num > -1)
{
ans += sudo + ' ';
}
sudo = "";
//Storing result of each line from input file(grades.txt) in output file(positive.txt).
if (ans != "")
{
output << ans << '\n';
}
}
//Treminating the file handles
output.close();
grades.close();
}
Output :
Grades.txt

positive.txt

C++ programa use include<iostream> and #include<fstream> grades.txt is Heights.txt Write a C++ program to perform the...
#include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...
Intro C++ questions: Question 1: Assume #include <fstream> Given ifstream fpIn; Write a statement to open a file called carpools.txt and assign the pointer to fpIn. If the file cannot be found, then print out the message No such file and exit the execution of the program. Question 2: Meridian Corporation of Fremont is trying to decide whether to reward employees who carpool to work. The proposed plan is to pay each employee in the carpool $ 0.08 per...
The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...
Answer this in c++ #include <iostream> #include <fstream> #include <string> using namespace std; class Person { public: Person() { setData("unknown-first", "unknown-last"); } Person(string first, string last) { setData(first, last); } void setData(string first, string last) { firstName = first; lastName = last; } void printData() const { cout << "\nName: " << firstName << " " << lastName << endl; } private: string firstName; string lastName; }; class Musician : public Person { public: Musician() { // TODO: set this...
can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block { std::string word; int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) { string filename="input.txt"; //declare array of struct word_block word_block arr[SIZE]; int count = 0; if (argc < 2) { cout << "Usage: " << argv[0] << "...
How would answer question #4 of this ?
#1 and 2
#include<iostream>
#include <fstream>
using namespace std;
void read(int arr[])
{
string line;
ifstream myfile("input.txt");
int i = 0;
if (myfile.is_open())
{
while (getline(myfile, line))
{
arr[i];
i++;
}
myfile.close();
}
}
void output(int arr[])
{
ofstream myfile("output.txt");
if (myfile.is_open())
{
int i = 0;...
I need a really good Pseudo/Algorithm code for this C++ program below. #include <iostream> #include<fstream> using namespace std; int main() { int low, high, i;//integer varaible bool flag;//boolean flag string a = "Enter two numbers(intervals): ";//string datatype ofstream f;//fow writing to file f.open("a.txt"); cout << a; cin >> low >> high; cout << "Prime numbers between " << low << " and " << high << " are: "; do /*...
With basic (do not use #include <algorithm>, etc.) and simple C++ Write a program which reads a text file “input.txt” and stores all the distinct words in an array. A word consists of letters only - uppercase and/or lowercase. An incoming word should be inserted into the array such that it is always in ascending order. Use binary search to ensure that no duplicate words are added. Assume that there are no more than 100 distinct words. Assume that the...
Write a C program which is called ‘multiple_copy’. This program copies one source file to two destination files as follows: $./multiple_copy....... source_file....... destination_file1......... destination_file2 multiple_copy program copies the contents of source file (i.e., source_file) to any named two destination files in the command line. The number of arguments should be 4 including multiple_copy. If the number of arguments is not 4, the program should display error message saying: “Usage: multiple_copy source_file destination_file1 destination_file2”. When the source_file does not exist, the...
1.Write an assembly language program that corresponds to the following C++ program: #include <iostream> using namespace std; const int amount = 20000; int num; int sum; int main () { cin >> num; sum = numb + amount; cout << "sum = " ,<< sum << endl; return 0; } 2. Test the program in the previous question twice. The first time, enter a value for num to make the sum within the allowed range for the Pep/8 computer. The...