-
String Streams
Ok this program that i am writing is a pain. I amsupposed to create a program that will print out a students ID number along with their grades in each class. Now there is a file with all class names like this one: (classes.dat)
CSC1
CSC2
CSC46
CSC151
MTH121
PHY50
CHN1
and as an example here is what is in one of the class files; (CSC1.dat):
11234 A-
12547 B
16753 B+
21886 C
48815 C+
i've got a program but it has some problems printing out the right things. Here it is but i'm pretty sure that its the function that causes the error.
Code:
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;
void read_data(string ID,istream& in);
int main()
{
cout<<"Welcome User to the Imaginary College Transcript Search & Printout"<<endl;
cout<<endl;
bool more;
while(more)
{
string ID;
string answer;
fstream text;
cout<<"Please Enter the ID number of the Student:"<<"\n";
cin>>ID;
text.open("classes.dat");
if(text.fail())
{
cout<<"You Are at the Wrong School.\n";
exit(1);
}
read_data(ID,text);
text.close();
cout<<"Do You Wish to Print Out Another Transcript ? (y/n)"<<endl;
cin>>answer;
if(answer == "y")
{
continue;
}
else if (answer == "n")
{
more = false;
}
else
{
cout<<"You Obviously Don't Go to This College." <<endl;
cout<<"Attempt Answering Properly Next Time."<<endl;
exit(3);
}
}
return 0;
}
void read_data(string ID,istream& in)
{
fstream data;
char class_name[10];
int counter = 0;
string ident;
char mark;
while (counter<7)
{
for (int j=0;j<1;j++)
{
in>>class_name;
data.open("class_name.dat");
for (int i=0;i<2;i++)
{
if (i==0)
{
data>>ident;
}
else
{
data>>mark;
}
}
if(ident == ID)
{
cout<<"Student ID "<<ident<<endl;
cout<<class_name<<" "<<mark<<endl;
}
}
data.close();
counter++;
}
}
Thanks for your time.
-
I ran your code, and it seems you have more than a few issues, (especially if this is all your code). I forced the file name and added a cout line for debugging to see what was going on. You might want to do the same and see what it prints. Then use that as a starting point for finding your problems.
Code:
void read_data(string ID,istream& in)
{
fstream data;
char class_name[10];
int counter = 0;
string ident;
char mark;
while (counter<7)
{
for (int j=0;j<1;j++)
{
in>>class_name;
data.open("csc1.dat"); //renamed so we open something - where else doeas this come from ?
for (int i=0;i<2;i++)
{
if (i==0)
{
data>>ident;
}
else
{
data>>mark;
}
}
cout<<"Ident: "<<ident<<"-- ID: "<<ID<<"\n"; //Debug line
if(ident == ID)
{
cout<<"Student ID "<<ident<<endl;
cout<<class_name<<" "<<mark<<endl;
}
}
data.close();
counter++;
}
}
That and it looks like you're opening the same file over and over in the loop, really you probably want to open the file, use a loop to read the data, then close the file.
-
ok thanks for the input. I'm trying to do what you proposed but had no luck yet. I guess this will be a long night. :)
-
ok here is my new function.
I am still trying to debug it. Can you pros figure out the problem with the second while loop. The program freezes there and i'm not sure why. The logic behind the code seems find but the code itself is problematic. Thanks for your time.
Code:
void read_data(int ID,istream& in)
{
int number = ID;
cout<<"Student ID: "<<number<<endl;
fstream data;
string line, line2;
string class_name;
int counter = 0;
int ident;
char ch;
char mark;
while (counter < 7)
{
getline(in,line);
istringstream instr(line);
while (instr.get(ch))
{
while (ch!='\n')
{
instr>>class_name;
}
data.open("class_name.dat");
getline(data,line2);
istringstream instream(line2);
while (instream.get(ch))
{
while (ch!='\n')
{
if (isdigit(ch))
{
instream>>ident;
}
else if (isspace(ch))
{
instream>>mark;
}
if(ident != ID)
{
break;
}
else
{
cout<<class_name<<" "<<mark<<endl;
}
}
data.close();
}
}
counter ++;
}
}
-
so does anyone have any ideas?
-
is there not anyone who can help?