C++ noob input question

Hi all,

I want to read space seperated characters to an array of dynamic size. The thing is, all i see around like get(), getline() is for string and preset array sizes. I need to have the array as char because i have some fiddling around to do with the ASCII values. Am i doomed to a static array of possibly 1million:(

Regards
Yigit
A std::string is a dynamic array of chars. You can access the individual characters of a std::string in the
same way as you'd access the individual characters of a char array.
theres something wrong with this, when i started using direct input things went weird. ıt doesnt even show that sorting works at all. What is wrong i cant find it.

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;
int cntr;
int passed_miliseconds;
void merge(int numbers[], int temp[], int left, int mid, int right){
  int i, left_end, num_elements, tmp_pos;
 
  left_end = (mid - 1);
  tmp_pos = left;
  num_elements = (right - left + 1);
 
  while ((left <= left_end) && (mid <= right))
  {
    if (numbers[left] <= numbers[mid])
    {
      temp[tmp_pos] = numbers[left];
      tmp_pos += 1;
      left += 1;
    }
    else
    {
      temp[tmp_pos] = numbers[mid];
      tmp_pos += 1;
      mid += 1;
    }
  }
 
  while (left <= left_end)
  {
    temp[tmp_pos] = numbers[left];
    left += 1;
    tmp_pos += 1;
  }
  while (mid <= right)
  {
    temp[tmp_pos] = numbers[mid];
    mid += 1;
    tmp_pos += 1;
  }
 
  //modified
  for (i=0; i < num_elements; i++)
  {
    numbers[right] = temp[right];
    right -= 1;
  }
}
 void m_sort(int numbers[], int temp[], int left, int right){
  int mid;
 
  if (right > left)
  {
    mid = (right + left) / 2;
	
    m_sort(numbers, temp, left, mid);
    m_sort(numbers, temp, (mid+1), right);
 for(int i=0;i<cntr-1;i++){
		if(i==mid+1)
			cout<<" I ";
		cout<<numbers[i]<<" ";}cout<<endl;
    merge(numbers, temp, left, (mid+1), right);
  }
}
void mergeSort(int numbers[], int temp[], int array_size){
  m_sort(numbers, temp, 0, array_size - 1);
}
/*int file_read(char a[], bool first)
{
	string reader;
	
	int i=0;
	ifstream myfile("test2.txt");
	if(!myfile) 
      {
		cout<<"Error opening output file"<<endl;
      system("pause");
      }
	else
	{
		if(first==true)
		{
			cntr=0;
			while(!myfile.eof())
			{
    			myfile>>reader;
				cntr++;
			}
			return cntr;
		}
		else
		{
			while(!myfile.eof())
    		{
     	 		myfile>> a[i];
      			i++;
   	 		}
		}
	}	
}*/
void swap(int a[],int b[],string c,string d,bool x,int s)//swaps string to ascii for sorting, then back
{
	if(x){
		for (int i = 0; i < s; i++)
        {
                a[i]=c[i];
				b[i]=d[i];
        }//end for
		}
	else{
		for (int i = 0; i < s; i++)
        {
                c[i]=a[i];
				d[i]=b[i];
        }//end for
		}
}
/*void sub_print(char a[])//print the middles. Do before 25th.
{
	int temp;
}*/
int main()
{
		int size=0;
		int *translator1,*translator2;
		
string buffer, array1;
cout<<"Enter sumthin"<<endl;
cin>>array1;
cntr=array1.length();

		translator1= new int[size];
		translator2= new int[size];



		swap(translator1,translator2,array1,buffer,true,size);
		long int before = GetTickCount();
         mergeSort(translator1, translator2, size);
			 long int after = GetTickCount();
 		swap(translator1,translator2,array1,buffer,false,size);
        for (int i = 1; i < size; i++)
        {
                cout << array1[i] << " ";
        }//end for
		 cout<<"\nExecution Time: "<<(after-before)<<" ms.\n";

        return 0;
		
}//end main
 




Last edited on
1 line of code, excluding newlines. That's a lot of reading...

Hint:
http://cplusplus.com/reference/string/string/length/
http://cplusplus.com/reference/string/string/c_str/
You may need to add one to the length to get the proper length of your string (including the null character).

-Albatross
As jsmith already pointed out to you, a string is exactly an array of characters. There is no need to convert to/from integer arrays.

You are getting lost because you are trying to do too much at one time. The trick with programming is to take a large problem and break it down into smaller parts. The smaller parts should not depend on the larger parts.

Be careful also with your variable names. For example, "swap" is not a good name, because it doesn't actually 'swap' anything... it copies stuff. Further, many of your variable names are not instructive. What is "x" for? (You have to read the function to know.) Why is an integer value named "s"? Wouldn't something like "size" or "count" or "array_length" be better? But again, you don't actually need this function...

"translator1", "translator2", "buffer", and "array1" are all aliases for the same things, so why are there so many names? You only need two:

1
2
    string input_array;
    string sorted_array;


The code in main() then becomes ridiculously simple:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    string input_array;
    string sorted_array;

    cout << "Please enter something: ";
    getline( cin, input_array );

    sorted_array = merge_sort( input_array );

    cout << "Your given input: " << input_array << "\n";
    cout << "After merge sort: " << sorted_array << "\n";
    return 0;
}

Obviously, I've made some assumptions about your requirements. I made the merge sort have the following prototype:

string merge_sort( const string& s )

You can use some default arguments to avoid having to write a wrapper function:

string merge_sort( const string& s, unsigned left = 0, unsigned right = string::npos )

That makes it easier to have a single, recursive function to do the sorting.
Be sure to read up on the Wikipedia about the merge sort:
http://en.wikipedia.org/wiki/Merge_sort

Hope this helps.
I deleted everything and started from scratch with the mentality of as little code as possible because after all, you have to learn something and i can't learn anything good from that mess except that its a mess:)

On my new start i have got across a problem. I want to remove spaces in an input so i used the following. It works, but when i print the whole content i get the part with spaces removed and some of the original spaced input towards the end.
1
2
getline(cin,raw_input);
remove(raw_input.begin(),raw_input.end(),' ');


for example when i enter: 1 2 A + / b E......i get:12A+/bE / b E

btw, if i want to convert them to their ASCII values, will int(raw_input[]) suffice?
Actually, I think an explicit type cast does suffice for this purpose.

-Albatross
The remove() algorithm really only shuffles things around. You need to erase() the leftover stuff...

1
2
getline( cin, raw_input );
raw_input.erase( remove( raw_input.begin(), raw_input.end(), ' ' ), raw_input.end() );

Good luck!
Problem solved. Thanks everyone.
Topic archived. No new replies allowed.