Thread: String Streams

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    60

    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.
    C++ can hurt.

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    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.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    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.
    C++ can hurt.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    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 ++;
    	}
    }
    C++ can hurt.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    so does anyone have any ideas?
    C++ can hurt.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    is there not anyone who can help?
    C++ can hurt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. += operator
    By BKurosawa in forum C++ Programming
    Replies: 8
    Last Post: 08-05-2007, 03:58 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM