Thread: Need a urgent help, Test in hours time!!

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    4

    Need a urgent help, Test in hours time!!

    Someone please help me I'm having my exam tmrw! So nervous!!


    Im having trouble combining both the .txt files in to 1 file and run the program which I have coded.

    The output is what I wanted but it goes:
    James
    90
    Thomas
    50
    Sheila
    60
    Jessica
    20
    Precillia
    10
    2 out of 5 of the class has failed the test.

    I want the output of this program to be like:

    James 90
    Thomas 50
    Sheila 60
    Jessica 20
    Precillia 10
    2 out of 5 of the class has failed the test.

    ---

    I've attached the necessary files. here is my program;

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
    string getcontent;
    ifstream openfile ("names.txt") ;
    if (openfile.is_open())
    {
    while(! openfile.eof())
    {
    openfile >> getcontent;
    cout << getcontent << endl;
    }
    }
    int marks[100];
    int x=0,fail=0, pass=0;
    
    
    fstream textfile;
    textfile.open("marks.txt");
    
    while(! textfile.eof()){
    textfile >> marks [x];
    
    if(marks[x]<50){
    fail++;
    }
    else{
    pass++;
    }
    x++;
    }
    
    textfile.close();
    openfile.close();
    cout << fail << " students out of "<< (fail+pass) << " did not pass.";
    
    return 0;
    }

    Attachment 11410Attachment 11411Attachment 11412

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You need to better format your code when posting. Indentation goes a long way to making your code readable.




    Code:
    ...
    while(! openfile.eof())
    ...
    while(! textfile.eof()){
    Do not use an EOF test to control your loops. Instead, you should be testing for success of the read operation itself.




    Code:
    while(! openfile.eof())
    {
    openfile >> getcontent;
    cout << getcontent << endl;
    }
    The stream extraction operator >> works by using whitespace to distinguish individual elements. You read a name from the file and output it, then you read a mark and output it. At each point where you output something you are also writing a newline which produces the output you are seeing.

    A better way would be to extract a name and a mark together and then output a name and a mark. This would allow the output you desire.




    There doesn't seem to be a purpose to the mark file. The marks already exist in the names file so why not use those.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    4
    Code:
    while(! openfile.eof())
    {
    openfile >> getcontent;
    cout << getcontent << endl;
    }
    The stream extraction operator >> works by using whitespace to distinguish individual elements. You read a name from the file and output it, then you read a mark and output it. At each point where you output something you are also writing a newline which produces the output you are seeing.

    A better way would be to extract a name and a mark together and then output a name and a mark. This would allow the output you desire.




    There doesn't seem to be a purpose to the mark file. The marks already exist in the names file so why not use those.[/QUOTE]

    I dont know how to do it with one file thats why Im posting here. Could you help me with it?
    here is the indented code

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
        string getcontent;             
        ifstream file ("names.txt") ; 
        if (file.is_open())   
    {
            while(! file.eof())  
                file >> getcontent;        
                cout << getcontent << endl;  
    }
    }
    
        int marks[100];            
        int x=0,fail=0, pass=0;   
    
        fstream textfile;          
        textfile.open("marks.txt");   
    
            while(! textfile.eof()){   
                textfile >> marks [x];     
    
                    if(marks[x]<50){          
                        fail++;
                        }
                        else{
                            pass++;
                            }
                    x++;
                }
    
        textfile.close();
        file.close();      
        cout << fail << " students out of "<< (fail+pass) << " did not pass."; 
    
    return 0;
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Consider:
    Code:
    std::string name;
    int age;
    ...
    std::cout << "Enter your name and age separated by a space: ";
    std::cin >> name >> age;
    You can read a name/mark pair from a file in a similar fashion. Read a name/mark and then print name/mark. Do that in a loop. Your names file has both name and marks in it apparently.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    4
    Sorry, Im half way there. I just want to know how could i filter out just the numbers from my text file?

    Like
    James 90
    Thomas 50
    Sheila 60
    Jessica 20
    Precillia 10

    I just want to the marks so that I can calculate the average. This text in a .txt file. How do I do it using Fstream

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You don't have to do anything with the name. You can read the name/mark and ignore the name part and just use the numbers as you wish for whatever calculation.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Don't the forum guidelines specifically say not to put urgent in your post titles? It doesn't get you any more attention and from most people it won't give you the kind of attention you want.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by VirtualAce View Post
    Don't the forum guidelines specifically say not to put urgent in your post titles? It doesn't get you any more attention and from most people it won't give you the kind of attention you want.
    If I see "urgent", or variants thereof, in a subject line I usually don't bother to read the thread unless it has half a dozen or more replies. If there is a particular time limit mentioned, I will not post until that time limit has passed.

    If that is the type of attention desired, people are welcome to keep creating threads like that.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a urgent help, Test in hours time!!
    By wowolinda in forum C Programming
    Replies: 2
    Last Post: 02-02-2012, 12:25 PM
  2. Time between Military Hours
    By StarOrbs in forum C++ Programming
    Replies: 18
    Last Post: 03-01-2005, 06:46 PM
  3. Time to test your C++ abilities...
    By Darkman in forum C++ Programming
    Replies: 19
    Last Post: 01-10-2004, 09:58 AM
  4. Replies: 3
    Last Post: 06-13-2003, 06:47 AM
  5. URGENT!!!!9 hours left!!!
    By emiyca in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2001, 08:34 AM