reading in chars from file

This is a discussion on reading in chars from file within the C++ Programming forums, part of the General Programming Boards category; ok i have a few questions about this code. it is supposed to display the first name alphabetically and the ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 04:02 AM
  3. reading and writing unsigned chars from a file
    By yahn in forum C++ Programming
    Replies: 17
    Last Post: 02-20-2006, 08:42 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21