Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Need help with beginning C++ homework
New Posts  All Forums:Forum Nav:

Need help with beginning C++ homework

post #1 of 6
Thread Starter 
This is my problem:

A teacher has asked all her students to line up single file according to their first name. For example, in one class Amy will be at the front of the line and Yolanda will be at the end. Write a program that prompts the user to enter the number of students in the class, then loops to read in that many names. Once all the names have been read in it reports which student wourld be at the front of the line and which one would be at the end of the line. You may assume that no two students have the same name. Input Validation: Do not accept a number less than 1 or greater than 25 for the number of students.

Here is what I have so far:

Quote:
#include <iostream>
#include <string>

using namespace std;

int main()
{
int numStudents;
string name;

//string first;
//string last;


// Get number of students
cout << "Enter in the number of students in the class: " << endl;
cin >> numStudents;

// Verify that number of students is greater than one and less than 25
while (numStudents < 1 || numStudents > 25)
{
cout << "The number of students must be more than one and less than 25.";
cin >> numStudents;
}

// Get names until the number of entries eqeals numStudents
for (int count = 1; count <= numStudents; count++)
{
cout << "Please enter the name of student " << count << endl;
cin >> name;


// I still need to display which is the first name and which is the last

(not really using this stuff below just thinking)
// if (name < first)
// cout << name << " is first" << endl;
//
// if (name > last)
// cout << name << " is last" << endl;




system("pause");
return 0;
} // end main
I think my Prof. is only expecting us to get the alphabetical order based on the ASCII code values of the starting letters (only first name).

I have also seen several solutions online using lines similar to:
Quote:
if(strcmp(current, first) < 0)
strcpy(first, current);
However we have not been taught anything with strcmp, or strcpy so I would prefer not to use them.

I'm thinking there must just be a way to to store the values of first and last and then replace the value if a more appropriate is processed for one of those two positions. All I care about is first and last.



If anyone can help me with this I would greatly appreciate it.
Main Desktop PC
(23 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 5820k ASUS x99 Deluxe EVGA GTX 1080 FTW 4 x 4GB 2800 DDR4 Corsair Vengeance 
Hard DriveHard DriveHard DriveHard Drive
256GB Samsung 840 Pro 240GB OCZ ARC100 3TB 7200 RPM Seagate Barracuda 2TB 7200 RPM Seagate Barracuda 
Optical DriveCoolingOSOS
ASUS 24x DVD/Burner Custom Loop Windows 10 Pro 64bit Debian Linux Distro 
MonitorMonitorKeyboardPower
Acer Predator XB270HU ACHIEVA Shimian QH2700 IPSMS Ducky Shine 2 (White LED/Brown Switches) EVGA 1300w G2 + Individually sleeved cables 
CaseMouseMouse PadAudio
Gun Metal NZXT Switch 810 EVGA Troq x10 Logitech G440 Onkyo TX-NR646 
Other
Bitfenix Recon Fan Controller 
  hide details  
Reply
Main Desktop PC
(23 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 5820k ASUS x99 Deluxe EVGA GTX 1080 FTW 4 x 4GB 2800 DDR4 Corsair Vengeance 
Hard DriveHard DriveHard DriveHard Drive
256GB Samsung 840 Pro 240GB OCZ ARC100 3TB 7200 RPM Seagate Barracuda 2TB 7200 RPM Seagate Barracuda 
Optical DriveCoolingOSOS
ASUS 24x DVD/Burner Custom Loop Windows 10 Pro 64bit Debian Linux Distro 
MonitorMonitorKeyboardPower
Acer Predator XB270HU ACHIEVA Shimian QH2700 IPSMS Ducky Shine 2 (White LED/Brown Switches) EVGA 1300w G2 + Individually sleeved cables 
CaseMouseMouse PadAudio
Gun Metal NZXT Switch 810 EVGA Troq x10 Logitech G440 Onkyo TX-NR646 
Other
Bitfenix Recon Fan Controller 
  hide details  
Reply
post #2 of 6
Did you learn vector's or arrays yet?

EDIT:Or linked lists?
The Sig Rig
(19 items)
 
Wifey Rig
(11 items)
 
Xbox PC
(11 items)
 
CPUMotherboardGraphicsRAM
i5-2500k @ 4.8GHz stable ASRock P67 Extreme4 Gen 3 MSI GTX 980 Ti 32GB (4 x 8GB) 1866Mhz Ripjaws X 
Hard DriveHard DriveCoolingOS
256GB Crucial M4 1.5 TB HDD XSPC Raystorm RS360 Windows 7 Professional 
MonitorPowerCaseMouse
Acer 28" 4K Corsair AX750 Corsair 800D SWTOR Razer Naga 
AudioOtherOtherOther
Creative Sound Blaster X-Fi Titanium XSPC RS120 8 x GT AP-15s White Sleeved Cables for Corsair AX750 
CPUMotherboardGraphicsRAM
Core 2 Quad Q6600 @ 3Ghz Asus P5G41T-M LX Plus EVGA GTX 680 2x4 GB 1333Mhz 
Hard DriveHard DriveOptical DriveCooling
500GB WD Black 64 GB Crucial M4 Sony CD/DVD burner H40 w/ GT AP-15s 
OSPowerCase
Windows 7 Home 64 bit Corsair CX430 APEVIA X-Plorer2 Pink 
CPUMotherboardGraphicsRAM
Pentium G620 Foxconn H67S ITX Intel HD 2000 2 x 4GB 1333Mhz 
Hard DriveOptical DriveOSPower
256GB Crucial M4 Old Laptop CD/DVD drive Windows 7 picoPSU-150-XT 
OtherOtherOther
4 x Purple LEDs Power button Tranquility Xbox 360 Skin 
  hide details  
Reply
The Sig Rig
(19 items)
 
Wifey Rig
(11 items)
 
Xbox PC
(11 items)
 
CPUMotherboardGraphicsRAM
i5-2500k @ 4.8GHz stable ASRock P67 Extreme4 Gen 3 MSI GTX 980 Ti 32GB (4 x 8GB) 1866Mhz Ripjaws X 
Hard DriveHard DriveCoolingOS
256GB Crucial M4 1.5 TB HDD XSPC Raystorm RS360 Windows 7 Professional 
MonitorPowerCaseMouse
Acer 28" 4K Corsair AX750 Corsair 800D SWTOR Razer Naga 
AudioOtherOtherOther
Creative Sound Blaster X-Fi Titanium XSPC RS120 8 x GT AP-15s White Sleeved Cables for Corsair AX750 
CPUMotherboardGraphicsRAM
Core 2 Quad Q6600 @ 3Ghz Asus P5G41T-M LX Plus EVGA GTX 680 2x4 GB 1333Mhz 
Hard DriveHard DriveOptical DriveCooling
500GB WD Black 64 GB Crucial M4 Sony CD/DVD burner H40 w/ GT AP-15s 
OSPowerCase
Windows 7 Home 64 bit Corsair CX430 APEVIA X-Plorer2 Pink 
CPUMotherboardGraphicsRAM
Pentium G620 Foxconn H67S ITX Intel HD 2000 2 x 4GB 1333Mhz 
Hard DriveOptical DriveOSPower
256GB Crucial M4 Old Laptop CD/DVD drive Windows 7 picoPSU-150-XT 
OtherOtherOther
4 x Purple LEDs Power button Tranquility Xbox 360 Skin 
  hide details  
Reply
post #3 of 6
Thread Starter 
Quote:
Originally Posted by Ironman517 View Post
Did you learn vector's or arrays yet?

EDIT:Or linked lists?
We are actually on our first chapter of looping and learning while, for, do while, nesting, if then, and etc.

Most the things we have tried have involved integers and numbers (for example find out how many kids are is a class then ask for their test score and find the average). That is while this alphabetical order stuff and strings started to confuse me.
Main Desktop PC
(23 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 5820k ASUS x99 Deluxe EVGA GTX 1080 FTW 4 x 4GB 2800 DDR4 Corsair Vengeance 
Hard DriveHard DriveHard DriveHard Drive
256GB Samsung 840 Pro 240GB OCZ ARC100 3TB 7200 RPM Seagate Barracuda 2TB 7200 RPM Seagate Barracuda 
Optical DriveCoolingOSOS
ASUS 24x DVD/Burner Custom Loop Windows 10 Pro 64bit Debian Linux Distro 
MonitorMonitorKeyboardPower
Acer Predator XB270HU ACHIEVA Shimian QH2700 IPSMS Ducky Shine 2 (White LED/Brown Switches) EVGA 1300w G2 + Individually sleeved cables 
CaseMouseMouse PadAudio
Gun Metal NZXT Switch 810 EVGA Troq x10 Logitech G440 Onkyo TX-NR646 
Other
Bitfenix Recon Fan Controller 
  hide details  
Reply
Main Desktop PC
(23 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 5820k ASUS x99 Deluxe EVGA GTX 1080 FTW 4 x 4GB 2800 DDR4 Corsair Vengeance 
Hard DriveHard DriveHard DriveHard Drive
256GB Samsung 840 Pro 240GB OCZ ARC100 3TB 7200 RPM Seagate Barracuda 2TB 7200 RPM Seagate Barracuda 
Optical DriveCoolingOSOS
ASUS 24x DVD/Burner Custom Loop Windows 10 Pro 64bit Debian Linux Distro 
MonitorMonitorKeyboardPower
Acer Predator XB270HU ACHIEVA Shimian QH2700 IPSMS Ducky Shine 2 (White LED/Brown Switches) EVGA 1300w G2 + Individually sleeved cables 
CaseMouseMouse PadAudio
Gun Metal NZXT Switch 810 EVGA Troq x10 Logitech G440 Onkyo TX-NR646 
Other
Bitfenix Recon Fan Controller 
  hide details  
Reply
post #4 of 6
Quote:
Originally Posted by Accuracy158 View Post
All I care about is first and last.
Ahh i didnt see this. disregard my question about lists. How much have you learned about strings? Have you learned strcmp()?

EDIT:

Here try looking here
http://www.cplusplus.com/reference/c...string/strcmp/

strcmp() returns either a int > 0 (indicating a difference, string a is greater than string b), 0 (indicating equal strings), or a int < 0 (indicating a difference, string a is less than string b).

strcmp() uses ctyle strings (char*). so you will need to use... string.c_str() as inputs. Sorry, its been a while since my beginner C++ courses
Edited by Ironman517 - 10/21/11 at 12:17pm
The Sig Rig
(19 items)
 
Wifey Rig
(11 items)
 
Xbox PC
(11 items)
 
CPUMotherboardGraphicsRAM
i5-2500k @ 4.8GHz stable ASRock P67 Extreme4 Gen 3 MSI GTX 980 Ti 32GB (4 x 8GB) 1866Mhz Ripjaws X 
Hard DriveHard DriveCoolingOS
256GB Crucial M4 1.5 TB HDD XSPC Raystorm RS360 Windows 7 Professional 
MonitorPowerCaseMouse
Acer 28" 4K Corsair AX750 Corsair 800D SWTOR Razer Naga 
AudioOtherOtherOther
Creative Sound Blaster X-Fi Titanium XSPC RS120 8 x GT AP-15s White Sleeved Cables for Corsair AX750 
CPUMotherboardGraphicsRAM
Core 2 Quad Q6600 @ 3Ghz Asus P5G41T-M LX Plus EVGA GTX 680 2x4 GB 1333Mhz 
Hard DriveHard DriveOptical DriveCooling
500GB WD Black 64 GB Crucial M4 Sony CD/DVD burner H40 w/ GT AP-15s 
OSPowerCase
Windows 7 Home 64 bit Corsair CX430 APEVIA X-Plorer2 Pink 
CPUMotherboardGraphicsRAM
Pentium G620 Foxconn H67S ITX Intel HD 2000 2 x 4GB 1333Mhz 
Hard DriveOptical DriveOSPower
256GB Crucial M4 Old Laptop CD/DVD drive Windows 7 picoPSU-150-XT 
OtherOtherOther
4 x Purple LEDs Power button Tranquility Xbox 360 Skin 
  hide details  
Reply
The Sig Rig
(19 items)
 
Wifey Rig
(11 items)
 
Xbox PC
(11 items)
 
CPUMotherboardGraphicsRAM
i5-2500k @ 4.8GHz stable ASRock P67 Extreme4 Gen 3 MSI GTX 980 Ti 32GB (4 x 8GB) 1866Mhz Ripjaws X 
Hard DriveHard DriveCoolingOS
256GB Crucial M4 1.5 TB HDD XSPC Raystorm RS360 Windows 7 Professional 
MonitorPowerCaseMouse
Acer 28" 4K Corsair AX750 Corsair 800D SWTOR Razer Naga 
AudioOtherOtherOther
Creative Sound Blaster X-Fi Titanium XSPC RS120 8 x GT AP-15s White Sleeved Cables for Corsair AX750 
CPUMotherboardGraphicsRAM
Core 2 Quad Q6600 @ 3Ghz Asus P5G41T-M LX Plus EVGA GTX 680 2x4 GB 1333Mhz 
Hard DriveHard DriveOptical DriveCooling
500GB WD Black 64 GB Crucial M4 Sony CD/DVD burner H40 w/ GT AP-15s 
OSPowerCase
Windows 7 Home 64 bit Corsair CX430 APEVIA X-Plorer2 Pink 
CPUMotherboardGraphicsRAM
Pentium G620 Foxconn H67S ITX Intel HD 2000 2 x 4GB 1333Mhz 
Hard DriveOptical DriveOSPower
256GB Crucial M4 Old Laptop CD/DVD drive Windows 7 picoPSU-150-XT 
OtherOtherOther
4 x Purple LEDs Power button Tranquility Xbox 360 Skin 
  hide details  
Reply
post #5 of 6
Thread Starter 
Quote:
Originally Posted by Ironman517 View Post
Ahh i didnt see this. disregard my question about lists. How much have you learned about strings? Have you learned strcmp()?
No... I have seen several solutions online involving that but we did not discuss it. Our text actually uses char[] for most things but the Prof. showed us a very little about strings and said it would be easier and more useful.
Main Desktop PC
(23 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 5820k ASUS x99 Deluxe EVGA GTX 1080 FTW 4 x 4GB 2800 DDR4 Corsair Vengeance 
Hard DriveHard DriveHard DriveHard Drive
256GB Samsung 840 Pro 240GB OCZ ARC100 3TB 7200 RPM Seagate Barracuda 2TB 7200 RPM Seagate Barracuda 
Optical DriveCoolingOSOS
ASUS 24x DVD/Burner Custom Loop Windows 10 Pro 64bit Debian Linux Distro 
MonitorMonitorKeyboardPower
Acer Predator XB270HU ACHIEVA Shimian QH2700 IPSMS Ducky Shine 2 (White LED/Brown Switches) EVGA 1300w G2 + Individually sleeved cables 
CaseMouseMouse PadAudio
Gun Metal NZXT Switch 810 EVGA Troq x10 Logitech G440 Onkyo TX-NR646 
Other
Bitfenix Recon Fan Controller 
  hide details  
Reply
Main Desktop PC
(23 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 5820k ASUS x99 Deluxe EVGA GTX 1080 FTW 4 x 4GB 2800 DDR4 Corsair Vengeance 
Hard DriveHard DriveHard DriveHard Drive
256GB Samsung 840 Pro 240GB OCZ ARC100 3TB 7200 RPM Seagate Barracuda 2TB 7200 RPM Seagate Barracuda 
Optical DriveCoolingOSOS
ASUS 24x DVD/Burner Custom Loop Windows 10 Pro 64bit Debian Linux Distro 
MonitorMonitorKeyboardPower
Acer Predator XB270HU ACHIEVA Shimian QH2700 IPSMS Ducky Shine 2 (White LED/Brown Switches) EVGA 1300w G2 + Individually sleeved cables 
CaseMouseMouse PadAudio
Gun Metal NZXT Switch 810 EVGA Troq x10 Logitech G440 Onkyo TX-NR646 
Other
Bitfenix Recon Fan Controller 
  hide details  
Reply
post #6 of 6
I'm not sure i quite understand the task but this code seems to do the required output without using anything from the STL or that kind of jazz. Might give you an idea or something.



Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{ 
int numStudents;

const int LIST_LENGTH = 26;
char characterList[LIST_LENGTH] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; /*used to indentify letters as values
                                                             for example: 0 = a, 1 = b, 2 = c ect.*/

// Get number of students
cout << "Enter in the number of students in the class: " << endl;
cin >> numStudents;



// Verify that number of students is greater than one and less than 25 
while (numStudents < 1 || numStudents > 25)
{
cout << "The number of students must be more than one and less than 25.";
cin >> numStudents;
}

string name = ""; //stores enterd name
string firstName = ""; //stores name at the top of the list
string lastName = "";  //stores name at the bottom of the list

int firstNameHead = 26; /*stores the value of the earliest character that has been
                        entred in a name, for example: if 'john' was entered, the letter
                        'j' would be recognized, and stored as the value 9 since 
                        characterList[9] = 'j'*/

int lastNameHead = 0; //same as firstNameHead but for the the last in the list   

int compare;   //used to compare names first letter to the one at the bottom and top of the lists
               


// Get names until the number of entries eqeals numStudents
for (int count = 1; count <= numStudents; count++)
{
cout << "Please enter the name of student " << count << endl;
cin >> name;


for(int i = 0; i < LIST_LENGTH; i++)      
{                                             //iterates through the character list array trying to match the enterd
if(name[0] == characterList[i])              //name's first letter with a character from characterList.
compare = i;                                 //When it finds the corresponding letter, it assign the value of the letter in
}                                              //characterList for comparison                                 

if(compare < firstNameHead)               //essentially checks if the name enterd should be higher in the list than the one currently   
{                                        //If the value of first letter in the entered name is lower than the one stored
           firstName = name;             //in firstNameHead, make the name enterd the new top of the list
           firstNameHead = compare;
}

if(compare > lastNameHead)              
{                                      //essentially checks if the name enterd should be lower in the list than the one currently  
           lastName = name;           //If the value of first letter in the entered name is higher than the one stored
           lastNameHead = compare;    //in lastNameHead, make the name enterd the new bottom of the list
}

}

cout<<"First name is: "<<firstName<<endl;
cout<<"Last name is: "<<lastName<<endl;


system("pause");
return 0;
}
Dolans Mate
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 760 Gigabyte P55A-UD3 Gainward GTX 560Ti Mushkin Radioactive (4GB) 
Hard DriveCoolingOSMonitor
160GB Seagate x 2 Cooler Master Hyper 212 EVO Windows 7 Ultimate 64-bit LG LED Flatron E2250V x 2 
KeyboardPowerCaseMouse Pad
Das Keyboard Utimate S  Silverstone Strider 500w NZXT Guardian 921 Razer Goliathus Fragged Alpha 
Audio
Logitech X-540s 
  hide details  
Reply
Dolans Mate
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 760 Gigabyte P55A-UD3 Gainward GTX 560Ti Mushkin Radioactive (4GB) 
Hard DriveCoolingOSMonitor
160GB Seagate x 2 Cooler Master Hyper 212 EVO Windows 7 Ultimate 64-bit LG LED Flatron E2250V x 2 
KeyboardPowerCaseMouse Pad
Das Keyboard Utimate S  Silverstone Strider 500w NZXT Guardian 921 Razer Goliathus Fragged Alpha 
Audio
Logitech X-540s 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Need help with beginning C++ homework