String + Array = Confused student

This is a discussion on String + Array = Confused student within the C++ Programming forums, part of the General Programming Boards category; Ok so basically I'm working with parallel arrays. One stores the name of an Employee and the other the rate ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    4

    String + Array = Confused student

    Ok so basically I'm working with parallel arrays. One stores the name of an Employee and the other the rate of their pay. Eventually I am allowing the user to input the data....then outputting the highest and lowerst paid employee. However I am having a problem inserting the name into the array. I am completely confused as to what I'm doing wrong. Couldn't find any info on it. Any help would be much appreciated my final is tomorrow.

    Code:
    #include <iostream>
    #include<iomanip>
    #include<string>
    
    
    using namespace std;
    
    const int elements	= 15;
    void insertInfo(char name[], double rate[], int elements);
    void sort(char name[], double rate[]);
    void printExtremes(char name[], double rate[], int elements);
    
    int main()
    {
    	
    	char name[elements];
    	double rate[elements];
    
    	insertInfo(name, rate, elements);
    	printExtremes(name, rate, elements);
    
    	return 0;
    
    }
    
    void insertInfo(char name[], double rate[], int elements)  // Having trouble with this function
    {
    	int index = 0;
    	cout << "Enter last name and rate of pay: ";
    
    		for (index = 0; index < elements; index++)
    		{
    			cin.get(name,29,'\n');
    			cin >> rate[index];
    		
    		
    		}
    }
    void printExtremes( char name[], double rate[], int elements)
    {
    	double largestRate;
    	double smallestRate;
    	char largestName;
    	char smallestName;
    	int index;
    	int maxIndex = 0;
    	for (index = 1; index < elements; index++)
    		if (rate[maxIndex] < rate[index])
    			maxIndex = index;
    	largestRate = rate[maxIndex];
    	largestName = name[maxIndex];
    
    		for (index = 1; index < elements; index++)
    		if (rate[maxIndex] > rate[index])
    			maxIndex = index;
    	smallestRate = rate[maxIndex];
    	smallestName = name[maxIndex];
    	cout << "The highest paid employee is " << largestName << setfill ('-') << setw(20) << "$" << largestRate << endl;
    
    	cout << "The lowest paid employee is " << smallestName << setfill ('-') << setw(20) << "$" << smallestRate << endl;
    }

  2. #2
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Gotta love them teachers, parallel arrays instead of using a class/struct? Are the "parallel" arrays a requirement of the assignment? If so, your teacher should loose his/her job, he/she is obviously failing to teach you how to program well.
    goto( comeFrom() );

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4
    Quote Originally Posted by StainedBlue View Post
    Gotta love them teachers, parallel arrays instead of using a class/struct? Are the "parallel" arrays a requirement of the assignment? If so, your teacher should loose his/her job, he/she is obviously failing to teach you how to program well.
    PROBLEM #1
    Acme Publishing needs a C++ Program. They have 15 employees. Write a
    program for Acme that will allow them to enter 15 last names and 15 pay rates
    into parallel arrays. The program should then display the highest and the lowest
    pay rate stored in the array with the last name of the corresponding employee.

    There's a copy of the problem. Pretty much all the information I found said to use a class or structure.

  4. #4
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Are you allowed to use std::string?

    if so:
    Code:
    
    void getEmployeeInfo(std::string names[], double rates[], int numEmployees)
    {
        for(int i = 0; i < numEmployees; i++)
        {
            std::string name;
            double rate;
    
            std::cout << "Enter Name: ";
            std::cin >> name;
            std::cout << "Enter " << name << "'s pay rate: ";
            std::cin >> rate;
    
            names[i] = name;
            rates[i] = rate;
        }
    }
    goto( comeFrom() );

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4
    Awesome thank you very much. Definitely worked but do you know why exactly the original way I coded it wasn't working?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    In the original way you only appear to have one char array = string.

    You might have needed something like:

    Code:
    char name[elements][maxNameLength];
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    You were confused between getting an individual name into a char[] and storing an "array of names"

    you thought you had an array of names like
    [LARRY][CURLY][MOE][etc.]

    but you didn't

    all you had was an array that looked like
    [L][A][R][R][Y]



    You could've done some char pointer stuff, but that's generally frowned upon in C++, so better off using std::string. (Less headache & confusion)
    goto( comeFrom() );

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4
    Quote Originally Posted by StainedBlue View Post
    You were confused between getting an individual name into a char[] and storing an "array of names"

    Basically if you typed the name "LARRY" at the prompt,
    you had an array that looked like [L][A][R][R][Y]

    where you thought you had an array of names like
    [LARRY][CURLY][MOE][etc.]

    You could've done some char pointer stuff, but that's generally frowned upon in C++, so better off using std::string. (Less headache & confusion)
    Ok thanks. I thought the cin.get would have corrected that but obviously not. Thanks a lot for the help.

  9. #9
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    For future reference, this is a little more clear:
    Code:
    
    void printExtremes( char name[], double rate[], int count)
    {
            /*  Get Index of minimum rate
            */
    	int minIndex = 0;
    	for (int i = 1; i < count; i++){
    		
                if (rate[i] < rate[minIndex])
    	        minIndex = i;
            }
    	
    	/*  Get Index of maximum rate
            */
    	int maxIndex = 0;
    	for (int i = 1; i < count; i++){
    		
    	    if (rate[i] > rate[maxIndex])
    	        maxIndex = i;
            }
    	
    	cout << "The highest paid employee is " << name[maxIndex] << setfill ('-') << setw(20) << "$" << rate[maxIndex] << endl;
    	cout << "The lowest paid employee is " << name[minIndex] << setfill ('-') << setw(20) << "$" << rate[minIndex] << endl;
    }
    or to further condense:
    Code:
    
    
    int maxIndex = 0;
    
    for(int i = 1; i < count; i++ ){
    
        maxIndex = ( rate[i] > rate[maxIndex]) ? i : maxIndex;
    }
    Last edited by StainedBlue; 05-03-2010 at 01:13 AM.
    goto( comeFrom() );

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Or you could just learn to use std::min_element and std::max_element. Saves some code.
    Good when you have the principle down of creating the required logic.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Elysia View Post
    Or you could just learn to use std::min_element and std::max_element. Saves some code.
    Good when you have the principle down of creating the required logic.
    I made the assumption that such things were off limits, that the point of the assignment was the logic.
    goto( comeFrom() );

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    And I was merely pointing out that once you have learned that logic, you can simplify the code more by using built-in algorithms in the standard library. Clever!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Elysia View Post
    And I was merely pointing out that once you have learned that logic, you can simplify the code more by using built-in algorithms in the standard library. Clever!
    Agreed, no need to get all supercilious now...
    goto( comeFrom() );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my assigment
    By cloverdagreat in forum C Programming
    Replies: 16
    Last Post: 11-21-2009, 11:18 AM
  2. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  5. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21