How to read the data as a whole string?

This is a discussion on How to read the data as a whole string? within the C++ Programming forums, part of the General Programming Boards category; Whitewater High School has contracted you to write a program that will read a file of student names and test ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    67

    Question How to read the data as a whole string?

    Whitewater High School has contracted you to write a program that will
    read a file of student names and test scores in the formation FIRST
    NAME LAST NAME: SCORE1 SCORE2 SCORE3, where each of the scores is an
    integer. Then your program is to print a report, sorted by last name
    with each of the last names fully capitalized (that is, if you read in
    Wilkinson you print WILKINSON) followed by a comma, then the first
    name, then a colon, then the scores, then the average for the student,
    and lastly a letter grade (based upon 90 percent or greater being an
    "A," 80 percent or greater being a "B," and so forth). At the end of
    the report the averages for each test are to be printed also. Needless
    to say, the report must be written to a file and must be "pretty" (in
    proper columns and such). (HINT: Read in the whole line as a string,
    switch the first and last name, sort the names, then compute the math)
    Data file: data.txt

    The data file content is:

    Mary Jones: 89 90 100
    Tom Brown: 100 99 100
    John Smith:66 80 98
    Englebert Humberdink: 85 87 88
    Tom Jones: 76 78 89
    Paul McCartney: 88 88 99
    Olivia Newton: 77 66 98
    Susan Barlow: 87 98 89
    Robert Payne: 88 44 76
    George Franklin: 77 88 99
    Margaret Ibach: 87 89 90
    Maggie Chang: 99 99 100
    Blaire Bates: 89 87 78
    John Lennon: 88 77 99
    Silvia Stalone: 66 55 88
    Pepper Johnson: 90 80 99
    Alfred Newman: 80 80 90
    Hugh Walker: 56 90 70
    Maureen Ferguson: 100 90 70
    Cho Zhang: 99 99 99
    Lisa Graham: 80 87 78
    Paul Revere: 89 78 90
    Franklin Smythe: 88 88 100
    Mark Anderson: 100 90 80
    Toni Interbitzen: 90 90 90
    My question is how to read the data as a whole string?you know for each line,there are firstname,lastname,and 3 scores.They are different type,how could do that?
    And also if I do this,how do I switch the firstname and lastname,and how to compute the ave score?(How do I take the 3 scores out of the whole string?)


    OK,guys,I already finish my code but it also has some strange errors.Please help me to fix.
    Code:
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    void loadData(string firstname[],string lastname[],int score1[],int score2[],int score3[])
    {
         int i=0;
         ifstream in;
         in.open("data.txt");
         if(in.fail())
         {
            in.close();
            cout<<"Data.txt not found,program ending!"<<endl;
            exit(1);
         }
         while(in >> firstname[i] >> lastname[i] >> score1[i] >> score2[i] >> score3[i])
         i++;
    }
    
    void ChangeLastname(string &lastname)
    {
    	for(size_t i=0;i<lastname.length();i++)
    	{
    		lastname[i] = toupper(lastname[i]);
    	}
    }
    
    void sorting(string lastname[],string firstname[],int score1[],int score2[],int score3[])
    {
          for(int i=0;i<25;i++)
         {
              for(int j=0; j<25; j++)
    
           { { if(lastname[i] < lastname[j])
             swapValues2(lastname[i],lastname[j]);
             swapValues2(firstname[i],firstname[j]);
             swapValues(score1[i],score1[j]);
             swapValues(score2[i],score2[j]);
             swapValues(score3[i],score3[j]); }
           }
    }
    void swapValues2(string& v1,string& v2)
    {
              string temp;
              temp=v1;
              v1=v2;
              v2=temp;
    }
    
    void swapValues(int& v1,int& v2)
    {
         int temp;
         temp=v1;
         v1=v2;
         v2=temp;
    }
    
    void grade(int score1[],int score2[],int score3[],int ave[],int aveScore[],int sum[],string rank[])
    {
    
         for(int i=0;i<25;i++)
        { ave[i]=(score1[i]+score2[i]+score3[i])/3;
          if(ave[i]>=90)
          rank[i]="A";
          else if(ave[i]<90&&ave[i]>=80)
          rank[i]="B";
          else if(ave[i]<80&&ave[i]>=70)
          rank[i]="C";
          else if(ave[i]<70&&ave[i]>=60)
          rank[i]="D";
         sum[0]+=score1[i];
         sum[1]+=score2[i];
         sum[2]+=score3[i];}
         for(int j=0;j<3;j++)
         aveScore[j]=sum[j]/25 }
    void WriteToFile(string lastname[],string firstname[],int score1[],int score2[],int score3[],int score[],int aveScore[],string rank[])
    {
         int i=0,j=0;
         ofstream out;
         out.open("data2.txt")
         if(out.fail())
         {
            out.close();
            cout<<"data2.txt not found,program ending!"<<endl;
            exit(1);
         }
         out<<setw(20)<<"NAME"<<setw(4)<<"SCORE1"<<setw(4)<<"SCORE2"<<setw(4)<<"SCORE3"<<setw(4)<<"RANK";
         while(out<<setw(20)<<lastname[i]<<","<<firstname[i]<<":"<<setw(4)<<score1[i]<<setw(4)<<score2[i]<<setw(4)<<score3[i]<<setw(4)<<rank[i]<<endl)
         i++;
         out<<setw(20)<<"TEST AVERAGE"<<setw(4)<<aveScore[0]<<setw(4)<<aveScore[1]<<setw(4)<<aveScore[2]<<endl;}
    
    int main(int argc, char *argv[])
    {
        string firstname[25], lastname[25];
        int score1[25], score2[25], score3[25],aveScore;
        loadData(string firstname[],string lastname[],int score1[],int score2[],int score3[]);
        ChangeLastname(lastname);
        sorting(string lastname[],string firstname[],int score1[],int score2[],int score3[]);
        grade(int score1[],int score2[],int score3[],int score[],int aveScore[],int sum[]);
        writeToFile(string lastname[],string firstname[],int score1[],int score2[],int score3[],int score[],int aveScore[],string rank[]);
        system("pause");
        return 0;
    }
    the complier said1.)"implicit declaration of function `swapValues2(...)'"and also for swapValues.
    (2.)"temp,v1,v2"undeclared and also"
    (3.) warning: cannot pass objects of type `basic_string<char,string_char_traits<char>,__defa ult_alloc_template<false,0> >' through `...'".
    for(1)and(3),I really don't know how to do.But for(2),That is really Great!I am sure I declared them,but why?
    Last edited by tx1988; 05-28-2007 at 08:07 PM.

  2. #2
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    67
    Quote Originally Posted by Sentral View Post
    sorry,I don't know your meaning.I don't think I did something wrong!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    As Sentral noted, try to do it first, then come back with what you have tried.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    67
    Quote Originally Posted by laserlight View Post
    As Sentral noted, try to do it first, then come back with what you have tried.
    I have the whole idea,but I need to know how to read the data in a string.This is the beginning point.

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    67
    Quote Originally Posted by tx1988 View Post
    I have the whole idea,but I need to know how to read the data in a string.This is the beginning point.
    After this,I can do the project by myself.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    There's an imaginatively named function called 'getline'.
    Perhaps you could start with that.
    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.

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    67
    Quote Originally Posted by Salem View Post
    There's an imaginatively named function called 'getline'.
    Perhaps you could start with that.
    But after that,how do I take the lastname out and sorting for it?since the whole information in one string now.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Once you have it in a string, then you can parse it. So what can you do with a string object? How about googling for something like "C++ string" or "C++ class string"?

    Also check for ways of extracting data from strings. There's some class in C++ that can do this, similar to sscanf() in C.

  10. #10
    Registered User
    Join Date
    May 2007
    Posts
    67
    Quote Originally Posted by MacGyver View Post
    Once you have it in a string, then you can parse it. So what can you do with a string object? How about googling for something like "C++ string" or "C++ class string"?

    Also check for ways of extracting data from strings. There's some class in C++ that can do this, similar to sscanf() in C.
    But I am a very super beginner,and our professor's problem is much harder than the book we used(Problem Solving with C++,6e).And I am only learn C++ 2 months,and have no foundation.I really don't know many functions and class.In fact,we didn't learn class yet!Our professor is just showing his program in class and didn't explain to us very clearly.I can just learn from the book which is much more easier than his lecture.
    Ai,GOD!

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Maybe your professor doesn't know how to teach you, or maybe the fault lies with you. Either way, if you want to get this assignment done, you have to kick it into gear and research this.

    Since you say you're a beginner, I hope you take seriously and remember these two things.

    1) Experience teaches you better sometimes than just reading a fact from a book or a reference.

    2) Programming is not just about writing code. You need to learn how to learn, and be able to meet different challenges as they arise. Important tools: Google, MSDN (for Windows coding), man pages (for *nix coding). If you can't use at least one of those at this point, get cracking and figure it out. It's not so much knowing the answer to a problem as much as knowing how to find out the answer to a problem that will most likely help you later on.

  12. #12
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    I would ditch using getline since every line has the same format and you need to parse it anyway.

    I would basically read each line, item by item with the >> operator. Look up ifstream for more inf

    Code:
    file >> fname >> lname >> score1 >> score2 >> score3;

  13. #13
    Registered User
    Join Date
    May 2007
    Posts
    67
    Quote Originally Posted by Darryl View Post
    I would ditch using getline since every line has the same format and you need to parse it anyway.

    I would basically read each line, item by item with the >> operator. Look up ifstream for more inf

    Code:
    file >> fname >> lname >> score1 >> score2 >> score3;
    I am doing the same way you did instead of reading into one string.

  14. #14
    Registered User
    Join Date
    May 2007
    Posts
    67
    Quote Originally Posted by MacGyver View Post
    Maybe your professor doesn't know how to teach you, or maybe the fault lies with you. Either way, if you want to get this assignment done, you have to kick it into gear and research this.

    Since you say you're a beginner, I hope you take seriously and remember these two things.

    1) Experience teaches you better sometimes than just reading a fact from a book or a reference.

    2) Programming is not just about writing code. You need to learn how to learn, and be able to meet different challenges as they arise. Important tools: Google, MSDN (for Windows coding), man pages (for *nix coding). If you can't use at least one of those at this point, get cracking and figure it out. It's not so much knowing the answer to a problem as much as knowing how to find out the answer to a problem that will most likely help you later on.
    Thank you,man!I will try to do that and hope I can do better!

  15. #15
    Registered User
    Join Date
    May 2007
    Posts
    67
    Another question is how to sorting an string array by their first letter?(I mean how to compare
    the first letter of each word?)

    how about this?Why it is wrong?
    void ChangeLastname(string lastname[],string LASTNAME[])
    {
    for(int i=0;i<25;i++)
    LASTNAME[i]=toupper(lastname[i]);
    }
    Last edited by tx1988; 05-28-2007 at 04:47 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

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