In: Computer Science
If you have any doubts, please give me comment...
Functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
/*reads from a file
and prints every item to the screen*/
// file contains sets of
//int
//string
//Don't forget to use infile.ignore() between << and getline
//ifstream must be passed by reference
void readAndPrint(ifstream &infile);
//uses a for loop to return the base to the exponent
//e.g. 2^4 = 16
// this fuction should read data.txt file
long power(long base, long exponent);
#endif
Functions.cpp
#include "Functions.h"
void readAndPrint(ifstream &infile){
infile.open("data.txt");
int num;
string str;
while(!infile.eof()){
infile>>num;
infile.ignore();
getline(infile, str);
cout<<num<<endl;
cout<<str<<endl;
}
infile.close();
}
long power(long base, long exponent){
int result = 1;
for(int i=0; i<exponent; i++){
result *= base;
}
return result;
}
Lab05b.cpp
#include<iostream>
#include "Functions.h"
int main(){
ifstream infile;
readAndPrint(infile);
cout<<endl;
cout<<"2^3 = "<<power(2, 3)<<endl;
cout<<"10^4 = "<<power(10, 4)<<endl;
return 0;
}