i need some help for this sorting function. The aim of this function is to create an array of pointer to char to let the user fill it with some words an gets it back sorted with alphabetical order. This is part of the code:
void Sort(bool flag)
{
if(flag == false)
{
//ordina stringhe
int NumString = 0;
cout<<"Number of strings: ";
cin>>NumString; //!!
char *Vector[NumString];
for(int i=0;i<NumString;i++)
{
cout<<"Insert the "<<i<<" element: ";
cin>>Vector[i];
int Val = 0;
char* swap;
for(int i=0;i<NumString;i++)
{
for(int j=0;j<NumString;j++)
{
Val = strcmp(Vector[i],Vector[j]);
if(Val > 0)
{
swap = Vector[i];
Vector[i]=Vector[j];
Vector[j]=swap;
}
}
}
for(int i=0;i<NumString;i++)
{
cout<<Vector[i];
cout<<endl;
}
}
}
And when i try to run it i get this error but i don't understand why.
This is the output of the program:
Number of strings: 3
Insert the 0 element: abc
read from master failed
: Input/output error
RUN FAILED (exit value 1, total time: 7s)
char *Vector[NumString] is defined as an array of char pointers.
More on this:
https://www.codeproject.com/Articles/7042/How-to-interpret-complex-C-C-declarations
Since none of the char* elements are initialized, your program is making an illegal memory access when attempting cin and gets closed/crashes.
To avoid this, you can use std::string instead which takes care of memory allocation for you.
Edit:
If you must use char arrays, you can do the following:
you can pre-allocate the char array lengths like so: char Vector[numString][noteLength]. Then use std::cin >> std::setw(noteLength) >> Vector[i].
You could also dynamically allocate the pointers using your previous definition of char *Vector[NumString] and calling Vector[i] = new char[noteLength]. Don't forget to call delete Vector[i] when you're done!
You'd also have to #include <iomanip> to use std::setw
Related
I am working on a project where I parse a string in to an array and then return it back to the main function. It parses fine but when I return it to the main function I can't get access to the array elements.
//This is from the Main function. It calls commaSeparatedToArray which returns the array.
for (int i = 0; i < numberOfStudents; i++) {
string * parsedToArray = mainRoster->commaSeparatedToArray(studentData[i]);
Degree degreeType = SOFTWARE;
for (int i = 0; i < 3; i++) {
if (degreeTypeStrings[i] == parsedToArray[8])
degreeType = static_cast<Degree>(i);
}
mainRoster->add(parsedToArray[0], parsedToArray[1], parsedToArray[2], parsedToArray[3], stoi(parsedToArray[4]), stoi(parsedToArray[5]), stoi(parsedToArray[6]), stoi(parsedToArray[7]), degreeType);
}
//Here is the commaSeparatedToArray function
string * roster::commaSeparatedToArray(string rowToParse) {
int currentArraySize = 0;
const int expectedArraySize = 9;
string valueArray[expectedArraySize];
int commaIndex = 0;
string remainingString = rowToParse;
while (remainingString.find(",") != string::npos) {
currentArraySize++;
if (currentArraySize <= expectedArraySize) {
commaIndex = static_cast<int>(remainingString.find(","));
valueArray[currentArraySize - 1] = remainingString.substr(0, commaIndex);
remainingString = remainingString.substr(commaIndex + 1, remainingString.length());
}
else {
cerr << "INVALID RECORD. Record has more values then is allowed.\n";
exit(-1);
}
}
if (currentArraySize <= expectedArraySize) {
currentArraySize++;
commaIndex = static_cast<int>(remainingString.find(","));
valueArray[currentArraySize - 1] = remainingString.substr(0, commaIndex);
remainingString = remainingString.substr(commaIndex + 1, remainingString.length());
}
if (currentArraySize < valueArray->size()) {
cerr << "INVALID RECORD. Record has fewer values then is allowed.\n";
exit(-1);
}
return valueArray;
}
1) You can't return arrays in C++. Your code (as I'm sure you know) returns a pointer to an array. That's an important difference.
2) The array is declared locally in the function and therefore no longer exists after the function has exitted.
3) Therefore once you have returned from the function you have a pointer to something which no longer exists. Bad news.
4) You must always consider the lifetime of objects when you program C++. One solution to this problem is to dynamically allocate the array (using new[]). This means that the array will still exist when you exit the function. But it has the signifcant disavantage that you must remember to delete[] the array at a suitable later time.
5) The best solution (in general) is to use a std::vector. Unlike an array a std::vector can be returned from a function. So this option leads to the simplest, most natural code.
vector<string> roster::commaSeparatedToArray(string rowToParse) {
...
vector<string> valueArray(expectedArraySize);
...
return valueArray;
}
Since your array/vector is constant size, you could also use a std::array
array<string, expectedArraySize> valueArray;
To complete the answer that John has already given, I made some example code to show you, how such function could look like.
Parsing, or tokenizing can be easily done with the std::sregex_token_iterator. That is one of the purposes for this iterator. You can see the simplicity of the usage below.
In the function we define a vector af string and use its range constructor to do the whole tokenizing.
Then we make a sanity check and return the data.
Please see:
#include <string>
#include <regex>
#include <iterator>
#include <vector>
#include <algorithm>
#include <iostream>
const std::regex separator(",");
constexpr size_t ExpectedColumnSize = 9;
std::vector<std::string> commaSeparatedToArray(std::string rowToParse)
{
// Parse row into substrings
std::vector<std::string> columns{
std::sregex_token_iterator(rowToParse.begin(),rowToParse.end(),separator ,-1),
std::sregex_token_iterator() };
// Check number of columns
if (columns.size() != ExpectedColumnSize) {
std::cerr << "Error. Unexpected number of columns in record\n";
}
return columns;
}
// test code
int main()
{
// Define test data
std::string testInputData{ "1,2,3,4,5,6,7,8,9" };
// Get the result from the parser
std::vector<std::string> parsedElements{ commaSeparatedToArray(testInputData) };
// show the result on the console
std::copy(parsedElements.begin(), parsedElements.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
return 0;
}
class Node_Str{
public:
string name;
string value;
string type;
Node_Str(string name,string value,string type){
name=name;
value=value;
type=type;}};
static stack<Node_Str> s;
void find_token(string input){
int cursor=0;
string current="";
while(cursor<input.length()){
char value;
value=input[cursor];
cout<<value<<endl;
if(value=='('||value==')'||value=='+'||value=='-
'||value=='*'||value=='/'){
Node_Str* p=new Node_Str("pare",string(1,value),"Pare");
s.push(*p);
cursor++;
delete p;}
if(value==' '){
cursor++;
}
if(value=='1'||value=='2'||value=='3'||value=='4'){
Node_Str* p=new Node_Str("num",string(1,value),"Number");
s.push(*p);
cursor++;
delete p;}}}
int main(){
while(!s.empty()){
cout<<s.top().value<<" ";
s.pop(); }
return 0; }
The find_token function should separate the input string by white Space and constructing the Node_Str object with the value of that string. Then in the
main function, I would like to print it. The characters are limit. Just '1', '2','3','4','+,'-','*','/'.
Input is 4 + 4 , output should be 4+4. However, there is no output.
The comments already said about memory leak and forgetting to call find.
In addition to that, stack is a container in which to last to be pushed in would be the first to be popped out. In order to output 0 1 2 3, you would need to push in the stack in the sequence of 3 2 1 0.
Just giving a better version.
#include <iostream>
#include <stack>
using std::cout;
using std::stack;
static stack<int> s;
void find()
{
int* p;
for (int i = 3; i >= 0; i--) {
p = new int(i);
s.push(*p);
delete p; // p itself does not have to be returned so it can be safely deleted here
//This can also ne replaced by directly using s.push(i)
}
}
int main() {
find();
while (!s.empty())
{
cout << s.top() << " ";
s.pop();
}
return 0;
}
as suggested by leyanpan, data should be pushed into reverse order, to get the desired output. One more point is no need of dynamic allocation for int type. It is always better to store non array built in types in stack rather than heap.
Also allocating stack data structure as static variable will extend scope up to program termination. Better to use stack object as a local variable in main and pass it as are reference argument to function find.
With this program, when i type in a name nothing is returned.
How would I fix this?
There are 1000 lines of info that looks like this:
114680858 19670607 Matilda Vincent MI
114930037 19471024 Desdemona Hanover ID
115550206 19790110 Xanadu Perlman ND
116520629 19630921 Alexander Hall SD
117050976 19301016 David Lamprey GA
119610646 19650202 Thomas Porlock IL
120330928 19621126 Cary Cartman NC
etc......
Code:
struct employees
{
int ss_number;//social security
int dob;//date of birth YYYY/MM/DD Ex.) 19870314=1987/03/14
string f_name;
string l_name;
string state; //state of residence
};
void read_file()//read file into array of 1000 structs
{
ifstream data("/home/www/class/een118/labs/database1.txt");
employees array[1000]
if(!data.fail())
{
int i;
for(int i=0;i<1000;i++)
{
data>>array[i].ss_number
>>array[i].dob
>>array[i].f_name
>>array[i].l_name
>>array[i].state;
}
for(int i=0;i<1000;i++)
{
cout<<array[i].ss_number>>" "<<array[i].dob>>" "<<array[i].f_name>>" "<<
array[i].l_name>>" "<<array[i].state;
}
}
}
void print_person(employees e)
{
cout<<e.ss_number>>" "<<e.dob>>" "<<e.f_name>>" "<<e.l_name>>" "<<e.state;
}
void search(employees array[])//type in name and get that persons ss_number,dob etc...
{
string first;
string last;
cout<<"Enter name";
cin>>first>>last;
for(int i=0;i<1000;i++)
{
if(array[i].f_name==first && array[i].l_name==last)
{
print_person(array[i]);
}
}
}
void main()
{
employees array[10];
read_file();
search(array);
}
// ...
There are two arrays. One is in main, the other is in read_file. They have the same name but are different sizes.
The array in read_file has no relationship to the array in main. You passed the array to search but not to read_file. I suggest you pass the array to read_file by reference and remove the array declaration in read_file.
Better yet, eliminate the array and use std::vector. It would be std::vector<employees>.
Edit 1: Searching the array
In your search function you will need to pass two additional parameters: array capacity and the number of records in the array. If you used std::vector<employees>, you could get the number of employees in the array by:
number_of_employees = array.size();
The for loop would use iterators:
std::vector<employees>::const_iterator iter;
for (iter = array.begin(); iter != array.end(); ++iter)
{
// process array slot by dereferencing it:
employee e = *iter;
cout << e << "\n"; // This could happen if you overloaded operator <<
}
Otherwise, with an array, your loop would look like:
void search(employees array[], unsigned int capacity, unsigned int employees_in_array)
{
for (unsigned int i = 0; i < employees_in_array; ++i)
{
cout << array[i];
}
}
A nice improvement is that this search function doesn't hardcode the size. So you can change the size from 10 (in main) to 1000 without modifying the search function.
If you sort your container, you can use a binary search.
See: std::binary_search, std::find, std::lower_bound, std::upper_bound
I'm trying to run a dynamic array that employs strings, but when I push it through a function I get compile errors 'dynamicArray': undeclared identifier, 'string':undeclared identifier, and illegal use of type 'void'. All of these errors point to the header for some reason.
I call the pointer here:
string* dynamicArray = NULL;
I call the function here:
populateArray(dynamicArray);
What is in the header:
void populateArray(string *&dynamicArray);
The function:
void populateArray(string *&dynamicArray)
{
char decide;
bool moreStrings = true;
int counter = 0;
while (moreStrings == true)
{
counter ++;
dynamicArray = new string[counter];
cout << "\nEnter your string here:";
cin >> dynamicArray[counter - 1];
cout << "\nDo you want to enter another string? Y/N:";
cin >> decide;
decide = toupper(decide);
if (decide == 'N')
{
moreStrings = false;
}
}
}
PS: vector may be better, but I'm afraid that isn't an option. Please only offer fixes that deal with pointers.
With #include <string> and using namespace std; added, it compiles just fine for me.
#include <string>
#include <iostream>
using namespace std;
void populateArray(string *&dynamicArray);
int main(){
string* dynamicArray = NULL;
populateArray(dynamicArray);
return 0;
}
void populateArray(string *&dynamicArray)
{
char decide;
bool moreStrings = true;
int counter = 0;
while (moreStrings == true)
{
counter ++;
dynamicArray = new string[counter];
cout << "\nEnter your string here:";
cin >> dynamicArray[counter - 1];
cout << "\nDo you want to enter another string? Y/N:";
cin >> decide;
decide = toupper(decide);
if (decide == 'N')
{
moreStrings = false;
}
}
}
You need to include <string> in your header file.
BTW, there might be potential memory leak in your code, if moreString is true, dynamicArray will point to another new string array, without deleting current one.
I see a bigger problem than the missing include and the using clause...
You wrote:
dynamicArray = new string[counter];
But this will allocate a new memory area for you every time. It does not copy the previously allocated elements. IF you don't want to use std::vector, you need to use malloc for the first element and than call realloc to copy your previously allocated data to your new one.
Check this form more info: What is C++ version of realloc(), to allocate the new buffer and copy the contents from the old one?
Changed completely due to suggestions from other member. Most problems solved, still having problems. Now won't output any names from the array in main. Not sure if I'm passing them back correctly from function.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void bubblesort(string[], const int);
int sub = 0;
int main()
{
const int maxsize = 100;
string friendArray[maxsize];
ifstream friends;
friends.open("myFriends.dat");
while (sub < maxsize)
{
getline(friends, friendArray[sub]);
sub++;
}
bubblesort(friendArray, maxsize);
cout<<friendArray[0]<<" "<<friendArray[1]<<" "<<friendArray[2];
system("pause");
return 0;
}
void bubblesort(string *array, const int size)
{
bool swap;
string temp;
do
{
swap = false;
for (int count = 1; count < (size - 1); count++)
{
if(array[count-1] >array[count])
{
temp = array[count-1];
array[count-1] = array[count];
array[count] = temp;
swap = true;
}
}
}
while(swap);
}
Your problem isn't necessarily that temp inside bubblesort is not a char, the problem is that array is declared as a string and not a string[].
The reason you're getting the error is because array[count+1] is of type char, and temp is of type string. std::swap expects two elements of the same type.
However, that may be the least of your problems, your code doesn't compile for quite a few reasons. Not just that but you're passing in maxsize to bubblesort at each iteration. There's a flaw in both your logic and your syntax.
EDIT: Since you're still having trouble getting the sorting to work, here's a working modification of your code:
#include <iostream>
void bubblesort(std::string array[], size_t size)
{
bool bSwapped;
std::string temp;
do
{
bSwapped = false;
for (size_t count = 1; count < size; count++)
{
if(array[count-1] > array[count])
{
std::swap(array[count-1], array[count]);
bSwapped = true;
}
}
}
while(bSwapped);
}
int main(void)
{
std::string array[] = { "def", "ghk", "abc", "world", "hello" };
bubblesort(array, sizeof(array)/sizeof(*array));
for (size_t i = 0; i < sizeof(array)/sizeof(*array); ++i)
std::cout << array[i] + " ";
std::cout << std::endl;
return 0;
}
bubblesort could also be written as: void bubblesort(std::string *array, size_t size). There's no difference in this case since, when passed to a function, arrays decay into pointers.
Since arrays are passed by reference, a pointer to the first element, any modifications made to array inside of bubblesort will actually be modifying your array in main. So that's how arrays are "returned".
std::vector is a good alternative to the standard array, since it automatically resizes and obviously contains the length of the internal array so that you don't have to pass the size everywhere you pass an std::vector. You can also use it the same way as a regular array.
temp is a string, array[count] is a char (since an std::string is a vector of char elements.) I'm not sure what you're trying to do here, but the compiler is correct - you can't assign a char to a string.
You could change temp to be a char, since all you do with it is assign a char to it, and then assign it back to an element of array, which is also a char.
You need to declare temp as char. You can use std::swap to avoid such mistakes in the future:
std::swap(array[count], array[count+1]);
This would make your code compile, but it would not do what you're trying to do (bubblesort). The problem is that you are passing a single string (which is also an "array" of characters) instead of an array of strings, which is, in a very lose sense, "an array of arrays of characters". Your bubblesort needs to accept string *array as its first parameter.