Final Codes to remember Flashcards
Write Data to a File(Names)
include <iostream></iostream>
#include<fstream>
using namespace std;</fstream>
int main()
{
offstream outputFile;
outputFile.open(“demofile.txt”);
cout«“Now writing data to the file”;
//four names to the file
outputFile«“Bach\n”;
outputFile«“jessica\n”;
outputFile«“sea\n”;
outputFile«“audrey\n”;
//close the file
outputFile.close();
cout«“Done.”
return 0;
}
write user input to file
include <iostream></iostream>
#include<fstream>
using namespace std;</fstream>
int main()
{
int number1,number2,number3;
offstream outputFile;
outputFile.open(“Numbers.txt”);
cout«“Enter a number”;
cin»number1;
cout«“Enter a number”;
cin»number2;
cout«“Enter a number”;
cin»number3;
outputFile«number1«endl;
outputFile«number2«endl;
outputFile«number3«endl;
cout«“All numbers are saved in file.”;
outputFile.close();
cout«“Done.”
return 0;
Write user input strings
include <iostream></iostream>
#include<fstream>
#include <string>
using namespace std;</string></fstream>
int main()
{
string name1,name2,name 3;
offstream outputFile;
outputFile.open(“Friends.txt”);
cout«“Enter friend name”;
cin»name1;
cout«“Enter 2nd friend name”;
cin»name2;
cout«“Enter 3rd friend name”;
cin»name3;
outputFile«name1«endl;
outputFile«namer2«endl;
outputFile«name3«endl;
cout«“All names are saved in file.”;
outputFile.close();
cout«“Done.”
return 0;
readuser input string code
include <iostream></iostream>
#include<fstream>
#include <string>
using namespace std;</string></fstream>
int main()
{
string name;
ifstream inputFile;
inputFile.open(“friends.txt”)
cout«“Reading data from the file.”;
inputFile»name;
cout«name«endl;
inputFile»name;
cout«name«endl;
inputFile»name;
cout«name«endl;
inputFile.close();
cout«“Done.”
return 0;
write sales using for loop
include <iostream></iostream>
#include<fstream>
using namespace std;</fstream>
int main()
{
offstream outputFile;
outputFile.open(“Sales.txt”);
int numberofDays;
double sales;
cout«“For how many days do you have sales”;
cin»numberofDays;
for(int count=1;count<=numberofDays; count++)
{
cout«enter the sales for day”
«cout«”:”;
cin»sales
outputFile«sales«endl;
}
outputFile.close();
cout«“All numbers are saved in file.”;
return 0;
}
read determine if file exist
include <iostream></iostream>
#include<fstream>
#include <string>
using namespace std;</string></fstream>
int main()
{
string filename;
int number;
cout«“enter the file Name”;
cin»filename;
ifstream inputFile;
inputFile.open(filename.c_str());
if(inputFile)
{
while(ninputeFile»number)
{
cout«number«endl;
}
inputFile.close();
}
else
{
cout«“error opening the file”;
}
return 0;
}
Write Array to file
include <iostream></iostream>
#include<fstream>
#include <ctsdlib>
#include <ctime>
using namespace std;</ctime></ctsdlib></fstream>
int main()
{
unsigned seed=time(0);
srand(seed);
const int Array_Size=10;
int number[Array_Size];
int count;
ofstream.outputFile;
outputFile.open(RandomNumbers.txt”);
for(count=0;count <Array_Size; count++)
outputFile«number[count]«endl;
outputFile.close();
cout«“The numbers were saved to the file.”;
return 0;
}
read number from file
include <iostream></iostream>
#include<fstream>
using namespace std;</fstream>
int main ()
{
int value1,value2,value3,sume;
ifstream.inFile;
inFile.open(“Numbers.txt”);
inFile»value1;
inFile»value2;
inFile»value3;
infile.close();
sum=value1+value2+value3;
cout«“Here are the numbers:”
«value1«” “ «value2«” “«value3«endl;
cout«“Their sum is””«sum<,endl;
return 0;
recurrsion steps
include <iostream></iostream>
using namespace std;
void message(int);
int main()
{
message(5);
cout«””;
return 0;
}
void message(int times)
{
if(times>0)
{
cout«times«””;
message(times-1);”
}
recurusion factorial
include <iostream></iostream>
using namespace std;
int factoria(int);
int main()
{
int number;
cout«“Enter an interger value and I will display”:
cout«“it’s factorial:”;
cin»number;
cout«“The factorial of “”«_space;number«“is”;
cout«factorial(number)«endl;
return 0;
}
int factorial(int n)
{
if(n==0)
return 1; //base case
else
return n=factorial(n-1); //recursive case
}
recursive binary search
include <iostram></iostram>
using namespace std;
int binarySearch(int[],int,int,int);
int main()
{
int test[20] numbers go here 20 of them
int empID;
int results;
cout«“Enter the the employee id you wish to search for:”;
cin»empID;
results=binarySearch(tests,0,Size-1,empId);
if(result==-1)
cout«“That number does not exisit in the array.”;
ellse
cout«“That id is not found at eleement”«results;
return 0;
}
int binarySearch(ont array[],int first,int last,int value)
{
int middle;
if(first>last)
return-1;
middle=first+last/2;
if(array[middle]==value)
return middle;
if(array[middle]<value)
return binarySearch(array,middle,last,value);
else
return binarySearch(array,first,middle-1,value);
}
struct payroll
struct PayRoll
{
int empNumber;
string name;
doule hours;
double payRate;
double grossPay;
};
int main()
{
payRoll employee;
cout«“Enter the employee’s number:”
cin»employee.empNumber;
cout«“Enter the employee’s name”;
cin.ignore();
getline(cin,employee.name);
cout«“How mauny hours did the employee work?”;
cin»emplpyee.hours;
cin»employee.payRate;
employee.grossPay=employee.hours=employee.payRate;
cout«“here is the employee’s payroll data:”;
cout«“name”«employee.name«endl;
cout«“Number:”«employee.empNumber«endl;
cout«“hour’s worked:”«employee.hours«endl;
cout«“hourly Payrate:”«employee.payRate«endl;
cout«fixed«showpoint«setprecision(2);
cout«gross pay:$«employee.grosspay«endl;
return 0;
}
structure circle
include <iostream)
#include cmath
#include<iomanip>
using namespace std;</iomanip>
const double Pi=3.14159
struct Circle
{
double radius;
double diameter;
double area;
};
int main()
{
Circle c:
cout«Enter the diameter of a circle”;
cin»c.diameter/2
c.area=PI*pow(c.radius, 2.0);
cout«fixed«showpoint«setprecision(2);
cout«“The radius and area of the circle are:”;
cout«
radius:”«c.radius«endl;
cout«“Area:«c.area«endl;
return 0;
}
WriteReadArraysOfStructures
include <iostream)
#include cmath
#include<iomanip>
using namespace std;</iomanip>
struct payInfo
{
int hours;
double payRate;
};
int main()
{
const int Num_Workers=3;
PayInfo workers(Num_Workers);
int index;
cout«“Enter the hours worked by”«Num_workers«“empolyess and their hourly rates.\n;
for(index=0;index<Num_Workers; index++)
{
cout«“Hourly worked by employee #”;
cout«(index+1)«”:”;
cin»worker(index).payRate;
cout«endl;
cout«“Here is the gross pay for each employee\n”
cout«fixed«showpoint«setprecision(2);
for(index=0;index <Num_worker; index++)
{
double gorssl
gross=workers[index].hours*worker[index].payRate;
cout«“Employee #”«(index+1);
cout«”:$”«gross«endl;
}
return 0;
}