Thread: Help with file save

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    4

    Help with file save

    I have everything working correctly besides when a person enters both first AND last name, it throws the program into a loop, and I cant get the save to work correctly, it builds just fine, but when it is ran the program shuts down.


    The program is first supposed to ask if you want to load info from a file or start fresh. Then the user can choose to enter names and grades to a list, view the list, or exit. I can not get the program to save the list.
    Code:
    #include <fstream>
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <cctype>
    #include <cmath>
    #include <istream>
    using namespace std;
    
    double exam[100][4];
    string students[100];
    
    int numberofstudents=0;
    char assign(double average);
    void selection();
    
    void print()
    {
    
    	cout<<setw(10)<<left<<"Student"<<setw(10)<<left<<"Exam 1"<<setw(10)<<left<<"Exam 2"<<setw(10)<<left<<"Exam 3"<<setw(10)<<left<<"Final"<<setw(10)<<left<<"Average"<<setw(10)<<left<<"Letter"<<endl;
    	for (int i=0; i<numberofstudents;i++)
    	{
    		double average=((exam[i][0]+exam[i][1]+exam[i][2]+exam[i][3])/4);
    		cout<<setw(10)<<left<<students[i]<<setw(10)<<left<<exam[i][0]<<setw(10)<<left<<exam[i][1]<<setw(10)<<left<<exam[i][2]<<setw(10)<<left<<exam[i][3]<<setw(10)<<left<<average<<setw(10)<<left<<assign(average)<<endl;
    	}
    
    }
    void add()
    {	
    	
    	cout<< " Please enter student name "<<endl;
    	cin>>students[numberofstudents]; //When a person's first AND last name are entered, it throws the program into a continual loop, what is used so it gets first and last names without the loop.  
    	cout<< " Please enter the exam grades in this order: Exam 1, Exam 2, Exam 3, Final "<<endl;
    	cin>> exam[numberofstudents][0]>>exam[numberofstudents][1]>>exam[numberofstudents][2]>>exam[numberofstudents][3];
    	numberofstudents++;
    	ofstream save;
    	save.open("grades.txt");
    	save<<students[0]<<exam[0][0]<<exam[0][1]<<exam[0][2]<<exam[0][3];
    	
    	while (save)
    	{	
    		numberofstudents++;
    		save<<students[numberofstudents]<<exam[numberofstudents][0]<<exam[numberofstudents][1]<<exam[numberofstudents][2]<<exam[numberofstudents][3];
    		
    	}
    	save.close();
    
    	selection();
    
    }
    char assign(double average)
    {
    	char grade;
    		if (100>=average&&average>89)
    					grade= 'A';
    		else if (90>average&&average>79)
    					grade='B';
    		else if (80>average&&average>69)
    					grade='C';
    		else if (70>average&&average>59)
    					grade='D';
    		else		grade='F';
    		
    	
    	return grade;
    
    }
    
    void selection()
    {
    	bool flag=true;
    	while (flag)
    	{
    	int t;
    
    
    	cout<<"Please enter activity selection: "<<endl;
    		cout<<"0 - Print Student Grades "<<endl;
    		cout<<"1 - Add Student "<<endl;
    		cout<<"9 - Exit "<<endl;
    		cin>>t;
    	if (t==0)
    	{
    		print() ;
    	}
    	else if (t==1)
    	{
    		add() ;
    	}
    	else if (t==9)
    	{
    		flag=false;
    	}
    	else 
    	{
    		cout<< "INVALID ENTRY"<<endl;
    		flag=true;
    	}
    	}
    
    
    }
    
    int main()
    {	
    	char y;
    	cout<<"Would you like to enter data from an existing file(Y/N)?"<<endl;
    	cin>>y;
    	if ('Y'==y||y=='y')
    	{
    		
    	ifstream grades;
    	grades.open("grades.txt");
    	
    	grades>>students[0]>>exam[0][0]>>exam[0][1]>>exam[0][2]>>exam[0][3];
    	
    	while (grades)
    	{	numberofstudents++;
    		grades>>students[numberofstudents]>>exam[numberofstudents][0]>>exam[numberofstudents][1]>>exam[numberofstudents][2]>>exam[numberofstudents][3];
    		
    	}
    		grades.close();
    	selection();
    	}
    	else if(y=='N'||y=='n')
    	{
    	selection();
    	}
    	else 
    	{
    		cout<<" INVALID SELECTION";
    	}
    
    return 0;
    }
    Last edited by peter94; 11-11-2008 at 09:27 PM.

  2. #2
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    I don't know if this is the right solution, but try this instead of cin:
    Code:
    getline( cin, students);
    I had problems with just standard cin as well, but my problem was that the input was only one word at a time. This may not fix your program.
    Hatman Approves!

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Every time you open grades.txt as an ofstream, you destroy the data previously in the file. If that's not what you want, you should open it in append mode (see your textbook for details).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Save a file with a name in a variable?
    By guriwashere in forum C Programming
    Replies: 2
    Last Post: 06-01-2009, 04:03 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Ask user to save file before exiting.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 11-15-2004, 06:34 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM