Can someone help me write the correct logic statements so that I can output the
ID: 3636198 • Letter: C
Question
Can someone help me write the correct logic statements so that I canoutput the name and three decimal numbers in ascending order?
Thank you.
Here is the code:
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
double num1, num2, num3;
string fName, lName;
ifstream inFile;
ofstream outFile;
cout << " **Program outputs your full name and some numbers in ascending order. All input "
<< "read from a file.!**";
cout << " ";
cout << endl;
inFile.open("data.txt"); // open the input file
//check to make sure it's ok!
if (!inFile)
{
cout << " Cannot open the input file. Something is wrong!**";
cout << " The program terminates.** " << endl;
cout << " " << endl;
system("pause");
return 1;
}
outFile.open("results.txt"); //open the output file
cout << " Processing data...";
inFile >> fName >> lName;
cout << " Processing data...";
inFile >> num1 >> num2 >> num3;
//output full name to a file
outFile << fName << " " << lName << endl;
// begin conditionals to order numbers in ascending order
if (num1 < num3) // num1 less than num3
{ outFile << fixed << showpoint << setprecision(2) << num1 << ", ";
if (num3 < num2) // compare num2 and num3
outFile << fixed << showpoint << setprecision(2) << num3 << ", " << num2 << endl;
else
outFile << fixed << showpoint << setprecision(2) << num2 << ", " << num3 << endl;
}// end inner-if
}
else
{
outFile << fixed << showpoint << setprecision(2) << num3 << ", " << endl;
if (num1 < num2)
outFile << fixed << showpoint << setprecision(2) << num1 << ", " << fixed << showpoint << setprecision(2) << num2 << endl;
else
outFile << fixed << showpoint << setprecision(2) << num2 << ", " << fixed << showpoint << setprecision(2) << num1 << endl;
}
{
if (num2 < num3)
outFile << fixed << showpoint << setprecision(2) << num2 << ", " << endl;
if (num1 < num3)
outFile << fixed << showpoint << setprecision(2) << num1 << ", " << fixed << showpoint << setprecision(2) << num3 << endl;
else
outFile << fixed << showpoint << setprecision(2) << num3 << ", " << fixed << showpoint << setprecision(2) << num1 << endl;
}
else
{
if (num1 < num2)
outFile << fixed << showpoint << setprecision(2) << num1 << ", " << fixed << showpoint << setprecision(2) << num3 << endl;
else
outFile << fixed << showpoint << setprecision(2) << num2 << ", " << fixed << showpoint << setprecision(2) << num1 << endl;
*/
inFile.close();
outFile.close();
system("pause");
return 0;
}
Explanation / Answer
Mate, nested if expression is a really messy way of coding often, usually try to use "else if(condition)" If u want to print out 3 decimal number in ascending order, why don't sort just sort them? Take a look on this code. We use insertion sort function to sort numbers in ascending order. And we print them both to out file and stdout. Ask if u have any questions: #include #include #include #include using namespace std; void insertionSort(double* array, int size) { int key; int j; for (int i = 1; i = 0 && array[j] > key) { array[j+1] = array[j]; j--; } array[j+1] = key; } } int main() { double array[3]; string fName, lName; ifstream inFile; ofstream outFile; coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.