473,387 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 9588
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.

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

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,208 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
wyjsjh
3
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

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Sue | last post by:
<html> Is there someone here that can help me validate the period as the fourth from the last character in an email address. There is other information and validation on the form I have to do but...
8
by: Sue | last post by:
Hello! I am back with another question. Remember I am a new JavaScript student and I am aware that this code does not check for all the possibilities and that as a "NEW" JavaScript student I am...
16
by: Kirk Bevins | last post by:
Hello, new to posting, got a dilema in c++. I cant seem to create new instances of my student class. The idea is to make a database where the user inputs surnames and library card numbers etc. The...
1
by: Pieter Linden | last post by:
Hi, I think the subject line pretty much says it all... Say I have a students-classes database and I add a twist. I want to filter what courses a student can take by comparing the courses he...
3
by: sandy | last post by:
I am a student who is losing his mind. The code below is a header file which has the line: void ClearList(ListType *list); which generates the following compile time errors: variable or...
7
by: RallyDSM | last post by:
Hello, I'm currently trying to read a .CSV file and get all the data into an array so I can work with it in the program. Here is what I currently have. Private Sub IntializeData() Dim AL...
2
by: sallyk07 | last post by:
Modify the Student class so that each student object should also contain the scores for three tests. Provide a constructor that sets all instance values based on parameter values. Overload the...
31
by: Warly girl | last post by:
Hi i have a qustion plz help me to understand and solve it Phase One Problem description You are required to implement a student registration system. The system keeps information about the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.