Help with Names

This is a discussion on Help with Names within the C++ Programming forums, part of the General Programming Boards category; Hi peeps, Im trying to write a program that outputs the names of the service advisors ( 8 of them ...

  1. #1
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74

    Help with Names

    Hi peeps,
    Im trying to write a program that outputs the names of the service advisors ( 8 of them total ) and ask's for the number of surveys that are rated as 'A'.Then ask's for the total surveys to come up with the score. I know how to come up with the score.My problem is how do i get the program to store the names as constants and be able to ittorate through them one at a time and compute the score for each advisor?

    output should be similar to this.

    Please enter the number of surveys passed for Joe.

    Please enter the Total surveys recieved for Joe.

    The score for Joe is. // could leave this out until the end.

    I need to be able to save the info for each advisor and ouput in a tabular format at the end of the last advisor.

    This is going to be a console app. and it is not a homework assignment.
    I am writing this using C++ visual.net IDE

    Also I have just started to program so im not to advanced.So if we could keep it simple that would be great.

    thx in advanced.
    ben

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,542
    Start with the question/answer series for just one adviser

    Then put a
    for ( i = 0 ; i < 8 ; i++ )
    loop around it to make it happen 8 times
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    Quote Originally Posted by Salem
    Start with the question/answer series for just one adviser

    Then put a
    for ( i = 0 ; i < 8 ; i++ )
    loop around it to make it happen 8 times
    I have done that. but what i need it to do is lets say...

    advisors = { "Mike", "Joe", "Rick" ect....}

    I need it to ouput the names to the screen one at a time so that i can enter the info. and keep ittorating through them until the last advisor is reached. Having the ifno for each advisor stored so that at the end of input for the last advisor i can output something like this.
    Code:
                                              Scores
    Advisor           Score
    ---------          --------
    Mike               82.2
    
    Joe                 93.1
     
    Rick                78.5
    
    and so on.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,542
    Put them in an array
    Code:
    char *advisers[] = {
      "Mike",
      "Joe", 
      "Rick"
    };
    
    // Then you do
    cout << "Data for " << advisers[i] << endl;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    Ok thats what i was getting at. so use an array of chars.
    thanks for the help Salem. Gonna try it out now.

  6. #6
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    Im still lost and I have searched to find the answer!
    If i do this
    Code:
    char *advisors[] = {"Mike", "Rick", "Joe" };
    
    cout << *advisors << endl; // It just says Mike <- this is ok
    
    // if i do this to try to get the next name
    
    cout << *advisors[ 1 ] << endl; //it just gives me 'R'
    I need it to ittorate through the list of whole names and output them to the screen one at a time so I can enter the info for that paticular advisor.

    Also if you could list the for( structure that i would use) to do this that would be great.
    thx
    ben

  7. #7
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    look at Salem's code, to get the second name exclude the *, otherwise it is just the adress of the first character of that word.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  8. #8
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    you don't need to dereference, just step through the array.

  9. #9
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    I got it to work this way. but now i have to figure out some how to get the "enter the score: " before the name.

    Code:
    int main()
    {
    
    	char Names[3][5] = { {"Mike"} ,{"Rick"}, {"Joe"} };
    
    	for (int i = 0; i < 3; i++ ) {
    
    		for ( int j = 0; j < 4; j++ )  { 
    			cout << Names[ i ] [ j ] ;
    		}
    		cout << endl;
    	}
    
    	return 0;
    }

  10. #10
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    Figured it out finaly.Here's the code for those interested.Any sugestions would be appreciated. this code is basicly just a Test but it demonstrates that it works.
    Code:
    int main()
    {
    	int score = 0;
    	int survey = 0;
    
    	char Names[3][5] = { {"Mike"} ,{"Rick"}, {"Joe"} };
    
    	for (int i = 0; i < 3; i++ ) {
    		cout << "enter the score for ";
    
    		for ( int j = 0; j < 5; j++ )  
    			cout << Names[ i ] [ j ] ;
    		cout << " " ;
    		cin >> score;
    		cout << "Enter the surveys passed for " << Names[ i ];
    		cout << " ";
    		cin >> survey;
    		cout << endl;
    
    	}
    
    	return 0;
    }
    Last edited by big146; 06-06-2004 at 09:20 PM.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,542
    Why do you use a loop here?
    cout << Names[ i ] [ j ] ;

    When 2 lines later, you output the whole name in one go like this
    cout << "Enter the surveys passed for " << Names[ i ];

    Both work, but the latter is so much more elegant.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    Ya I know, I just figured that out lastnight. I was thinking that I had to have two for loops. One to get to the first index of
    Names[ 0 ] <- and then another for loop to itorate through each character in the second Names[ 0 ] [ 0 ] <-. to spell the name out.

    but i changed it to this and it works..not sure how it does but it does.
    Code:
    nt main()
    {
    	int score = 0;
    	int survey = 0;
    
    		char Names[3][5] = { {"Mike"} ,{"Rick"}, {"Joe"} };
    
    	for (int i = 0; i < 3; i++ ) {
    		cout << "enter the score for "<< Names[ i ];
    		cout << " " ;
    		cin >> score;
    		cout << "Enter the surveys passed for " << Names[ i ];
    		cout << " ";
    		cin >> survey;
    		cout << endl;
    
    	}
    
    	return 0;
    }

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >> ...not sure how it does ....

    That's the "magic" of strings. The language/compiler allows you to treat a null terminated character array as a single object, rather than an array of char. Therefore you can do things like >> or << on a given string all at once, without having to loop through char by char. There are some special rules---like you can't compare two null terminated char arrays using the equals/not equals opertors, less than or greater than operators, but that can be overcome using STL string class (or some other string class) rather than a raw null terminated char array (sometimes called a C-style string instead of string); and >> only works for strings that don't contain whitespace, but that can be overcome using getline(), etc.


    Bottom line, the concept of strings is very useful. But, if/when you use the term string, you need to be sure you understand what type of string you are using so you can follow the appropriate rules.

  14. #14
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74

    Hi elad

    Thanks for that. Thats where I was messing up. I was treating it as indavidual characters instead of a string object. That makes it a little more clear now. Just now on chapter 4 array's.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  2. runtime function names
    By lruc in forum Tech Board
    Replies: 2
    Last Post: 10-11-2008, 10:51 AM
  3. reading folder names..how is it done ?
    By roalme00 in forum C++ Programming
    Replies: 8
    Last Post: 01-11-2008, 09:34 AM
  4. Opening files with korean file names
    By tek in forum C++ Programming
    Replies: 4
    Last Post: 08-28-2003, 10:22 PM
  5. Ordering names
    By unixOZ in forum C Programming
    Replies: 1
    Last Post: 11-06-2002, 04:54 PM

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