Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,916 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,107 people online right now. Registration is fast and FREE... Join Now!




read from a file

 
Reply to this topicStart new topic

read from a file, C++

student2007
4 Dec, 2007 - 02:02 PM
Post #1

New D.I.C Head
*

Joined: 4 Dec, 2007
Posts: 7


My Contributions


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(first line)

What was the output of this statement?:c:bdcbabdabcabaabacbadbaccccccccccbadcba (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)
-----------------------------------------------------------------------------------

#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()
{

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();
}

void GetQuestion(ifstream &in)
{
string question
while(!(in.eof()))
{
}

}

void GetAnswer(ifstream &in)
{
string answer
while(!(in.eof()))
{
}

}

void GetResponse(ifstream &in)
{
string response
while(!(in.eof()))
{
}

}
return 0;
}



User is offlineProfile CardPM
+Quote Post

student2007
RE: Read From A File
4 Dec, 2007 - 03:34 PM
Post #2

New D.I.C Head
*

Joined: 4 Dec, 2007
Posts: 7


My Contributions
ANYONE??




User is offlineProfile CardPM
+Quote Post

student2007
RE: Read From A File
4 Dec, 2007 - 08:43 PM
Post #3

New D.I.C Head
*

Joined: 4 Dec, 2007
Posts: 7


My Contributions
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;
}
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Read From A File
4 Dec, 2007 - 08:57 PM
Post #4

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
This isn't the fastest forum in the world. You have no choice but to be extremely patient :-)

Anyways, please put your code inside [ code ] [ /code ] tags, with no space inside the square braces.

Anyways, I'm about to have lunch, so I'll reply more later.
User is offlineProfile CardPM
+Quote Post

student2007
RE: Read From A File
5 Dec, 2007 - 12:01 AM
Post #5

New D.I.C Head
*

Joined: 4 Dec, 2007
Posts: 7


My Contributions
Look forward for you help...thanks in advance

QUOTE(Nayana @ 4 Dec, 2007 - 09:57 PM) *

This isn't the fastest forum in the world. You have no choice but to be extremely patient :-)

Anyways, please put your code inside [ code ] [ /code ] tags, with no space inside the square braces.

Anyways, I'm about to have lunch, so I'll reply more later.


User is offlineProfile CardPM
+Quote Post

Nayana
RE: Read From A File
5 Dec, 2007 - 01:20 AM
Post #6

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
OK, I'll give you some pointers (the standard English kind).

* The easiest way to do it is to keep the file open until the program ends.
* You should read in one line at a time, then do the question thingy, then read the next line etc.

* In order to count the a's, b's, c's and d's you will need to loop through the rest of the string

For example, if the string is "ababacac" (4 c's, 2 a's and 2 b's) then

CODE

string test = "ababacac";
int a,b,c;

for(int i = 0; i < test.length() i++) {
  if(test[i] == 'a') a++;
  else if(test[i] == 'b') b++;
  else if(test[i] == 'c') c++;
}

then a should be 4,
b 2
c 2


I'm going home now, it's 5.15pm
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 02:29PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month