Reading in a file of names and printing them out alphabetically

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

41 Replies - 1724 Views - Last Post: 11 April 2011 - 09:55 PM Rate Topic: -----

#1 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Reading in a file of names and printing them out alphabetically

Posted 11 April 2011 - 12:47 PM

I'm reading in 25 names from a file, and printing them out alphabetically according to last name! I'm reading from a file called names.txt. The names will be in first last format with one name per line.
I'm to use an array and strings to complete this.
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    char names[25];
    string input, first;
    getline(cin, input);
    first = input;
    
    ifstream infile;
    infile.open("names.txt");
    infile >> names;
    
    cout << names << endl;
      
                 
system("PAUSE");
return 0;    
}



right now I'm just trying to get it to print the names out and once I get that accomplished i will continue with how to get them printed out in alphabetic order!!!

Is This A Good Question/Topic? 0
  • +

Replies To: Reading in a file of names and printing them out alphabetically

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 665
  • View blog
  • Posts: 2,296
  • Joined: 31-December 10

Re: Reading in a file of names and printing them out alphabetically

Posted 11 April 2011 - 12:52 PM

You need to use a loop to read in all the names. Something like:
while(infile >> names)
    cout << names << endl;



I just noticed that you didn't include the string header, yet you declare two string variables. I don't know if using a char array is required, if not, you should just use strings because they'll make it easier for you.

This post has been edited by vividexstance: 11 April 2011 - 12:54 PM

Was This Post Helpful? 1
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Reading in a file of names and printing them out alphabetically

Posted 11 April 2011 - 12:54 PM

you can use std::sort on an array of strings.

#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>

int main() {
   std::string Names[25];
   std::string first,last;
   //..get all the names
   std::sort(Names,Names+25);
   //..display sorted names 
}


here is a link to some documentation
Was This Post Helpful? 0
  • +
  • -

#4 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Re: Reading in a file of names and printing them out alphabetically

Posted 11 April 2011 - 01:06 PM

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    string names[25]; 
    ifstream inFile;
    inFile.open("names.txt");
    while(inFile >> names[25])
    {
                 cout << names[25] << endl;
    }
      
                 
system("PAUSE");
return 0;    
}


This compiles but when i run it, it closes the program and says its not working properly!
Was This Post Helpful? 0
  • +
  • -

#5 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Reading in a file of names and printing them out alphabetically

Posted 11 April 2011 - 01:10 PM

first off your code is likly to crash or have strange behavior because your accsesing the array out of bounds names[25]
Was This Post Helpful? 1
  • +
  • -

#6 platrelefs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 31-March 09

Re: Reading in a file of names and printing them out alphabetically

Posted 11 April 2011 - 01:16 PM

I would suggest throwing some try catch statements in there as well. The file reading/writing methods would be happier with you :)
Was This Post Helpful? 0
  • +
  • -

#7 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Re: Reading in a file of names and printing them out alphabetically

Posted 11 April 2011 - 01:28 PM

I'm not really sure what that means! =/ I'm very new to computer programming and not very good at it at all. I've declared the array of 25, so shouldn't it print out the names?
Was This Post Helpful? 0
  • +
  • -

#8 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Re: Reading in a file of names and printing them out alphabetically

Posted 11 April 2011 - 01:39 PM

I've changed the string part to Char, and now it will print out the names that I want, but there is just one problem. When they print they print out like this...
tweety
bird
tom
sawyer
They won't print out the whole name on one line?
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    char names[25]; 
    ifstream inFile;
    inFile.open("names.txt");
    while(inFile >> names)
    {
                 cout << names << endl;
    }
      
                 
system("PAUSE");
return 0;    
}

Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,690
  • Joined: 25-December 09

Re: Reading in a file of names and printing them out alphabetically

Posted 11 April 2011 - 01:47 PM

First this is a C++ program so use the std::string class. Second to get the entire line you should be using the getline() function. See these links: getline() for string or getline() for C-string.

But please stick with the std::strings.

Jim
Was This Post Helpful? 1
  • +
  • -

#10 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Re: Reading in a file of names and printing them out alphabetically

Posted 11 April 2011 - 01:56 PM

Whenever I use the strings it gives me all kinds of errors! But if I use char it works and prints them out! This is how I did it!
int main()
{
    string names[25]; 
    ifstream inFile;
    inFile.open("names.txt");
    while(inFile >> names)
    {
                 cout << names << endl;
    }

would it work if I did it like this?
 char stringNames[25] 

Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,690
  • Joined: 25-December 09

Re: Reading in a file of names and printing them out alphabetically

Posted 11 April 2011 - 02:10 PM

In the following snippet you have defined an array of 25 strings named names. Yet when you try to extract the names from the file you are not using array notation.

   string names[25]; 

    while(inFile >> names)



The while should be: while(infile >> names[i]).

Jim
Was This Post Helpful? 1
  • +
  • -

#12 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Re: Reading in a file of names and printing them out alphabetically

Posted 11 April 2011 - 02:21 PM

I tried that too! I still get errors like no match for 'operator>>' in 'inFile >> names'
and then i get a bunch of little notes that say

note C:\Dev-Cpp\include\c++\3.4.2\bits\istream.tcc:87std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]

Why does it work when I use char but not when I use string?
also when I use get line it tells me there's no function call for getline..
Was This Post Helpful? 0
  • +
  • -

#13 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 665
  • View blog
  • Posts: 2,296
  • Joined: 31-December 10

Re: Reading in a file of names and printing them out alphabetically

Posted 11 April 2011 - 02:45 PM

you need to put this at the top of your program:
#include <string>



The reason is that char is a built-in type, and string is a standard library class that someone wrote to make dealing with strings easier than having to use C-style NULL-terminated strings. So like someone else already said, stick with strings.

This post has been edited by vividexstance: 11 April 2011 - 02:46 PM

Was This Post Helpful? 2
  • +
  • -

#14 collegejunkie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-February 11

Re: Reading in a file of names and printing them out alphabetically

Posted 11 April 2011 - 02:48 PM

I have that at the top thats why I don't understand why this isn't working?? and I've got the using std::getline; and it won't let me use getline without giving me and error either!?!? and eventually it will compile but when i run it, the program shuts down and says its not working properly!
#include <iostream>
using namespace std;

#include <fstream>
#include <string>
using std::string;
using std::getline;


int main()
{
    string name[25]; 
    
    ifstream inFile;
    inFile.open("names.txt");
    while(inFile >> name[25])
    {
                 getline(cin, name[25]);
    }
    

      
                 
system("PAUSE");
return 0;    
}

Was This Post Helpful? 0
  • +
  • -

#15 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 665
  • View blog
  • Posts: 2,296
  • Joined: 31-December 10

Re: Reading in a file of names and printing them out alphabetically

Posted 11 April 2011 - 02:52 PM

put those #include statements right above the 'using namespace std' and before the #include <iostream> and see what happens. Also, when you're using arrays, array indexing starts at 0 and goes to N-1 where N is the size of the array you declared. You have it at 25, so the highest element in the array you can access is 24, not 25. You're trying to get an element that is out of the range of that array, which is an error. Instead you need to create an integer initialized to zero, use that to index the array, and increment that integer by one after you input a name.

This post has been edited by vividexstance: 11 April 2011 - 02:54 PM

Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3