Please use C++ code that is appropriate for an intro to programming class please
ID: 3908845 • Letter: P
Question
Please use C++ code that is appropriate for an intro to programming class please. You must create a header file for this assignment.
The prototype code for the main.cpp you must use is below:
The code provided to use for the postive integer validation is as follows:
Thanks!
Explanation / Answer
// myheader.h
#ifndef MYHEADER_H_
#define MYHEADER_H_
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
using namespace std;
// function declarations
bool isPositiveInteger(string numbers) ;
int randNum();
bool testMyFile(string filename);
void writeMyFile(int count, string filename);
// function definitions
// function that checks if the input string is a positive integer or not
bool isPositiveInteger(string numbers) {
int i; //counter for the loop
int goodnums = 0; //count of good number characters found
int length = numbers.size(); //get the size of the string passed in
bool done = false; //flag to break our loop early
for (i = 0; i < length && !done; i++) {
//if character is between '0' and '9' count it, else done
if (numbers[i] >= '0' && numbers[i] <= '9') {
goodnums++;
} else {
done = true;
}
}
//if goodnums == length, return true, else return false
if (goodnums == length && length != 0) {
return true;
} else {
return false;
}
}
// function that generates random number between 1-100(inclusive)
int randNum()
{
return (rand()%100 + 1);
}
//function to test if the input file contains only positive integers or not
bool testMyFile(string filename)
{
ifstream fin(filename.c_str());
string line;
if(fin.is_open())
{
while(!fin.eof())
{
getline(fin,line);
if(!isPositiveInteger(line))
{
fin.close();
return false;
}
}
fin.close();
return true;
}else // file doesn't exist
return false;
}
// function to write count number of random numbers in file given by filename
void writeMyFile(int count, string filename)
{
ofstream fout(filename.c_str());
for(int i=0;i<count-1;i++)
fout<<randNum()<<" ";
fout<<randNum();
fout.close();
}
#endif /* MYHEADER_H_ */
//end of myheader.h
//main.cpp
// C++ program that reads a number provided from command line argument and generates and writes that number of random numbers in a file.
# include "myheader.h"
int main(int argc, char** argv) {
if(argc > 1) // check if any command line argument is provided or not
{
string countStr = argv[1]; // read the argument
if(!isPositiveInteger(countStr)) //check if the argument is positive integer
{
cout<<" You must enter an integer between 1 and 1000 on the command line. ";
return 1;
}
// convert string to int
stringstream countStream(countStr);
int count;
countStream >> count;
// check if argument value is between 1-1000
if(count < 1 || count > 1000)
{
cout<<" You must enter an integer between 1 and 1000 on the command line. ";
return 1;
}
// filename for writing the random numbers
string filename = "numbers.txt";
srand(time(NULL)); // set the seed value for random number generator
writeMyFile(count, filename); // write the numbers in file
if (testMyFile(filename)) // test if the file contains only positve numbers or not
{
cout << "success: this file has only positive integers in it." << endl << endl;
} else {
cout << "fail: this file has things other than positive integers in it." << endl << endl;
}
}else
cout<<" Invalid number of arguments";
return 0;
}
//end of program
//end of main.cpp