How can I align the text horizontally and show every name

If you run this program all the names of every college and university will be aligned vertically and quite frankly it looks like a mess.

For example, the name of this college in the program looks like this:
Zion
Bible
College

and I want it to look like this: Zion Bible College.

The next problem is that it doesn't show every college and university(I think it shows from W to Z and that's it. maybe because the file is to big?). How can I solve the problem?

Here is a sample of what appears in the text file.

1
2
3
4
5
6
7
8
9
10
11
name
A T Still University of Health Sciences
Abilene Christian University
Abraham Baldwin Agricultural College
Academy College
Academy For Five Element Acupuncture
Academy of Art University
Academy of Chinese Culture and Health Sciences
Academy of Oriental Medicine
ACT College
Acupuncture & Integrative Medicine College-Berkeley



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void collegesUniversities()
{
    ifstream campuses;
    campuses.open("colleges.txt");

    string schools;

    if(!campuses)
        cerr << "Error opening file. ";
    else
    {
      while(campuses.good())
       {
         getline(campuses,schools, '\n');
         cout << schools << endl;
       }
    }
    campuses.close();
}

int main()
{
   collegesUniversities();
}
Last edited on
closed account (SECMoG1T)
Here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
   while(campuses >> schools)
      {
          cout << schools << "\n"; 
       }

       ///Might be you may use 
  
    

    while (campuses) /// chechs if the stream is fine.
     {
        getline (campuses,schools, '\n');
        cout<<schools<< endl;
     }
Last edited on
You can think of a string as an array of chars that copies until null termination. Your program is reading one string at a time then starting a new line. You can use the getline() function to get the whole line.
Well that solves the first problem but now there is a new problem.

This new problem is that it keeps repeating the name of different colleges and university several times like this:

Zion Bible college
Zion Bible college
Zion Bible college

and it still doesn't show every college and university. I'm pretty sure that every college and university is there but because there are too many characters in the file, it only shows a fraction of the entire thing.

edit1- Hold on a moment I think I found something.
edit2- Well I found that in the text file some colleges do repeat, but Zion Bible college for sure doesn't repeat so I don't know why it's repeating in the program when there's only one Zion Bible College
Last edited on
closed account (SECMoG1T)
It would be better if your function is void coz , it seems to me that your function accomplishes
It's task without necessarily returning a string.

/// that return will also be print accounting for one of you'r duplicate

1
2
my baed i av noted my checker while(campuses) allows an extra iteration sorry for that use 
           while (campuses.good ())
Last edited on
Good point

Alright, everything looks good but the main problem is why doesn't it shows every college and university on the console window?
Last edited on
closed account (SECMoG1T)
Hehe din't ge it but try this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
   #include <iostream> 
   #include <fstream>
   #include <string> 
   #include <cstdlib>

   using namespace std;

   void collegesUniversities()
     { 
         ifstream campuses("colleges.txt"); /// constructor opens file
         string schools; 

         if(!campuses) {cerr << "Error opening file. "; exit (1);}

         while(campuses.good()) 
        {
             getline(campuses,schools, '\n'); 
             cout << schools << endl; 
         } 
         campuses.close ();
    } 

   int main() 
    { 
      collegesUniversities();
    }
Last edited on
Alright looks good.
Topic archived. No new replies allowed.