Thread: c++// Programming Challenge #11 in Chapter 8

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    4

    c++// Programming Challenge #11 in Chapter 8

    I am not sure what I am doing wrong but the showNames part of my code is not show in my output boy
    can I get some tip or help no what I am doing wrong
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The first thing you did wrong is post a C++ problem in the C forum. Mods, please move.


    You should also post your code in [code][/code] tags. It's easier for us, we can often spot-check it without having to download and compile/run it. Check out the following links
    Announcements - General Programming Boards
    Announcements - C Programming

    You should also be more clear in your problem description. What is an "output boy". Did you mean "output box"? Or did you mean you expect the word "boy" in the output but don't see it? It would help if you provided a small input file that demonstrated the problem. I ran your program with a simple names.txt and saw exactly what I expected, based on the code. Was there enough output that it scrolled off the screen and you simply need to scroll back up to see it?

    Some other problems:

    • You need to #include the right header to use system() -- I don't know what this is in C++
    • system("pause") is platform specific -- it will only work on Windows. Maybe not an issue, but then again, maybe it is (it makes my job of helping you more difficult since I use Linux and it breaks on that command). If you must use it for school, fine. Otherwise, consider other ways to keep the console open after your program terminates.
    • You use magic numbers, like 26 and 65. Better to define some constants, or use the character literal 'A' (which equivalent to decimal value 65 if using an ASCII or compatible character set).
    • Your code assumes ASCII or compatible character sets. This is very common but not universal -- not all character sets have the entire alphabet contiguous and in order, nor does 'A' necessarily start at 65 (Google "EBCDIC" for example). I mention this more for informative reasons than anything, but it would not be difficult to make your code more generic.

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    4
    what happens is that only this part of the program is working
    Code:
    cout << "Here are the unsorted names:\n";
        cout << "--------------------------\n";
        showArray(names, NUM_NAMES);           //display the array.
        selectionSort(names, NUM_NAMES);       //sort the array
        cout << endl;
    
        cout << "Here are the names sorted:\n";
        cout << "--------------------------\n";
        showArray(names, NUM_NAMES);      //display the array.
        cout << endl;
    then it says press any key to continue this is what is supposed to happen
    Allen, Jim Allison, Jeff

    Total names that being with A: 2
    Collins, Bill

    Total names that being with B: 1
    and so on
    how do i fix this

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    
    using namespace std;
    int binarySeach(string bames[], int, string);
    
    
    
    
    //Function prototypes
    int file(string names[]);
    void showArray(string names[], int NUM_NAMES);
    void selectionSort(string names[], int NUM_NAMES);
    void showName(string names[], int NUM_NAMES);
    
    
    
    
    int main()
    {
    	string names[200];
    	int NUM_NAMES = file(names);
    
    
    	cout << "Here are the unsorted names:\n";
    	cout << "--------------------------\n";
    	showArray(names, NUM_NAMES);           //display the array.
    	selectionSort(names, NUM_NAMES);       //sort the array
    	cout << endl;
    
    
    	cout << "Here are the names sorted:\n";
    	cout << "--------------------------\n";
    	showArray(names, NUM_NAMES);      //display the array.
    	cout << endl;
    
    
    	showName(names, NUM_NAMES);       //count how many names begin with each letter.
    
    
    	system ("pause");
    	return 0;
    }
    
    
    int file(string names[])
    {
    	
    	ifstream infile;
    	infile.open("names.txt");
    	string line;
    
    
    	//if (!infile)
    	//{
    	//	cout << "unable to open name.txt" << endl;
    	//}
    	int row = 0;
    	while (getline(infile, line))
    	{
    		names[row] = line;
    		row++;
    	}
    
    
    	return row;
    }
    
    
    void showArray(string names[], int NUM_NAMES)
    {
    	for (int count = 0; count < NUM_NAMES; count++)
    	{
    		cout << names[count] << endl;
    	}
    }
    
    
    void selectionSort(string names[], int NUM_NAMES)
    {
    	int startScan, minIndex;
    	string minValue;
    	for (startScan = 0; startScan < (NUM_NAMES - 1); startScan++)
    	{
    		minIndex = startScan;
    		minValue = names[startScan];
    		for (int index = startScan + 1; index < NUM_NAMES; index++)
    		{
    			if (names[index] < minValue)
    			{
    				minValue = names[index];
    				minIndex = index;
    			}
    		}
    		names[minIndex] = names[startScan];
    		names[startScan] = minValue;
    	}
    }
    
    
    void showName(string names[], int NUM_NAMES)
    {
    	int count[26] = { 0 }, k = 0;
    	for (int row = 0; row <NUM_NAMES; row++)
    		{
    			count[names[row][0] - 'A']++;
    		}
    	for (int row = 0; row<26; row++)
    	{
    		if (count[row]>0)
    		{
    			for (int j = 0; j<count[row]; j++)
    			{
    				cout << names[k++] << endl;
    			}
    			cout << "***********************" << endl;
    			cout << "Total names Starting with " << char(65 + row) << ": " << count[row] << endl;
    			cout << endl;
    		}
    	}
    
    
    }

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Please provide a sample input file that demonstrates your problem. For example, I made up the following input file for testing
    Code:
    $ cat names.txt 
    Bob
    Alice
    Zeke
    Sam
    George
    Fred
    Sally
    which gave me the following output
    Code:
    $ ./a.out 
    Here are the unsorted names:
    --------------------------
    Bob
    Alice
    Zeke
    Sam
    George
    Fred
    Sally
    
    
    Here are the names sorted:
    --------------------------
    Alice
    Bob
    Fred
    George
    Sally
    Sam
    Zeke
    
    
    Alice
    ***********************
    Total names Starting with A: 1
    
    
    Bob
    ***********************
    Total names Starting with B: 1
    
    
    Fred
    ***********************
    Total names Starting with F: 1
    
    
    George
    ***********************
    Total names Starting with G: 1
    
    
    Sally
    Sam
    ***********************
    Total names Starting with S: 2
    
    
    Zeke
    ***********************
    Total names Starting with Z: 1
    That looks correct to me. Thus, you need to help us replicate your problem by giving us the exact input you use, which causes the errors you are describing.

    EDIT: Put the sample input file in [code][/code] tags, to preserve formatting, spacing, etc.

    EDIT 2: Also posted here: http://stackoverflow.com/questions/2...rogramm-to-wor. Please don't cross-post, it causes wasted effort. It's mentioned in the forum rules (one of the links I provided in post #2).
    Last edited by anduril462; 04-23-2014 at 12:00 PM.

  5. #5
    Registered User
    Join Date
    Apr 2014
    Posts
    4

    sorry

    what happens is that only this part of the program is working
    Code:
    Allen, Jim  
    Griffin, Jim  
    Stamey, Marty  
    Rose, Geri 
    Taylor, Terri  
    Johnson, Jill 
    Allison, Jeff  
    Looney, Joe  
    Wolfe, Bill 
    James, Jean  
    Weaver, Jim  
    Pore, Bob 
    Rutherford, Greg  
    Javens, Renee 
    Harrison, Rose  
    Setzer, Cathy 
    Pike, Gordon  
    Holland, Beth
    
    
    Here are the names sorted:
    --------------------------
    Allen, Jim
    Allison, Jeff
    Collins, Bill
    Griffin, Terri
    Harrison, Rose
    Holland, Beth
    James, Jean
    Javen, Renee
    Johnson, Jill
    Looney, Joe
    Pike, Gordon
    Pore, Bob
    Rutherford, Greg
    Setzer, Cathy
    Smith, Bart
    Weaver, Jim
    Wolfe, Bill
    
    
    Press any key to continue
    this is what is supposed to happen
    Code:
    
    
    Code:
    Here are the unsorted names;
    ----------------------------
    Collins, Bill     
    Allen, Jim  
    Griffin, Jim  
    Stamey, Marty  
    Rose, Geri 
    Taylor, Terri  
    Johnson, Jill 
    Allison, Jeff  
    Looney, Joe  
    Wolfe, Bill 
    James, Jean  
    Weaver, Jim  
    Pore, Bob 
    Rutherford, Greg  
    Javens, Renee 
    Harrison, Rose  
    Setzer, Cathy 
    Pike, Gordon  
    Holland, Beth
    
    
    Here are the names sorted:
    --------------------------
    Allen, Jim
    Allison, Jeff
    Collins, Bill
    Griffin, Terri
    Harrison, Rose
    Holland, Beth
    James, Jean
    Javen, Renee
    Johnson, Jill
    Looney, Joe
    Pike, Gordon
    Pore, Bob
    Rutherford, Greg
    Setzer, Cathy
    Smith, Bart
    Weaver, Jim
    Wolfe, Bill
    
    
    
    
    Allen, Jim Allison, Jeff
    ****************************
    Total names that being with A: 2
    
    
    Collins, Bill
    ****************************
    Total names that being with B: 1
    
    
    Press any key to continue
    and so on
    how do i fix this

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    
    using namespace std;
    int binarySeach(string bames[], int, string);
    
    
    
    
    //Function prototypes
    int file(string names[]);
    void showArray(string names[], int NUM_NAMES);
    void selectionSort(string names[], int NUM_NAMES);
    void showName(string names[], int NUM_NAMES);
    
    
    
    
    int main()
    {
        string names[200];
        int NUM_NAMES = file(names);
    
    
        cout << "Here are the unsorted names:\n";
        cout << "--------------------------\n";
        showArray(names, NUM_NAMES);           //display the array.
        selectionSort(names, NUM_NAMES);       //sort the array
        cout << endl;
    
    
        cout << "Here are the names sorted:\n";
        cout << "--------------------------\n";
        showArray(names, NUM_NAMES);      //display the array.
        cout << endl;
    
    
        showName(names, NUM_NAMES);       //count how many names begin with each letter.
    
    
        system ("pause");
        return 0;
    }
    
    
    int file(string names[])
    {
        
        ifstream infile;
        infile.open("names.txt");
        string line;
    
    
        //if (!infile)
        //{
        //    cout << "unable to open name.txt" << endl;
        //}
        int row = 0;
        while (getline(infile, line))
        {
            names[row] = line;
            row++;
        }
    
    
        return row;
    }
    
    
    void showArray(string names[], int NUM_NAMES)
    {
        for (int count = 0; count < NUM_NAMES; count++)
        {
            cout << names[count] << endl;
        }
    }
    
    
    void selectionSort(string names[], int NUM_NAMES)
    {
        int startScan, minIndex;
        string minValue;
        for (startScan = 0; startScan < (NUM_NAMES - 1); startScan++)
        {
            minIndex = startScan;
            minValue = names[startScan];
            for (int index = startScan + 1; index < NUM_NAMES; index++)
            {
                if (names[index] < minValue)
                {
                    minValue = names[index];
                    minIndex = index;
                }
            }
            names[minIndex] = names[startScan];
            names[startScan] = minValue;
        }
    }
    
    
    void showName(string names[], int NUM_NAMES)
    {
        int count[26] = { 0 }, k = 0;
        for (int row = 0; row <NUM_NAMES; row++)
            {
                count[names[row][0] - 'A']++;
            }
        for (int row = 0; row<26; row++)
        {
            if (count[row]>0)
            {
                for (int j = 0; j<count[row]; j++)
                {
                    cout << names[k++] << endl;
                }
                cout << "***********************" << endl;
                cout << "Total names Starting with " << char(65 + row) << ": " << count[row] << endl;
                cout << endl;
            }
        }
    
    
    }
    [/QUOTE]

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    WHERE IS THE INPUT FILE!?!?!?!?!?!?!?!?!

    Seriously, I can not repeat your problem. I need to see the input file, exactly as it is formatted. Maybe it is in a different format than I expect, and thus your showName function doesn't work. But I can't test that without knowing the exact format of the input file.

  7. #7
    Registered User
    Join Date
    Apr 2014
    Posts
    4
    I put the file in attachments is that ok

    names.txt
    Code:
    Collins, Bill     
    Allen, Jim  
    Griffin, Jim  
    Stamey, Marty  
    Rose, Geri 
    Taylor, Terri  
    Johnson, Jill 
    Allison, Jeff  
    Looney, Joe  
    Wolfe, Bill 
    James, Jean  
    Weaver, Jim  
    Pore, Bob 
    Rutherford, Greg  
    Javens, Renee 
    Harrison, Rose  
    Setzer, Cathy 
    Pike, Gordon  
    Holland, Beth
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    See, this is a perfect example of why crappy posts without the required information are so annoying. We've been going back and forth for 4 hours!. If you would have posted this the first time I asked for it, your problem would have been solved 4 hours ago. I asked you to post the exact input format in code tags. What you posted in the code tags is not what is in the file you attached. I don't know why you couldn't simply copy-paste this into code tags. Here are the contents of the file you attached:
    Code:
     "Collins, Bill",
     "Smith, Bart",
     "Allen, Jim",
     "Griffin, Terri",
     "Johnson, Jill",
     "Allison, Jeff",
     "Looney, Joe",
     "Wolfe, Bill",
     "James, Jean",
     "Weaver, Jim",
     "Pore, Bob",
     "Rutherford, Greg",
     "Javen, Renee",
     "Harrison, Rose",
     "Setzer, Cathy",
     "Pike, Gordon",
     "Holland, Beth"
    Notice that the first "letter" of each name is not actually a letter.

    Also, you completely failed to address any of other the issues I mentioned in post #2.

    Yes, I know I am being a a-hole. But seriously, if you can't follow simple directions, then you stand little chance of succeeding in programming, or most other fields.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming Challenge: Permutations
    By vasiqshair in forum C++ Programming
    Replies: 7
    Last Post: 09-09-2011, 03:45 AM
  2. Replies: 6
    Last Post: 07-26-2011, 02:57 PM
  3. Programming challenge
    By sybariticak47 in forum Windows Programming
    Replies: 1
    Last Post: 01-05-2006, 02:14 AM
  4. programming challenge
    By grohyt in forum C++ Programming
    Replies: 3
    Last Post: 09-19-2005, 07:07 PM
  5. C programming challenge
    By lost in C in forum C Programming
    Replies: 6
    Last Post: 03-11-2002, 01:27 AM