Hello All.
I am currently working on a project that introduces us to recursion and was needing some help with my program. This is early code so i know many things aren't correct.

#include <iostream>
#include <fstream>
using namespace std;


int str_length(const char[] str);
int str_compare(const char[] str1, const char[] str2);
void str_reverse(char[] str, int length);
bool str_replace_first(char[] str, const char[] find, const char[] replace, bool is_partial_match);

int main (int argc, char* argv[]) 
{
	ifstream infile;
	infile.open (argv[1]);
	ofstream outfile;
	outfile.open (argv[2]);

	if (argc < 3)//checks parameters on the command line
	{
		cout << "Error: usage:main input output" << endl;
		return 1;
	}
	
	char a[50];
	
	for (int i=0; i<50; i++)
	{
		a[i] = 'A' + i;
		a[49] = '\0';
		
		cout << "string before reversing:  " << a << endl;
		stringReverse( a );
		cout << "string  after reversing:  " << a << endl;
		return 0;
		
	}
	//max length on list
	const int LINE_LENGTH = 100;
	//data type of list items
	char str[LINE_LENGTH];
	
	
	//checks to see if characters are readable
	while( infile.getline(str, LINE_LENGTH))
	{
		cout << "Read from file: " << str << endl;
	}
	
	//make sure to close your file. 
	infile.close();
	
}
//Writes a string of characters backwards
//pre: There must be characters. Size is >= 0
int str_length(const char[], int str)

int str_compare(const char[], int str1, const char[], int str2)

void str_reverse(char[], int str, int length)
{
	if (length > 0)
	{
		cout << str.substr(length-1, 1);
	
		str_reverse(str, length-1);
	}

}

bool str_replace_first(char[] str, const char[] find, const char[] replace, bool is_partial_match)

i am currently working on the str_length function. I m not sure where to start. My two biggest problems is that i am unsure how to run this function when it is called in the terminal and second, i am unsure to to get the data that is being stored in the array for the input file then print that count to the output file. Also, we were given sample input and out on the user level of the program as a means to test our code. I will list that below. Thanks for all the help.

Sample Input
reverse hello
length_iter basketball
compare Duck elephant
replace_first there re ir
Sample Output
olleh
10
-33
their

Recommended Answers

All 2 Replies

To get the data from the input file, just use cin. For example:

int main(){
    //From previous posts you have the code that goes here
    char command[100], array[100];

    cin >> command;//A clue would be put this in a loop
     //In the loop 
             cin >> array;

To get the data from the input file, just use cin. For example:

int main(){
    //From previous posts you have the code that goes here
    char command[100], array[100];

    cin >> command;//A clue would be put this in a loop
     //In the loop 
             cin >> array;

cin takes input from "standard input" (i.e. the keyboard). To read from the input file, you would want to read from an ifstream, which in this case is called infile .

Hello All.
I am currently working on a project that introduces us to recursion and was needing some help with my program. This is early code so i know many things aren't correct.

#include <iostream>
#include <fstream>
using namespace std;


int str_length(const char[] str);
int str_compare(const char[] str1, const char[] str2);
void str_reverse(char[] str, int length);
bool str_replace_first(char[] str, const char[] find, const char[] replace, bool is_partial_match);

int main (int argc, char* argv[]) 
{
	ifstream infile;
	infile.open (argv[1]);
	ofstream outfile;
	outfile.open (argv[2]);

	if (argc < 3)//checks parameters on the command line
	{
		cout << "Error: usage:main input output" << endl;
		return 1;
	}
	
	char a[50];
	
	for (int i=0; i<50; i++)
	{
		a[i] = 'A' + i;
		a[49] = '\0';
		
		cout << "string before reversing:  " << a << endl;
		stringReverse( a );
		cout << "string  after reversing:  " << a << endl;
		return 0;
		
	}
	//max length on list
	const int LINE_LENGTH = 100;
	//data type of list items
	char str[LINE_LENGTH];
	
	
	//checks to see if characters are readable
	while( infile.getline(str, LINE_LENGTH))
	{
		cout << "Read from file: " << str << endl;
	}
	
	//make sure to close your file. 
	infile.close();
	
}
//Writes a string of characters backwards
//pre: There must be characters. Size is >= 0
int str_length(const char[], int str)

int str_compare(const char[], int str1, const char[], int str2)

void str_reverse(char[], int str, int length)
{
	if (length > 0)
	{
		cout << str.substr(length-1, 1);
	
		str_reverse(str, length-1);
	}

}

bool str_replace_first(char[] str, const char[] find, const char[] replace, bool is_partial_match)

i am currently working on the str_length function. I m not sure where to start. My two biggest problems is that i am unsure how to run this function when it is called in the terminal and second, i am unsure to to get the data that is being stored in the array for the input file then print that count to the output file. Also, we were given sample input and out on the user level of the program as a means to test our code. I will list that below. Thanks for all the help.

Sample Input
reverse hello
length_iter basketball
compare Duck elephant
replace_first there re ir
Sample Output
olleh
10
-33
their

Break this into several different parts. It'll be more manageable that way. You are terminating the program prematurely here:

for (int i=0; i<50; i++)
	{
		a[i] = 'A' + i;
		a[49] = '\0';
		
		cout << "string before reversing:  " << a << endl;
		stringReverse( a );
		cout << "string  after reversing:  " << a << endl;
		return 0;
		
	}

with the return 0; line.

it appears to me that the str_reverse function isn't supposed to display anything, so delete the cout line below:

void str_reverse(char[], int str, int length)
{
	if (length > 0)
	{
		cout << str.substr(length-1, 1);
	
		str_reverse(str, length-1);
	}

}

Regarding the function itself, nizbit had this thread a few days ago. Is this the same assignment?

http://www.daniweb.com/forums/thread144263.html

Regarding this code:

while( infile.getline(str, LINE_LENGTH))
	{
		cout << "Read from file: " << str << endl;
	}

getline may not be your best bet here. See this thread, also by nizbit:

http://www.daniweb.com/forums/thread144362.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.