How to input reverse arrays and reverse strings

This is what I need to do for two parts of my project, need some insight on how to even start or do this. so how do i add on this reverseit function? and for the arrays? to be honest, im not good with this and my teacher really hasn't taught me much so i have to attempt to learn everything on my own.


• Add a new function to your project called: ReverseIt() 
• From the main function call ReverseIt(). 
• ReverseIt will create an array to hold 10 integers, open your input file, read each line, add the integer on 
each line into the array.  Close the input file. 
• At this point, ReverseIt function has an array of 10 integers that it read from the file. 
• Now open an output file called ReverseArray.txt, and write the array elements into the file in reverse 
order. In other words, the last integer that you read from your StraightArray.txt will be the first integer 
in this ReverseArray.txt file 


and


• Add a new function to your project called: ReverseIt() 
• From the main function call ReverseIt(). 
• ReverseIt will open StraightStrings.txt as input file and open a ReverseStrings.txt file as output file. 
• It will read one line of the input file and put its string into a character array. 
• Then write the character array in reverse order into the output file: 
o Example: input string = “Hello World”  Î output string to output file = “dlroW olleH”  
• Note: this is done one string at a time. You are not reading all the input file strings into an array, then 
processing them.  You are reading one string from the input file, and then writing it to the output file


So far on my project, I have this and I'm not even sure if everything is correct

Assign.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include "Finance.h"
#include <iomanip>
#include <fstream>
using std::ifstream;
using std::ofstream;

using namespace std;



int _tmain(int argc, _TCHAR* argv[])
	{
	Finance myFinance;
	myFinance.x=7;
	
	ifstream inFile ("datafile.txt");
	ofstream outFile ("resultfile.txt");

	float loan;
	float interest;
	int duration;
	float payment;

	if(!inFile)

	{
		cerr << "Error: Input file could not be opened" << endl;
		exit(1);
	}

	if(!outFile)
	{ cerr << "Error: Output file could not be opened" << endl;
	exit(1);
	}
	while ( !inFile.eof() )
	{
		inFile >> loan >> interest >> duration;
		payment = myFinance.loanPay (loan, interest, duration);
		outFile << loan << ", " << interest << ", " << duration << "= " << payment << endl;
	}
	cout << "End-of-file reached.." << endl;
	inFile.close();
	outFile.close();


	for (char response=' '; response != 'x'; ) 
	{
		cout << "This program is designed to calculate a Maturity value of an investment or \n";
		cout << "the monthly payments on a loan.  Please enter the 1 or 2 or 3. \n";
		cout << "1) Maturity Calculator \n";
		cout << "2) Monthly Loan Payments \n";
		cout << "3) Swap Numbers \n";
		cin >> response;
        switch (response)
        {

    case '1':
        myFinance.maturitydriver();
        break;
    case '2':
        myFinance.loanDriver();
        break;
    }
	}
	 
	return 0;
}


Finance.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include "Finance.h"
#include <iomanip>
using namespace std;

double Finance::MaturityCalc (double invest, float interestR, float years, int compound)
{	
	double interestT;
	double epon;

	interestT = (1+((interestR/100) / compound)); // =(1+P/M)
	epon = (years * compound); //^(Y*M)

	double result = invest * pow (interestT, epon); //Finally, multiply the investment into the total interest

	return result;
}
int Finance::maturitydriver()
{
	double invest;	
	float interestR; 
	float years;	
	int compound;	
	double maturity; 

	cout << "This program will calcuate a maturity.  Please enter the following: \n";
		
	//ask user for input
	cout << "\nInital investment: ";
	cin >> invest;
	cout << "\n";
	
	cout << "Interest rate, in percentage: ";
	cin >> interestR;
	cout << "\n";
	
	cout << "Total years of maturity: ";
	cin >> years;
	cout << "\n";
	
	cout << "Compound, number of times interest is applied: ";
	cin >> compound;
	cout << "\n";

	maturity = MaturityCalc(invest, interestR, years, compound);
	
	cout << "Your maturity is: " << maturity << "\n";

	system ("pause");
	return 0;
}

double Finance::loanPay (int loan, float rate, int payment)
{
		float baseLoan = 0;		
		double numer = 0;		
		double denom = 0;		
		double resultB = 0;		
		double resultF = 0;		
		double resultT = 0;		

		baseLoan = (1 + (rate/1200));						
		resultB = pow ((double) baseLoan, (double) payment); 
		numer = ((double) rate/1200) * resultB;			
		denom = resultB - 1;							 
		resultF = numer / denom;						 
		resultT = resultF * loan;						 
	
	return resultT;
}
int Finance::loanDriver()
{
	cout << "This program will calculate your monthly loan payments,\n";
	cout << "please enter the following: \n";

		int loan, payment = 0;  //L and N, Loan amount and number of payments
		float rate = 0;			//R, interest rate
		double getLoan = 0; //calls getloan
	
		cout << "\nLoan amount: ";
		cin >> loan;
		cout << "\n";
	
		cout << "Interest Rate, in percentage: ";
		cin >> rate;
		cout << "\n";

		cout << "Number of payments: ";
		cin >> payment;
		cout << "\n";

		getLoan = loanPay(loan,rate,payment);

		cout << "Your monthly loan payment is: " << getLoan << "\n"; //display results

		system ("pause");
		return 0;
};

int swapDriver () 
{ 
	int x;
	int y;
 cout << "\nPlease enter x: "; 
 cin >> x; 
 cout << "\nPlease enter y: "; 
 cin >> y; 
 swap (x, y);
 cout << "\n After swap: Here is x: "<< x << " and y: " << y; 
 system ("pause");
 return 0;
}
void swap (int x, int y)
{
int temp = x;
x = y;
y = temp;
return ;
}


Finance.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once
class Finance
{

public:
	double MaturityCalc (double invest, float interestR, float years, int compound);
		
		int maturitydriver ();
		int x;
	
public:
		double loanPay (int loan, float rate, int payment);
		int loanDriver ();
public:
	void swap (int x, int y);
	int swapDriver ();
};


datafile.txt
1
2
3
4
5
6
7
8
12000 5.75 60
12000 6.00 60
12000 6.25 60
12000 6.50 60
12000 5.75 48
12000 6.00 48
12000 6.25 48
12000 6.40 48


Any help will be very much appreciated.
if anyone can do this for me and get it working, i'd glady paypal you a little something something :)
Lol, I dont't want a reward but this may help you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
#include <string.h>

char STOP[265];
using namespace std;

int main (void)
{

    cout << "Enter the string to be reversed...\n";
    cin.getline(STOP,265);
  

  int c = strlen(STOP) - 1;

    for (; c >= 0; c--)
    {

        cout << STOP[c];

    }




    return 0;
}

Hope it helps!
Last edited on
Try with the following code, it will work fine

step1) Reading the string (line) from the input file
step2) Reversing the string
step3) Writing the reversed string to the output file

Sample input and output files also attached for your reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
        ifstream inFile ("datafile.txt");
	ofstream outFile ("resultfile.txt");

	char buffer[256];

	if(!inFile)

	{
		cerr << "Error: Input file could not be opened" << endl;
		exit(1);
	}

	if(!outFile)
	{ cerr << "Error: Output file could not be opened" << endl;
	exit(1);
	}
	while ( !inFile.eof() )
	{
		//Step1 - Reading string from input file
		inFile.getline(buffer,14);
		int bSize=strlen(buffer)-1;

		//Step2 - Reversing the char buffer		
		for (int i=0;i<(bSize/2);i++)
		{	  
		   char ch=buffer[i];
		   buffer[i]=buffer[bSize-i];
		   buffer[bSize-i]=ch;
		}
		//Reversing the char buffer
		

		//Step3 - Write to Output File
		if(bSize!=0)
			outFile<<buffer<<std::endl;		
	}
	cout << "End-of-file reached.." << endl;
	inFile.close();
	outFile.close();


datafile.txt
1
2
3
4
5
6
7
8
12000 5.75 60
12000 6.00 60
12000 6.25 60
12000 6.50 60
12000 5.75 48
12000 6.00 48
12000 6.25 48
12000 6.40 48



resultfile.txt
1
2
3
4
5
6
7
8
06 57.5 00021
06 00.6 00021
06 52.6 00021
06 05.6 00021
84 57.5 00021
84 00.6 00021
84 52.6 00021
84 04.6 00021



Last edited on
Topic archived. No new replies allowed.