Student Line up

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nothingfights
    New Member
    • Apr 2008
    • 17

    Student Line up

    I NEED HELP GETTING THIS STARTED.

    A teacher has asked all her students to line up in a single file according to first name. Write a program that asks the user to enter the number of sudents in the class, and then loops to read in that many names. Once all the names have been read in it reports which student would be at the front and which student would be at the end. Assume no two students have the same name.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

    Please read the Posting Guidelines and particularly the Coursework Posting Guidelines.

    Then when you are ready post a new question in this thread.

    MODERATOR

    Comment

    • nothingfights
      New Member
      • Apr 2008
      • 17

      #3
      so far i have..
      #include <iostream>
      using namespace std;

      int main()
      {
      int students;

      cout << "Enter in the number of students in the class: \n";
      cin >> students;

      while (students <= 1 || students >= 25)
      {
      cout << "Enter in a number that is no less than 1 and no more than 25: ";
      cin >> students;
      }
      After this i do not exactly get what it is asking(the part about looping to read it)

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by nothingfights
        while (students <= 1 || students >= 25)
        What is the value in students before you enter the loop?

        Also, if I enter 1, then 1 <=1 is true. So I Stay in the loop. If I enter 25, then 25>=25 is true. So I stay in the loop.

        I looks like this loop selects a value between 2 and 24. I this what you intend?

        All you do now is use students in another loop that asks for the name, stores it somewhere and asks for the next name until you have entered the number of names in the students variable.

        Post again when you have advanced a little.

        Comment

        • oler1s
          Recognized Expert Contributor
          • Aug 2007
          • 671

          #5
          First, whenever you post blocks of code, use CODE tags around them. Now that you know the rule about posting code, please follow it.

          Next, the code you have so far is a mess. Ok, so you got the part about asking the number of students right. Then everything goes wrong in the loop. First, what is up with the loop condition? Are you familiar with a for loop? It's the most straightforward to write this loop.

          Next, Why are you asking for numbers in the loop? You want names. Which brings up the first question. What data structure would you use to store names of people? What data structure would you use to store only one name? Then, multiple names?

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Originally posted by oler1s
            Ok, so you got the part about asking the number of students right. Then everything goes wrong in the loop. First, what is up with the loop condition? Are you familiar with a for loop? It's the most straightforward to write this loop.

            Next, Why are you asking for numbers in the loop? You want names. Which brings up the first question. What data structure would you use to store names of people? What data structure would you use to store only one name? Then, multiple names?
            To be fair, the OP's loop is an attempt at input validation - a.k.a. he/she is trying to make sure the user entered between 1 and 25 students - before reading in any names.

            Comment

            • nothingfights
              New Member
              • Apr 2008
              • 17

              #7
              I used the while loop in the begining because the number cannot be less than 1 or greater than 25.

              Comment

              • nothingfights
                New Member
                • Apr 2008
                • 17

                #8
                the challenge also does not seem to mention entering in any names it just asked for a class size number to be entered

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  Originally posted by nothingfights
                  ...Write a program that asks the user to enter the number of sudents in the class, and then loops to read in that many names. Once all the names have been read in...
                  Yes, your challenge does mention this.

                  Comment

                  • nothingfights
                    New Member
                    • Apr 2008
                    • 17

                    #10
                    Code:
                    for (int count = 1; count <= students; count++)
                    {
                    	char names;
                    	
                    	cout << "Enter in name " << count << ": \n";
                    	cin >> names;
                    	
                    }
                    I have this for the part about asking for names. The problem is it only allows the user to enter in the first name.

                    Comment

                    • Laharl
                      Recognized Expert Contributor
                      • Sep 2007
                      • 849

                      #11
                      If you want to have the string include whitespace for the last name, you need to use getline() rather than cin, since cin stops inputting at any whitespace. Getline has its own little quirks, like leaving the newline character in the input stream rather than discarding it (I think), but it works for this sort of thing. Docs for cin.getline() are here .

                      Comment

                      • nothingfights
                        New Member
                        • Apr 2008
                        • 17

                        #12
                        I did the cin.getline and now it askes all but the first name

                        Comment

                        • Laharl
                          Recognized Expert Contributor
                          • Sep 2007
                          • 849

                          #13
                          Can we see some code? If I had to guess, I'd say you need to flush the newline from the input buffer, but I'm not 100% sure.

                          Comment

                          • nothingfights
                            New Member
                            • Apr 2008
                            • 17

                            #14
                            Code:
                            for (int count = 1; count <= students; count++)
                            {
                            	const int SIZE = 25;
                            	char names[SIZE];
                            	
                            	cout << "Enter in name " << count << ": \n";
                            	cin.getline(names, SIZE);
                            	cin.get();
                            	
                            }
                            I fixed the probem but now i need aid in arranging it in alphebetical order and displaying the student in the front and he student in the back.

                            Comment

                            • Laharl
                              Recognized Expert Contributor
                              • Sep 2007
                              • 849

                              #15
                              Well, there are lots of well-documented sorting algorithms available. There's probably even a howto here about them (Bubble sort comes to mind). Or you could just use the <algorithm> header file's sort() method, and save yourself the trouble of reinventing the wheel.

                              Also, you've got another problem: When you put each name into the array, it overwrites the previous name. You can use strcat to append, but you're better off using a char*[], or even better, use a string[] and use this version of getline. You'll need a loop.

                              Comment

                              Working...