hey guys just joined the forum and so far it looks very promising :D

got a quick question for everyone that can help, i am trying to organize a txt file to output the "gpa" from highest to lowest. Now this is what i have so far...

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){
    
 string line;
 char gpa;   
 ifstream infile;

infile.open("C:\\progpa.txt");  

while(! infile.eof())
{
        getline(infile, line);
        cout<<line<<endl;
        
        }
 
    system("pause");
    return 0;
}

inside the txt file is simply one line: james 3.5

there will eventually be 15 gpa's and names in this file.

I would like to organize it so it display's the name and GPA's from highest to lowest.

I dont know much and would take any help/tips you guys can give. I imagine we have to put a counter/loop inside the file so it can differentiate between the name's and gpa?

Thanks guys

Recommended Answers

All 14 Replies

that loop is constructed incorrectly

while( getline(infile, line) )
{
    cout << line << "\n";
}

>>give. I imagine we have to put a counter/loop inside the file so it can differentiate between the name's and
All you have to do is put the name on the same line as GPA. Look at the file with Notepad.exe or some other text editor so that you can easily see the file's contents.

Thanks dragon. I have them on the same line, my question is how can i let the program know when it hits the gpa? make a loop that scans the txt file that stops when it hits a number?

You have to know the exact arrangement of the file you are trying to read. For example, if the file consists of a series of data arranged by lines with each line consisting of a name and a numerical value separated by a space then you can call the >> operator sequentially to read the name and the numerical value.

The point is that program doesn't know and can never know, you do, so you have to write the program to do what you want it to.

yep the txt file is arranged like that, a name then a space then the gpa(James 3.0) and thanks for the help Lerner. This is one of the things i have the most trouble with and something for some reason i haven't head around. I don't know were to start, to start organizing the GPA's. I'm going to work on pulling the whole file and then maybe transferering it to another txt file for organization and output?

That appears to be more trouble than its worth

std::string name;
float gpa;
infile.open("C:\\progpa.txt");  
while( infile >> name >> gpa)
{
    // now do something with them
}

thanks for the help dragon

ok i did what i wanted to do. I organized the GPA's which were in the txt file by a simple swap like code which i made. My new question is how can i do this but with an unkown number of student's and GPA's inside the txt file. For example a program that will keep up even if i keep on adding to the txt file so i wont have to go back and modify the program everytime i add one more student or GPA. Im thinking arrays to organize these? help plz :D

The while loop that I posted previously will read all the lines in the file no matter how big the file is. It will read a file with just one student or a file that contains a million students.

I have a long way to go, but i eventually want to work my way to make a program that the user enters a Student, Student's Classs Number, classe's Name, student's ID, Credits, Grade.
And then organize the txt file that contains the information for output and display them in this format.

Student Student Class Running Total

Number Name ID Grade GPA Credits

XXXXXX XXXXXXXXXX XXX-XXXX X X.XX XXX

XXX-XXXX X X.XX XXX

XXX-XXXX *X* X.XX XXX

XXX-XXXX X *X.XX* *XXX*


Current GPA: X.XX

Total Credits: XXX

Status: XXXXX


A, B, or C is to be marked with an *. The running GPA includes all Grades that are an A, B, C, D or F. If, at anytime, the running GPA falls below 2.0, tag that GPA line with an * before and after the GPA. Total Credits get the * tag when they exceed 65 credits.

This is an old project i use to have and i wanted to attempt to do it because i couldn't do it before.

I really don't know were to start with this one, when i see this i get overwhelmed lol and thanks again dragon for the info.

So im going to give this a try again and i would appreciate anyone that could guide me a bit :D thanks guys

>>I really don't know were to start with this one, when i see this i get overwhelmed

Start by figuring out exactly what you want to do. Sounds to me like you might want to:

1) ask user to enter input
2) sort user input somehow
3) store write user input in a file
4) retrieve user input from file, analyze it and display it in a given format.

or something like that. So write up exactly what you want the program to do from a very high level first. Then try to expand each high level step one at a time. You can save a little rewriting if you can think a little bit ahead, but don't try to do everything in your head at once.

Thanks lerner..this is what i have whipped up so far...

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){
    
string uinput;
string name;
float gpa;
char classid;
int credits;


ifstream infile;
ofstream outfile;

infile.open("C:\\progpa.txt");
outfile.open("C:\\userinput.txt");

cout<<"Enter record you are looking for:"<<endl;
cin>>uinput;
outfile<<uinput;

while(infile)
{
             infile>>name>>gpa>>classid>>credits;
             if (uinput==name){
                               
                            cout<<"Match Found"<<endl;   
                            cout<<"GPA:"<<gpa<<endl;
                            cout<<"Class:"<<classid<<endl;
                            cout<<"Credits:"<<credits<<endl;
                            
                            
                               }
                               else if(uinput!=name){
                                    cout<<"No Match Found"<<endl;
                                    }
                    
                                        
                    }  
                    

outfile.close();

    system("pause");
    return 0;
}

Only problem im having is when the match is found it display's everything correctly besides the classid. It only shows the first character of the id and then i imagine because this is not displaying correctly it is also messing up the display of the credits.

Here is what i have in the infile(progpa.txt)
james 2.0 cop1220 24
roxi 4.5 cgs2222 15
rojer 3.0 oct1234 9

so when i type in james it shows...

Match found
gpa: 2
class id: c
credits: 2990...

what am i doing wrong that's messing up the output of the class id and credits? Thanks guys

classid is declared to be a single char instead of a string.

lol thanks Lerner fixed the problem...ill let you know how the program is coming. :D

ok so far i have done this to the program

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){
    
string uinput;
string name;
float gpa;
string classid;
int credits;
string answer;


ifstream infile;
ofstream outfile;

infile.open("C:\\progpa.txt");
outfile.open("C:\\userinput.txt");

cout<<"Enter record:"<<endl;
cin>>uinput;
outfile<<uinput;

while(infile)
{
             infile>>name>>gpa>>classid>>credits;
             if (uinput==name){
                               
                            cout<<"Match Found"<<endl;
                            cout<<"Name:"<<name<<endl; 
                            cout<<"do you wish to see or add?"<<endl;
                            cin>>answer;
                            if(answer=="see")
                            {
                            cout<<endl;
                            cout<<"Name:"<<name<<endl;
                            
                            if(gpa<2)
                            cout<<"GPA:"<<gpa<<"*"<<endl;
                            else
                            if(gpa>=2)
                            cout<<"GPA:"<<gpa<<endl;
                            cout<<"Class:"<<classid<<endl;
                            cout<<"Credits:"<<credits<<endl;
                            }
                            
                               }
                               
                    
                    
                    
                    }  
                    

outfile.close();



 
    
    system("pause");
    return 0;
}

Still have a bit to go...but i have a question. Lets say when you enter a record name and it doesn't find a match i want the user to be able to add a new student record, classes, id number and etc... for that record. So i imagine all that info will have to go through the "outfile" then eventually make it's way into the "infile" for future reading/modification. My question is how can i pass the user's input from the "outfile" and into the" infile"? Thanks guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.