468,457 Members | 1,701 Online

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.
Apr 7 '08 #1
20 8883
sicarie
4,677 Expert Mod 4TB
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.

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

MODERATOR
Apr 7 '08 #2
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)
Apr 7 '08 #3
weaknessforcats
9,207 Expert Mod 8TB
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.
Apr 7 '08 #4
oler1s
671 Expert 512MB
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?
Apr 7 '08 #5
Ganon11
3,652 Expert 2GB
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.
Apr 7 '08 #6
I used the while loop in the begining because the number cannot be less than 1 or greater than 25.
Apr 7 '08 #7
the challenge also does not seem to mention entering in any names it just asked for a class size number to be entered
Apr 7 '08 #8
Ganon11
3,652 Expert 2GB
...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.
Apr 7 '08 #9
Expand|Select|Wrap|Line Numbers
1. for (int count = 1; count <= students; count++)
2. {
3.     char names;
4.
5.     cout << "Enter in name " << count << ": \n";
6.     cin >> names;
7.
8. }
I have this for the part about asking for names. The problem is it only allows the user to enter in the first name.
Apr 7 '08 #10
Laharl
849 Expert 512MB
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 .
Apr 7 '08 #11
I did the cin.getline and now it askes all but the first name
Apr 7 '08 #12
Laharl
849 Expert 512MB
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.
Apr 7 '08 #13
Expand|Select|Wrap|Line Numbers
1. for (int count = 1; count <= students; count++)
2. {
3.     const int SIZE = 25;
4.     char names[SIZE];
5.
6.     cout << "Enter in name " << count << ": \n";
7.     cin.getline(names, SIZE);
8.     cin.get();
9.
10. }
11.
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.
Apr 7 '08 #14
Laharl
849 Expert 512MB
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.
Apr 7 '08 #15
We have yet to learn the get version you have showed me, so i do not believe that i can use it on this.
Apr 7 '08 #16
i have this to show so far.
Expand|Select|Wrap|Line Numbers
1. #include <iostream>
2. #include <iomanip>
3. #include <cstring>
4. using namespace std;
5.
6. int main()
7. {
8.
9.     int students,
10.
11.
12.
13.
14.
15.
16.
17.     cout << "Enter in the number of students in the class: \n";
18.     cin >> students;
19.
20.     while (students < 1 || students > 25)
21.     {
22.         cout << "Enter in a number that is no less than 1 and no more than 25: ";
23.         cin >> students;
24.     }
25. for (int count = 1; count <= students; count++)
26. {
27.     char names[25];
28.
29.
30.    cout << "Enter in name " << count << ": \n";
31.    cin.getline(names, 25);
32.    cin.get();
33.
34.
35. }
36.
37. return 0;
38. }
I emailed my professor and he said that
You have to keep 2 more strings (min and max) and to compare the new name with both of them inside the loop. The problem is not thee reading of data, but the processing of each name right after the name,
Could anyone aid me with using the min\max?
Apr 8 '08 #17
I am a programmer of embedded system, I don't konow much about the standard C++ lib. I just cite the follow program as an example. Your program will be simlar to this.

#include <iostream>

using namespace std;

int main()
{
int number;
int value;
int max = 0;
int min = 0;
cout << "Enter in the number of values: ";
cin >> number;
cout << endl;
if(number < 1 || number > 25)
{
cout << "Enter in a number that is no less than 1 and no more than 25: ";
cin >> number;
cout << endl;
}
for(int i = 0; i < number; i++)
{
cout << "Enter a value: ";
cin >> value;
if(i == 0)
{
max = value;
min = value;
continue;
}
if(value > max) max = value;
if(value < min) min = value;
}
cout << "The maximum value is: " << max << endl;
cout << "The minimun value is: " << min << endl;
return 0;
}
Apr 8 '08 #18
I still do not understand how to convert to characters.
Apr 8 '08 #19
SpecialKay
109 100+
i have this to show so far.
Expand|Select|Wrap|Line Numbers
1. #include <iostream>
2. #include <iomanip>
3. #include <cstring>
4. using namespace std;
5.
6. int main()
7. {
8.
9.     int students;
10.     char names[25];
11.
12.     cout << "Enter in the number of students in the class: \n";
13.     cin >> students;
14.
15.     while (students < 1 || students > 25)
16.     {
17.         cout << "Enter in a number that is no less than 1 and no more than 25: ";
18.         cin >> students;
19.     }
20. for (int count = 1; count <= students; count++)
21. {
22.    cout << "Enter in name " << count << ": \n";
23.    cin.getline(names, 25);
24.    cin.get();
25. }
26.
27. return 0;
28. }
I emailed my professor and he said that

Could anyone aid me with using the min\max?
Hum, are you aloud to use Strings? you might not be that advanced yet
P.S i fixed up your code, you should create the char[] outside of the for loop.
Apr 8 '08 #20
i completed the program.
Expand|Select|Wrap|Line Numbers
1.          cout << "Enter in the number of students in the class: \n";
2.          cin >> students;
3.
4.     while (students < 1 || students > 25)
5.     {
6.         cout << "Enter in a number that is no less than 1 and no more than 25: ";
7.         cin >> students;
8.     }
9.     for (int i = 1; i <= students; i++)
10.    {
11.
12.          cout << "Enter in name " << i << ": \n";
13.          cin >> current;
14.     if  (i == 1)
15.     {
16.         strcpy(first, current);
17.         strcpy(last, current);
18.     }
19.     else if (i != 1)
20.     {
21.          if(strcmp(current, first) < 0)
22.             strcpy(first, current);
23.          if(strcmp(current, last) > 0)
24.             strcpy(last,current);
25.     }
26.    }
27.
There it is.
Apr 8 '08 #21