Thread: reading in chars from file

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    30

    reading in chars from file

    ok i have a few questions about this code. it is supposed to display the first name alphabetically and the last name alphabetically from the list.

    1. i don't think the strcpy function has been covered yet in the book so i was wondering if there was a different way to assign the current char to the first and last variables.

    2. when i used the strcpy function i got a warning saying i should use strcpy_s instead. i have no idea what the difference is could someone explain.

    3. when i read in the file it prints to the screen just right but the "last" student is displayed as what i initilized it as which is "aaaaa". how come the name after that alphabetically is not being stored in the "last" variable?

    here is my code so far:

    Code:
    #include <iostream>
    #include <cstring>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    	ifstream infile;
    	infile.open("LineUp.dat");
    
    	if (!infile)
    	{
    		cout << "Corrupt File " << endl;
    	}
    
    	int numStudents = 0;
    	char first[21] = "zzzzzz";
    	char last[21] = "aaaaaaa";
    	char name[21];
    
    	while (infile >> name)
    	{
    
    		if (strcmp(name,first) < 0)
    		{
    			strcpy_s(first, name);
    		}
    		if (strcmp(name,last) > 0)
    		{
    			strcpy_s(last, name);
    		}
    		cout << name << endl;
    		numStudents++;
    	}
    
    	cout << "The first student is " << first << endl;
    	cout << "The last student is " << last << endl;
    	cout << "There are " << numStudents << " students. " << endl;
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    30
    someone please help

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > #include <cstring>
    Since this is C++, why are you using C strings at all?
    They're horrible and bug prone compared to the C++ version.

    Code:
    #include <string>
    	string first = "zzzzzz";
    	string name;
    
    	while (infile >> name)
    	{
    
    		if ( name < first )
    		{
    			first = name;
    		}
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. reading and writing unsigned chars from a file
    By yahn in forum C++ Programming
    Replies: 17
    Last Post: 02-20-2006, 09:42 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM