read from text file help!!

This is a discussion on read from text file help!! within the C++ Programming forums, part of the General Programming Boards category; Hi Guys. I am new to this. I have to read data from a.txt file using C++. -------------------------------------------------------------------- The text ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    read from text file help!!

    Hi Guys. I am new to this. I have to read data from a.txt file using C++.
    --------------------------------------------------------------------
    The text file says:

    The cout statement comes from what library?:a:abcdabdcbadcbabdcbabdabcabaabacbadba(fi rst line)

    What was the output of this statement?:c:bdcbabdabcabaabacbadbaccccccccccbadcb a (second line)
    --------------------------------------------------------------------
    The output should be :

    Question: The cout statement comes from what library?
    Answer: a
    Responses: 36
    A: 12 (33%)
    B: 12 (33%)
    C: 6 (16.6%)
    D: 6 (16.6%)


    Question: What was the output of this statement?
    Answer: c
    Responses: 38
    A: 10 (26.3%)
    B: 10 (26.3%)
    C: 14 (38.8%)
    D: 4 (11.1%)

    ------------------------------------------------------------------------------------
    This is the code I have done so far: ( As you can see I have not done it all, but I am stuck because I am not sure what to put in the function parts, which is my my while loops are blank now. And I am not sure IF WHAT I DID is even right or not. SO any help will be greatly appreciated. Please help if you can. Thanks)
    -----------------------------------------------------------------------------------
    Code:
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include <stdio.h>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    string GetQuestion(ifstream &in);
    char GetAnswer(ifstream &in);
    string GetResponse(ifstream &in);
    
    int main() 
    {
      cout << " Sandeep Pulugurtha's Class Results " << endl;
      cout << " Mr. Lupoli CMSC 201 PROJECT #2 " << endl;
      cout << " December 1 , 2007 " << endl;
            
      string question;
      char answer;
      string response;
    
      ifstream infile;
      
      inFile.open("ExamResponses.txt", ios_base::in);
        if (!inFile) {
            cout << "Unable to open file";
            exit(1); // terminate with error
        }
    
        while(!(infile.eof()))
        {
            question = GetQuestion(infile);
            cout << question << endl;
        }
    
        while(!(infile.eof()))
        {
            answer = GetAnswer(infile);
            cout << answer << endl;
        }
    
        while(!(infile.eof()))
        {
            response = GetResponse(infile);
            cout << response << endl;
        }
        infile.close();
    }
    
     string GetQuestion(ifstream &in)
     {
             string question;
             while(!(in.eof()))
             {
                     getline(in, question, :)
             } 
    
     }
             
    char GetAnswer(ifstream &in)
     {
             char answer;
             while(!(in.eof()))
             {
                     getline(in,  answer, :)
             } 
    
     }
    
    string GetResponse(ifstream &in)
     {
             string response;
             while(!(in.eof()))
             {
                     getline(in, response, :)
             } 
    
     }
     return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Instead of all those eof() loops, I would just have one loop in main():
    Code:
    while (getline(infile, question, ':'))
    {
    	//Print question
    
    	getline(infile, answer, ':');
    	//Print answer
    
    	getline(infile, response);
    	//Call function to tally responses
    }
    Then make a function to tally the responses, and maybe another function to print them, or this could be in the same function.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    Okay, I've got it running - it is reading the file in (you can see it if you
    put a cout in the individual function), but it doesn't look like anything is
    being returned to the main() yet. It is running though...but im just stuck!!..please any help would be greatly appreciated!

    Code:
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include <stdio.h>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    string GetQuestion(ifstream &in);
    char GetAnswer(ifstream &in);
    string GetResponse(ifstream &in);
    
    int main()
    {
     cout << " Sandeep Pulugurtha's Class Results " << endl;
     cout << " Mr. Lupoli CMSC 201 PROJECT #2 " << endl;
     cout << " December 1 , 2007 " << endl;
    
    string question;
    char answer;
    string response;
    
     ifstream infile;
    
     infile.open("I:\\Exam Responses.txt", ios_base::in);
       if (!infile)
           {
           cout << "Unable to open file";
           exit(1); // terminate with error
       }
    
       while (!(infile.eof()))
       {
       GetQuestion(infile);
       cout << question;
    
       //GetAnswer(infile);
       GetResponse(infile);
       }
    
       infile.close();
    }
    
    
    string GetQuestion(ifstream &in)
     {
            string question;
            while(!(in.eof()))
            {
                getline(in, question, ':');
            }
            return question;
     }
    
    char GetAnswer(ifstream &in)
     {
            string answer;
            while(!(in.eof()))
            {
                getline(in,answer, ':');
    
            }
            return answer.at(0);
     }
    
    string GetResponse(ifstream &in)
     {
            string response;
            while(!(in.eof()))
            {
                    getline(in, response, ':');
    
            }
            return response;
     }

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > GetQuestion(infile);
    question = GetQuestion(infile);

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    what do u mean, replace what I have with what you gave?
    Where exactly should i put that ?


    thanks in advance.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,185
    Replace!
    Code:
       GetQuestion(infile);
       cout << question;
    question is empty because you haven't assigned it anything. GetQuestion returns a string that you want, but you're not assigning it to variable question.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. [HELP] Read text file in vc++ 2005
    By sonnb in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2007, 10:01 AM
  3. How to read chars from a text file and use them in TChart
    By Bachatero in forum C++ Programming
    Replies: 1
    Last Post: 08-29-2006, 04:03 PM
  4. getline function to read 1 line from a text file
    By kes103 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 06:21 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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