Using C++ write a program with the following: Main program: Ask the user for the
ID: 3671104 • Letter: U
Question
Using C++ write a program with the following:
Main program:
Ask the user for the name of a text file
Open that text file for input
Check that the file was successfully opened (end the program if it was not)
Call an input function to get 3 integer values from the text file
Call normalize function to get 3 double values
Call output function to write out the 3 integers and the 3 doubles
Close the input file and clear the input file variable
Call a function that will ask the user if they want to repeat the program.
Repeat all of the previous steps if the user chooses to continue
Input function:
receive the opened input file variable from the main program
read 3 integers in from the file
send back all 3 input values.
Normalize function:
receive the 3 integers from main
call largest function to find the largest of the 3 integers
divide each of the 3 input integers by the largest value, storing the result into 3 doubles that will be in the range of 0.0 to 1.0 send back the three new double values
Largest function:
receive 3 integers
use return to send back a copy of the largest of the three
Output function:
receive 3 integer values and 3 doubles
write out the values with some text
Repeat Option function:
receives nothing from the main program
give the user 2 options for repeating or not (i.e. ‘Y’ or ‘N’)
use an error check loop to force them to re-enter their response if they entered anything other than the specified options.
use return to send back the user choice
Explanation / Answer
//Program:
#include <iostream>
#include <fstream>
using namespace std;
//Input function:
/*receive the opened input file variable from the main program*/
int* input(ifstream &infile)
{
int inputs[3];
/*read 3 integers in from the file*/
for(int i=0;i<3;i++)
infile>>inputs[i];
/*send back all 3 input values.*/
int *p=inputs;
return p;
}
//Largest function:
//receive 3 integers
int largest(int input[3])
{
int large;
/*finding largest of 3 integers*/
if(input[0]>input[1]&&input[0]>input[2])
large=input[0];
else if(input[1]>input[0]&&input[1]>input[2])
large=input[1];
else
large=input[2];
/*use return to send back a copy of the largest of the three*/
return large;
}
//Normalize function:
// receive the 3 integers from main
double* normalize(int inputs[3])
{
double values[3];
/*call largest function to find the largest of the 3 integers*/
int large=largest(inputs);
for(int i=0;i<3;i++)
{
/*divide each of the 3 input integers by the largest value, storing the result into 3 doubles*/
/*that will be in the range of 0.0 to 1.0 */
values[i]=(double)inputs[i]/(double)large;
/*send back the three new double values*/
}
double *v=values;
return values;
}
//Output function:
/*receive 3 integer values and 3 doubles*/
void output(int *inputs,double *values)
{
/* write out the values with some text*/
cout<<endl<<"Three integer inputs are: "<<*(inputs+0)<<","<<*(inputs+1)<<","<<*(inputs+2);
cout<<endl<<"Three double values are: "<<*(values+0)<<","<<*(values+1)<<","<<*(values+2);
}
//Repeat Option function:
/* receives nothing from the main program*/
char option()
{
/*give the user 2 options for repeating or not (i.e. ‘Y’ or ‘N’)*/
int flag=0;
char choice;
//use an error check loop
while(flag==0){
cout<<endl<<"Do you want to continue <'Y' or 'N'>: ";
cin>>choice;
/*to force them to re-enter their response if they entered anything other than the specified options.*/
if(choice!='Y'&& choice!='N')
{
flag=0;
cout<<endl<<"re-enter the option";
}
else{
flag=1;break;
}
}
/* use return to send back the user choice*/
return choice;
}
// Main program:
int main()
{
while(true){
/*Ask the user for the name of a text file*/
cout<<endl<<"Enter the name of the text file: ";
string filename;
cin>>filename;
/*Open that text file for input*/
ifstream infile;
infile.open(filename.c_str());
/*Check that the file was successfully opened */
if(infile)
{
cout<<endl<<filename<<" is successfully opened.";
}
/*end the program if it was not*/
else
{
exit(0);
}
/* Call an input function to get 3 integer values from the text file*/
int *p;
p=input(infile);
/* Call normalize function to get 3 double values*/
double *v;
v=normalize(p);
/* Call output function to write out the 3 integers and the 3 doubles*/
output(p, v);
/* Close the input file and clear the input file variable*/
infile.close();
filename="";
/* Call a function that will ask the user if they want to repeat the program.*/
char choice=option();
/* Repeat all of the previous steps if the user chooses to continue*/
if(choice=='N')
exit(0);
}
return 0;
}
//Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.