How to...

I have written a function to take a string and return the string garbage attached. For example "THIS!!!!!" becomes "THIS". So Now that I have it written I want to give it an array of sorted strings that I have also. I want the string of sorted arrays to give each string to the function when the function returns the string corrected with no garbage I want this to be stored into an array all clean and proper.
So I'm just learning arrays and I'm really having a hard time with the concept of moving things about. I'm not sure how put my above writings into code.
This is my code. If you were to compile it the output is just the heading and then the sorted array with the junk still attached.

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
122
123
124
125
#include <iomanip>
#include <istream>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// Function to test for Empty file
void peekFile(ifstream &fin)
{	
	char empty;
	// To check if the file is empty
	empty=fin.peek();
	if(empty = fin.eof())
	cout << "No data exists" << endl;
}

// To Print Heading
void print_heading(ifstream &fin)
{
	//Formatting for Heading 
	cout << "Letter " << '\t' << "Count " << '\t'<< "Frequency " << endl;
	cout << setw(6) << setfill('=') << '='  <<'\t' << 
	setw(5) << setfill ('=') << '=' << '\t' << setw(9) 
	<< setfill('=') << '=' << endl;
	
	// Function incase file is empty
	peekFile(fin);
	

}

//Function to remove non - alpha
string OnlyAlpha(string str1)
{	
	string newStr1 ="";
	newStr1.reserve(str1.size()); 

	for(string::size_type i = 0; i < str1.size(); i++)
	{	
		if ( isalpha( str1[i] ) ) 
		newStr1.push_back( str1[i] );
	}

	return newStr1;
}

//To fill array of strings
void fillString(ifstream &fin, string inputarray[], const int size)
{
	string str= "";
	
	// Loop to fill input array
	for(int i = 0; size > i; i++)
	{	
		fin >> inputarray[i];
	} 
	
}

// Function to sort array of strings
void selectionSort(string list[], int size)
{
	int i = 0, j = 0, smallest = 0; 
		string temp = "";

	for(i = 0; i < size -1; i++)
	{
		smallest = i;
		
		for(j = i + 1; j < size; j++)
		{
			if(list[j] < list[smallest])
				smallest = j;
		}

		//swap
		temp = list[smallest];
		list[smallest] = list [i];
		list [i] = temp;
	}


}

//Function to print
void print(const string list[], int size)
{
	for(int i = 0; i < size; i++)
		cout << list[i] << "\t";
	cout << endl;

}




void main()
{	
	ifstream fin;

	fin.open("mp6input.txt");
	
	const int size = 100;
	//array
	string inputarray[size];
	
	// to print heading
	print_heading(fin);
	
	// to fill string
	fillString(fin, inputarray, size);
	
	// to sort array 
	selectionSort(inputarray, size);
	
	// to print array
	print(inputarray, size);

	

system("pause");


}




This is the code I was playing around with to think of an idea.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
oid funfun(string str1[], int size)
{	string str2;
	string ns;
	const int q=100;
	string chair[q];
	
	for( int  i = 0; i < size; i++)
	{
		str1[i]= str2;
		ns = OnlyAlpha(str2);
	}


}


I feel I'm pretty far from solution.
Thanks
By the way there is no need to initilaize a string with empty quotes

string newStr1 ="";

Such initialization is fully equivalent to

string newStr1;
OK vlad from moscow. Thanks. What do you do watch your computer until I post? You answer so many.
Do you think that something like the following is possible? The swap thing is a little fuzzy to me. But is it possible to do something like this to fill an array as a function which takes strings returns them?
Just a thought.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Function to fill new array with new words.
void funfun(string str1[], int size)
{	string str2;
	const int q=100;
	string newstr[q];
	
	string chair[q];
	
	for( int  i = 0; i < size; i++)
	{
		str1[i]= str2;
		for( int i = 0; i < size; i++)
		{
			newstr[i]= OnlyAlpha(str2);
		}
	}
	// Maybe here then I can swap the array of newstr with the original and just return 
	// The original?

}
dont really understand what you want to do.

but you wont get a return from a void function.
also i dont understand why you use string str1[] and string newstr[q], just write it without the brackets like you did in line 3 for str2.

you mentioned swapping, if you want to do that you can't just save a variable in another, the data of the first one will be lost. you need an additional variable to save the data from the first one before you overwrite it with the 2nd.

and str1[i] = str2; wont work. str1[i] is a single character, but str2 is a string.
Because i'm trying to write it as an array.
I want to have a function that takes an array that is sorted. It give the strings at position i to the OnlyAlpha() function this will return a string that I want to store into an array.
Only alpha takes a string like " HELLO!!!" and returns "HELLO"
Do see what I want?
Hopefully yes. The trouble is coming up with the code for it
@Jesseiky (1)

Keep that up & I will report you to admin for trolling.
What are you talking about ? TheIdeasMan? What does this mean "@Jesseiky (1)

Keep that up & I will report you to admin for trolling."


THx
@jlillie89

Yeah, they deleted their post, or someone has reported them.

It's annoying when trolls post something like:

I have no idea about this topic :)


Maybe we should all just report that sort of post, so as not to pollute your thread with useless stuff.
@jlillie89

Going back to your problem, there is an isalpha function you could use instead of the OnlyAlpha.

Also, main returns an int, not void. I usually explicitly return a zero at the end of main.

As Darkmaster said, you won't get anywhere with void functions. You could try sending arguments as references - that way the value of those variables in main will change, as opposed to changing local copies in functions.

Hope all goes well.
Oh I thought you were talking about me with that trolling thing. I was like what did I do ?
OK I have since worked on this problem not much progress. I use onlyalpha to take a word like THEIR!!! and give back THEIR it just removes the junk. I know main returns an int. I also know it is a big deal on here but this is what my teacher does so I use void main();


OK Here is this function. It is not returning the proper thing. I want to give onlyalpha an array at an index i; It will take this this string at index i remove the junk and put it back where it was and I will have an array of string with no junk attached to any of the words. Junk as in HEY!!! or NOW(((( ... This is my code.

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
#include <iostream>
#include<iomanip>
#include <string>
using namespace std;


void onlyAlpha(string str1, int countWord)
{	
	string newStr1 ="";
	newStr1.reserve(str1.size()); 

	for(string::size_type i = 0; i < str1.size(); i++)
	{	
		if ( isalpha( str1[i] ) ) 
		newStr1.push_back( str1[i] );
		countWord++;
	}
	
}

 //Function to fill new array with new words.
void funfun(string str1[], int isize, int& countWord)
{	

	
		for(int i = 0; i < isize; i++)
	{
		     onlyAlpha(str1[i],countWord);
	}		
		
		


}


void main()
{
	
	
	const int isize = 3;	
	string str1[]={"!!HELLO","WHAT###", "%%NOW"};
	string test= "";
	const int size = 100;
	string wordsminus[size];
	int wordCount = 0;
	
	funfun(str1,isize,wordCount);
	  
	for(int j=0; j < isize; j++)
	{
		cout << str1[j];
	}
	system("pause");

}


This is just a little side program I'm trying to get it working first before I copy and past into my actual homework program.

Why when I print the array which is supposed to me free from junk I get the original array.

Thanks
Oh I thought you were talking about me with that trolling thing. I was like what did I do ?


That's why the @Jesseiky (1) - meaning I was addressing the comment to them.


Why when I print the array which is supposed to me free from junk I get the original array.


That's because you send info to functions which then use local copies of the argument, not the argument itself. This is a basic feature of functions and is called pass by value.

To get around this you need pass by reference, which uses pointers or references, so that the original variables value is changed.

Read up about C++ references.

If there is only one variable value that will change, you can return it from a function.


I know main returns an int. I also know it is a big deal on here but this is what my teacher does so I use void main();


The thing is, it won't compile on a modern compiler - I would tell your teacher that. It would be very unfair to loose marks for doing something that is right. What compiler are you using?
Wait I got the part that I wanted solved just ignore me. I don't know what you mean by what compiler I'm using how would I tell this? I just have Microsoft Visual c++ 2010
I don't know if that helps you or not. I have been using void main the whole semester.

That's why the @Jesseiky (1) - meaning I was addressing the comment to them.

I didn't see this name I thought you were on a all night alcohol binge and were deliriousness.
:)
No, I'm just a little slow :)

What compiler do I use?
If I use this
for(string::size_type i = 0; i < str1.size(); i++)


or if I use
for(int j=0; j < isize; j++)
What is the difference. Because When I use size_type i and compare i (which is int to it)
I get an unsigned mismatch warning. So when should I be using one vs other.
Topic archived. No new replies allowed.