Help with a few problems

I'm a pretty new person to c++ so i'm kinda having some trouble with this problem.
im using functions to sort out a sentence inputted by the user into ascending and descending order. i keep getting the error that n is undefined but i'm not sure where i'm going wrong.

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;


//void get(int array[], int n);
string bubble_sort(string list);
string selection_sort(string reverse);
//void display(int array[], int n);

int main(void)
{
	string line, sorted_line;
	int n = 10;
	//int array[n] = { 0 };
	//get(array, n);
	//bubble_sort(array, n);
	//selection_sort(array, n);
	//display(array, n);

	

	cout << "Enter in string to be sorted ascending and decending" << endl;
	getline(cin, line);

	// Now pass the line of characters to the function for sorting.

	sorted_line = bubble_sort(line);

	cout << sorted_line;

	sorted_line = selection_sort(line);

	cout << sorted_line << endl;

	system("pause");
	return 0;
}


//template< typename T >
string bubble_sort(string list)
{
	char temp;
	for (int i = 0; i< n; ++i){
		for (int j = i + 1; j<n; ++j){
			if (list[j] < list[i]){
				int temp = list[i];
				list[i] = list[j];
				list[j] = temp;
						}
					}
		}

return list;
}
string selection_sort(string reverse)
{
	for (int i = 0; i < n; i++){
		for (int j = i + 1; j < n; j++){
			if (reverse[j] > reverse[i]){
				int temp = reverse[i];
				reverse[i] = reverse[j];
				reverse[j] = temp;
						}
					}
				}
	return reverse;
		}
Last edited on
First, post code within code tags. See http://www.cplusplus.com/articles/jEywvCM9/
You can edit your post.


Second, why do you have 'n' on those places that give the error? What value is supposed to be in the 'n'? Do you know that std::string has member function size()?
See http://www.cplusplus.com/reference/string/string/size/

The compiler error (and warning) messages do state the line number of the issue.
okay, so i should change the n to make the user input equal the string size and put that into the loops?
I did this and it now it isn't sorting the sentence
Please show the latest version of bubble_sort.
Last edited on
First, n isn't visible in those functions, which is why you're getting an error. Second, std::string tracks it's own length, so what's the point of it?

You can get the length of a std::string named list by calling std::string::size() or (if you prefer) std::string::length():
list.size();

Also - line 46 declares a char named temp which is never used. It is never used because that name is shadowed by another variable also named temp within an inner scope on line 51. You should delete it. Plus, the standard library provides a swap() function, too:
std::swap(list[i], list[j]);.

I did end up seeing that the temp was declared as two different types and fixed that, the bubble sort function was given to me by my teacher, and we haven't gone over the swap function yet.


Here is the latest version of the 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;


//void get(int array[], int n);
string bubble_sort(string list);
string selection_sort(string reverse);
//void display(int array[], int n);

int main(void)
{
	string line, sorted_line;
	int n = 10;
	//int array[n] = { 0 };
	//get(array, n);
	//bubble_sort(array, n);
	//selection_sort(array, n);
	//display(array, n);

	

	cout << "Enter in string to be sorted ascending and decending" << endl;
	getline(cin, line);
	for (int i=0;i<line.length();i++){ // line.length() gets the length of the string
         line[i]=tolower(line[i]);
        }

	// Now pass the line of characters to the function for sorting.

	sorted_line = bubble_sort(line);

	cout <<"Decending order: " << sorted_line << endl;

	sorted_line = selection_sort(line);

	cout <<"Acending order: " << sorted_line << endl;

	system("pause");
	return 0;
}


//template< typename T >
string bubble_sort(string list)
{
	char temp;
	string line;
	for (int i = 0; i< line.length(); ++i){
		for (int j = i + 1; j<line.size(); ++j){
			if (list[j] < list[i]){
				temp = list[i];
				list[i] = list[j];
				list[j] = temp;
						}
					}
		}

return list;
}
string selection_sort(string reverse)
{
	string line;
	char temp;
	for (int i = 0; i < line.length(); i++){
		for (int j = i + 1; j < line.size(); j++){
			if (reverse[j] > reverse[i]){
				 temp = reverse[i];
				reverse[i] = reverse[j];
				reverse[j] = temp;
						}
					}
				}
	return reverse;
		}
Last edited on
the bubble sort function was given to me by my teacher

Surely you have done something to it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
string bubble_sort(string list)
{
	// the 'list' has your data
	char temp;
	string line;
	// the 'line' is empty and thus line.length() == 0

	for ( int i = 0; i< line.length(); ++i ) {
	// 0<0 is false and the outer loop does thus 0 iterations
	// these lines are not executed
		for ( int j = i + 1; j < line.size(); ++j ) {
			if ( list[j] < list[i] ) {
				temp = list[i];
				list[i] = list[j];
				list[j] = temp;
			}
		}
	}

  // nothing has changed the 'list'
  return list;
}


Lets go back and use that n:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string bubble_sort( string list )
{
	const int n = ????.size(); // whose size?
	char temp;
	for (int i = 0; i < n; ++i ) {
		for (int j = i + 1; j < n; ++j ) {
			if ( list[j] < list[i] ){
				int temp = list[i];
				list[i] = list[j];
				list[j] = temp;
			}
		}
	}
  return list;
}
this solved my problem!! thank you!!
Topic archived. No new replies allowed.