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.
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.
// Function to fill new array with new words.
void funfun(string str1[], int size)
{ string str2;
constint 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?
}
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
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.
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.
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 :)
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.