Help with Testing code and correct output

Hello,

Could you help me with getting this code to work with free online compilers for testing purposes.

I want to use the Standard text input so it reads.


barry TroY JoE
jiLL
KaTe ABE HILDEGARDE TrOy
zoe


It would be most helpful if you could also help me in getting this type of output.


# unique names: 8
average lengh of a name: 4.500

Lenght of longest name(s) is 10
Longest name(s)
Hildegarde

Lenght of shortest name(s) is 3
shortest name(s)
Joe
Abe
Zoe

Alphabetized Name List
Abe
Barry
Hildegarde
Jill
Joe
Kate
Troy
Zoe






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

using namespace std;

const int MAX=30;

int search(string[], int, string);
void fixstring(string&);
void get_data(string[], int&);
void bubblesort(string[],int&);

int main()

{
    int n;
    int count;
    string names[MAX];
    int place;
    int finished = -9;
    string target = "";
    get_data(names, n);
    bubblesort(names, count);
    
    while (finished != -9)
    {
        cout<<"enter name to find \n";
        cout<< " Q to quit \n";
        if (target == "Q")
            finished = -9;
        else
        {
            place=search (names, n, target);
            cout<< target << "";
            if (place >= 0)
                cout << "found at position "<< place;
            else
            cout << "not found";
            cout << endl;
            
        }
    }
    return 0;
}

void get_data(string names[], int& n)

{
    //ifstream fin;
    string fnames;
    int namecount;
   // cout<< "enter name of input file" << endl;
    cin>>fnames;
    //fin.open(fnames.c_str());
    n = 0;
    while(cin)
    {
        fixstring(names[n]);
        n++;
        namecount++;
       cin>>fnames;
    }
    cout<<"Number of Names in the file "<<namecount<<endl;
    
}

void fixstring (string& word)
{
    int num = word.size();
    int lenght = word.size();
    word[0] = toupper (word[0]);
    for (int i=1; i<num; i++)
        word[i] = tolower(word[i]);
}

int search(string names[], int n, string target)
{
    for (int i=0; i<n; i++)
        if (names[i] == target)
            return i;
        return -1;
}

void bubblesort(string names[],int& count)
{
 string temp; //place holder when values are interchanged
 for (int i=0; i < count-1; i++)
 for (int j=0; j < count-(i+1); j++)
 if (names[j] < names[j+1])
 {
 temp = names[j];
 names[j] = names[j+1];
 names[j+1] = temp;
 cout << names[j] << endl;
 }
 cout << "Organized names by ABC order "<<endl;
 
}
Hello sr2cute702,

Most of the comments are in the following code. I did get it to work with the chnges I made.

One thing you need to learn is to initialize your variables when they are defined. Sometimes it will give me an error using an uninitialized variable.

In the "get_data" function I changed it to read from a file not "cin". I also added code to check if the file opened. Always good to check if the input file opened. A good idea for an output file, but not necessary because an output file will create the file if it does not exist.

The variable "count" is defined in main, but never given a value before it is used to call the "bubblesort" function. I fixed that, but not the best way to do that.

Check the comments in the following program.

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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <chrono>
#include <thread>
#include <limits>

using namespace std;   // <--- Not the best idea to use.

const int MAX = 30;

int search(string[], int, string);
void fixstring(string&);
void get_data(string[], int&);
void bubblesort(string[], int&);

int main()

{
	int n{};  // <--- Good practice to initialize all your variables.
	int count{};
	string names[MAX];
	int place{};
	int finished = 0;  // Changed to make the while loop work. Also not a good idea to set this up to end
	                   // the while loop before it starts.
	string target = "";  // <--- This is OK, but no need to initialize a string becuse it is already empty.

	get_data(names, n);
	count = n;  // <--- Defined "count", but never gave it a value before it is used in thee next line.
	bubblesort(names, count);

	//  Added this to print the sorted names. Keep or not it is up to you, but helps with testing.
	for (size_t lc = 0; lc < count; lc++)
	{
		std::cout << "\n " << names[lc];
	}

	while (finished != -9)
	{
		cout << "\n\n Enter name to find \n"; // Changed.
		cout << " Q to quit: "; // <--- Changed. Removed the "\n"
		std::cin >> target;

		if (target == "Q" || target == "q")  // <--- Added ||
			finished = -9;
		else
		{
			place = search(names, n, target);
			cout << "\n " << target << " "; // <--- Changed. Added space.
			if (place >= 0)
				cout << "found at position " << place;
			else
				cout << "not found";
			cout << endl;

		}
	}

	//  Could use something here to pause before the return.
	//  At least I need it.
	std::cin.get();  // <--- One possibioity.

	return 0;
}

void get_data(string names[], int& n)

{
	ifstream fin;
	std::string fileName{ "TextFile.txt" };  //<--- Added the variable and constant for file name for testing. Remove for run.
	string fnames;  // <--- Can keep this or use "fileName" if you want.
	int namecount{};
	
	//  Moved these lines up to here for when needed.
	// cout<< "enter name of input file" << endl;
	//cin >> fnames;
	//fin.open(fnames.c_str());

	fin.open(fileName);  //<--- Added.

	//  Always good practice and programming to make sure the input file is open.
	if (!fin.is_open())  //<--- Added.
	{
		std::cout << "\n File " << fileName << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // Requires header files "chrono" and "thread"
		exit(1);
	}

	n = 0;
	
	while (fin>>names[n])  // <--- Changed. This is where the read should go to.
	{
		fixstring(names[n]);
		n++;
		namecount++;
		//cin >> fnames;
	}

	cout << "Number of Names in the file " << namecount << endl;
	//  Clears the input buffer after a "fin >>" to something. Or can be used with "cin >>".
	fin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires heder file <limits>.
}

//  These next threee functions work.
void fixstring(string& word)
{
	int num = word.size();
	int lenght = word.size();
	word[0] = toupper(word[0]);
	for (int i = 1; i<num; i++)
		word[i] = tolower(word[i]);
}

int search(string names[], int n, string target)
{
	for (int i = 0; i<n; i++)
		if (names[i] == target)
			return i;
	return -1;
}

void bubblesort(string names[], int& count)
{
	string temp; //place holder when values are interchanged
	for (int i = 0; i < count - 1; i++)
		for (int j = 0; j < count - (i + 1); j++)
			if (names[j] > names[j + 1]) // <--- Changed to >.
			{
				temp = names[j];
				names[j] = names[j + 1];
				names[j + 1] = temp;
				//cout << names[j] << endl;  // <--- Confusing but OK for testing.
			}
	cout << "Organized names by ABC order " << endl;

}


Hope that helps,

Andy
Did you happen to know what library that you added is causing this error at my school's compiler?

#error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

Here is the code that mostly compiled with everything else.
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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <thread>
#include <limits>
using namespace std;   // <--- Not the best idea to use.                        
const int MAX = 30;
int search(string[], int, string);
void fixstring(string&);
void get_data(string[], int&);
void bubblesort(string[], int&);
int main()

{
  int n=0;  // <--- Good practice to initialize all your variables.             
  int count=0;
 string names[MAX];
  int place=0;
  int finished = 0;  // Changed to make the while loop work.                                                                                                                           
  string target = "";  // <--- This is OK, but no need to initialize a string becuse it is already empty.                                                                                                              

  get_data(names, n);
  count = n;  // <--- Defined "count", but never gave it a value before it is used in thee next line.                                                                                                                  
  bubblesort(names, count);

  //  Added this to print the sorted names. Keep or not it is up to you, but helps with testing.                                                                                                                       
  for (size_t lc = 0; lc < count; lc++)
    {
      cout << "\n " << names[lc];
    }

  while (finished != -9)
    {
      cout << "\n\n Enter name to find \n"; // Changed.                                                                                                                                                                
      cout << " Q to quit: "; // <--- Changed. Removed the "\n"                                                                                                                                                        
      cin >> target;
      if (target == "Q" || target == "q")  // <--- Added ||                                                                                                                                                            
        finished = -9;
      else
        {
          place = search(names, n, target);
          cout << "\n " << target << " "; // <--- Changed. Added space.                                                                                                                                                
          if (place >= 0)
            cout << "found at position " << place;
          else
            cout << "not found";
          cout << endl;

        }
    }

  //  Could use something here to pause before the return.                                                                                                                                                             
  //  At least I need it.                                                                                                                                                                                              
  cin.get();  // <--- One possibioity.                                                                                                                                                                                 

  return 0;
}

void get_data(string names[], int& n)

{
  ifstream fin;
  string fnames; //{ test004 };  //<--- Added the variable and constant for file name for testing. Remove for run.                                                                                                     
  //string test004;  // <--- Can keep this or use "fileName" if you want.                                                                                                                                              
  int namecount=0;

  //  Moved these lines up to here for when needed.                                                                                                                                                                    
   cout<< "enter name of input file" << endl;
   cin >> fnames;
  fin.open(fnames.c_str());

  //fin.open(fileName);  //<--- Added.                                                                                                                                                                                 

  //  Always good practice and programming to make sure the input file is open.                                                                                                                                        
  if (!fin.is_open())  //<--- Added.                                                                                                                                                                                   
    {
      cout << "\n File " << fnames << " did not open" <<endl;

      // exit(1);                                                                                                                                                                                                      
    }

  n = 0;

  while (fin>>names[n])  // <--- Changed. This is where the read should go to.                                                                                                                                         
    {
      fixstring(names[n]);
      n++;
      namecount++;
      //cin >> fnames;                                                                                                                                                                                                 
    }

  cout << "Number of Names in the file " << namecount << endl;
  //  Clears the input buffer after a "fin >>" to something. Or can be used with "cin >>".                                                                                                                             
  fin.ignore(numeric_limits<streamsize>::max(), '\n');  // <--- Requires heder file <limits>.                                                                                                                          
}
//  These next threee functions work.                                                                                                                                                                                  
void fixstring(string& word)
{
  int num = word.size();
  int lenght = word.size();
  word[0] = toupper(word[0]);
  for (int i = 1; i<num; i++)
    word[i] = tolower(word[i]);
}
int search(string names[], int n, string target)
{
  for (int i = 0; i<n; i++)
    if (names[i] == target)
      return i;
  return -1;
}
void bubblesort(string names[], int& count)
{
  string temp; //place holder when values are interchanged                                                                                                                                                             
  for (int i = 0; i < count - 1; i++)
    for (int j = 0; j < count - (i + 1); j++)
      if (names[j] > names[j + 1]) // <--- Changed to >.                                                                                                                                                               
        {
          temp = names[j];
          names[j] = names[j + 1];
          names[j + 1] = temp;
          //cout << names[j] << endl;  // <--- Confusing but OK for testing.                                                                                                                                           
        }
  cout << "Organized names by ABC order " << endl;
}
Topic archived. No new replies allowed.