Thread: cpp help, not even the teacher can figure this out!

  1. #1
    Kynan_BeetleBug
    Guest

    Question cpp help, not even the teacher can figure this out!

    I can't figure this out, not even a cpp teacher could, here is what i want to do, the teacher portion of the program included below, (click the url link) is sopposed to get a class aveage from the tests that the program also makes and scores an accumlator and a counter in teacher.txt the program what calling it either makes up values or entirely skipps it, I don't understand! what is going on here?

    please help, this is due tomarrow!

    thank you so much,
    Kynan Lalone

    http://www.chartermi.net/~dlalone/math_quiz1.cpp

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ...

    egh... localize the problem. It might just be me but I don't wanna read through all that source and your explanation wasn't full enough.

    sorry

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109

    Re: ...

    Originally posted by rmullen3
    egh... localize the problem. It might just be me but I don't wanna read through all that source and your explanation wasn't full enough.

    sorry
    it isn't just you. That is quite a bit of code to read through, and your explanation didn't explain what exactly you are having problems with. actually, it sortof confused me.

  4. #4
    It now compiles, You need to clarify where the problems are.

    Code:
    //Kynan Lalone 
    //Math Quiz
    
    #include <iostream.h> 
    #include <ctype.h>
    #include <stdlib.h>
    #include <time.h>
    #include <iomanip.h>
    #include <fstream.h>
    #include <string.h>  // I added this since you are using strcmp()
    
    //prototypes
    void rand_num(char, short &, short &, char, short &);  //See WARNING below
    void neg_num_check(short &, short &, short &, char);
    void problem_and_answer(short &, short &, char, short &, short &);
    void cAdd_sub_input();
    void correct_or_not(short, short, float &, float &);
    void menu2(short, char &);
    void teacher(); 
    
    int main ()
    {
    	short cGrade = 0;
    	short sMin_num = 0; 
    	short sMax_num = 0;
    	short sTemp = 0;
    	char cAdd_sub = ' ';
    	short sUser_Answer = 0;
    	short sReal_Answer = 0;
    	short r = 0;
    	float fAnswers_right = (float) 0.0;
    	float fAnswers_wrong = (float) 0.0;
    	float fAnswers_percent = (float) 0.0;
    	float fAnswers_temp = (float) 0.0;
    	char fAnswers_grade = ' ';
    	float f10Questions_counter = (float) 0.0;
    	//input 
    	cout << "Grade            Number To Enter" << endl;
    	cout << "-----            ---------------" << endl;
    	cout << "First                  1        " << endl;
    	cout << "Second                 2        " << endl;
    	cout << "Third                  3        " << endl;
    	cout << "Fourth                 4        " << endl;
    	cout << "Fifth                  5        " << endl;
    	cout << "Sixth                  6        " << endl;
    	cout << "Teacher               999       " << endl << endl;
    
    	
    	cout  << "Enter your grade number: ";
    	cin >> cGrade;
    	if (cGrade == 999)
    	teacher();
    	else
    	menu2(cGrade, cAdd_sub);
    	
    	//FUNction time!  - here were looping the questions and answers 10 times, all the while making new random numbers, checking for negitive numbers, giving a problem and ansewer and diplaying if its right or not.
    		for (short x=0; x < 10; x = x+1)
    		{
    			rand_num(cGrade, sMin_num, sMax_num, cAdd_sub, r);   //WARNING: short to char conversion
    			neg_num_check(sMin_num, sMax_num, sTemp, cAdd_sub);
    			problem_and_answer(sMin_num, sMax_num, cAdd_sub, sUser_Answer, sReal_Answer);
    			correct_or_not(sUser_Answer, sReal_Answer, fAnswers_right, fAnswers_wrong);
    			cout << endl;
    		}
    	
    	cout << "You have compleated all 10 questions." << endl << "You got " << fAnswers_right << " questions right and " << fAnswers_wrong << " answers wrong." << endl;
    	fAnswers_percent = (fAnswers_right / 10) * 100;
    	cout << setprecision(2) << setiosflags(ios :: fixed | ios :: showpoint);
    	cout << "You have " << fAnswers_percent << "%" << " ";
    	if (fAnswers_percent <= 100 && fAnswers_percent >=90)
    		fAnswers_grade = 'A';
    	if (fAnswers_percent < 90 && fAnswers_percent >=80)
    		fAnswers_grade = 'B';
    	if (fAnswers_percent < 80 && fAnswers_percent >=70)
    		fAnswers_grade = 'C';
    	if (fAnswers_percent < 70 && fAnswers_percent >=60)
    		fAnswers_grade = 'D';
    	if (fAnswers_percent <= 60 && fAnswers_percent >=0)
    		fAnswers_grade = 'F';
    	cout << "That is a " << fAnswers_grade << ".";
    	
    	//counter used for the teacher.txt (counts how many 10 question tests were taken
    	f10Questions_counter++;
    	//sending the above answers correct (fAnswers_right) to a file which is used in the teacher menu in teacher()
    	ofstream outFile;
    	outFile.open("teacher.txt", ios::app);
    	outFile << fAnswers_right<<endl;
    	outFile.close();
    	
    	return 0;
    }
    void menu2(short cGrade, char & cAdd_sub)
    { 
    	if (cGrade == 1)
    		{	
    			cAdd_sub = 'A';
    		}
    	if (cGrade == 2)
    		{
    			cAdd_sub_input();
    			cout << "Enter the problem type: ";	
    			cin >> cAdd_sub;
    		}
    	if (cGrade == 3)
    		{
    			cAdd_sub_input();
    			cout << "Multiplication          M        " << endl;
    			cout << "Enter the problem type: ";	
    			cin >> cAdd_sub;
    		}
    	if (cGrade == 4)
    		{
    			cAdd_sub_input();
    			cout << "Multiplication          M        " << endl;
    			cout << "Enter the problem type: ";	
    			cin >> cAdd_sub;
    		}
    	if (cGrade == 5)
    		{
    			cAdd_sub_input();
    			cout << "Multiplication          M        " << endl;
    			cout << "Divison                 D        " << endl;
    			cout << "Enter the problem type: ";	
    			cin >> cAdd_sub;
    		}
    	if (cGrade == 6)
      		{
      			cAdd_sub_input();
      			cout << "Multiplication          M        " << endl;
    			cout << "Divison                 D        " << endl;
    			cout << "Enter the problem type: ";	
    			cin >> cAdd_sub;
      		}
     }
    void correct_or_not(short sUser_Answer, short sReal_Answer, float & fAnswers_right, float & fAnswers_wrong)
    {
    	if (sUser_Answer == sReal_Answer)
    		{
    			cout << endl << "Your answer was " << sUser_Answer << " and the correct answer was " << sReal_Answer << "." << endl;
    			cout << "Congratulations, You Are Correct!" << endl;
    			fAnswers_right = fAnswers_right + 1;
    		}
    		else
    		{
    			cout << endl << "Your answer was " << sUser_Answer << " and the correct answer was " << sReal_Answer << "." << endl;
    			cout << "Sorry, Better Luck Next Time!" << endl;
    			fAnswers_wrong = fAnswers_wrong + 1;
    		}
    
    }
    void cAdd_sub_input()
    {
    	cout << "Problem           Letter To Enter"<< endl;
    	cout << "-----            ---------------" << endl;
    	cout << "Addition            	A        " << endl;
    	cout << "Subtraction           	S        " << endl;
    }
    void rand_num(char cGrade, short & sMin_num, short & sMax_num, char cAdd_sub, short & r)
    {	
    	//seed random numbers
    	srand(time(NULL));
    	if (toupper(cAdd_sub) == 'S' || toupper(cAdd_sub) == 'A')
    	{
    
    	
    		if (cGrade == 1)
    	
    		{	
    			//define variables as random numbers between 1 and 10
    			sMin_num = 1 + rand() % (10 - 1 + 1);
    			sMax_num = 1 + rand() % (10 - 1 + 1);
    		}
    		
    		else
    		{
    			//define variables as random numbers between 10 and 100
    			sMin_num = 1 + rand() % (100 - 1 + 1);
    			sMax_num = 1 + rand() % (100 - 1 + 1);
    		}
    	}
    	if (toupper(cAdd_sub) == 'M' || toupper(cAdd_sub) == 'D')
    	{	
    		//define variables as random numbers between 0 AND 12
    		sMin_num = 1 + rand() % (12 - 1 + 1);
    		sMax_num = 1 + rand() % (12 - 11 + 1);
    		
    		if (toupper(cAdd_sub) == 'D')
    		{
    			// get rid of the remainder 
    			r = sMin_num % sMax_num;
    			sMax_num = sMax_num - r;
    		}	
    	}
    }
    void neg_num_check(short & sMin_num, short & sMax_num, short & sTemp, char cAdd_sub)
    {	
    	//make sure there are no negitive answers
    	if (sMin_num < sMax_num && toupper(cAdd_sub) == 'S')
    	{
    		sTemp = sMin_num;
    		sMin_num = sMax_num;
    		sMax_num = sTemp;
    	}
    }
    void problem_and_answer(short & sMin_num, short & sMax_num, char cAdd_sub, short & sUser_Answer, short & sReal_Answer)
    {
    
    		if (toupper(cAdd_sub) == 'A')
    			{
    				//display problem
    				cout << sMin_num << " + " << sMax_num << " = ";
    				cin >> sUser_Answer;
    			
    				//do problem
    				sReal_Answer = sMin_num + sMax_num;
    			}
    		if (toupper(cAdd_sub) == 'S')
    			{
    				//display problem
    				cout << sMin_num << " - " << sMax_num << " = ";
    				cin >> sUser_Answer;
    				
    				//do problem
    				sReal_Answer = sMin_num - sMax_num;
    			}
    		if (toupper(cAdd_sub) == 'M')
    			{
    				//display problem
    				cout << sMin_num << " * " << sMax_num << " = ";
    				cin >> sUser_Answer;
    				//do problem
    				sReal_Answer = sMin_num * sMax_num;
    			}
    		if (toupper(cAdd_sub) == 'D')
    			{	
    				//display problem
    				cout << sMin_num << " / " << sMax_num << " = ";
    				cin >> sUser_Answer;
    				//do problem
    				sReal_Answer = sMin_num / sMax_num;	
    			}
    }
    void teacher()
    {
    	char cTpassword[14] = {' '};
    	short sTeacher_menu = 0;
    	short sTanswers_right = 0;
    	short sTanswers_accumlator = 0;
    	short sTanswers_counter = 0;
    	cout << "Enter your pre-determined password: ";
    	cin >> cTpassword;
    	
    	if (strcmp(cTpassword, "gradecheck999") == 0)
    	{
    		cout << endl << "Operation                Number to Enter" << endl;
    		cout << "---------                ---------------" << endl;
    		cout << "Class Average                    1      " << endl;
    		cout << "Erase Class Average File         2      " << endl << endl;
    		cout << "Enter your choice: ";
    		cin >> sTeacher_menu;
    		if (sTeacher_menu == 1)
    			{
    				ifstream sendFile;
    				sendFile.open("teacher.txt");
    	
    				if (!sendFile.fail())
    				{	
    						sendFile>>sTanswers_right;
    						
    				
    					while(!sendFile.eof())
    					{	
    						sTanswers_counter++;
    						sTanswers_accumlator = sTanswers_right + sTanswers_accumlator;
    						sendFile>>sTanswers_right;
    					}
    				}
    				sTanswers_accumlator = sTanswers_right + sTanswers_accumlator;
    				cout << endl << "Class Average " << sTanswers_accumlator / sTanswers_counter << endl;
    				sendFile.close();
    			}
    		if (sTeacher_menu == 2)
    			//erase the file
    		//somehow pause the program (press any key) 
    		//go to main	
    		main();
    	}
    	else
    	{
    		cout << "Wrong Password (remember, the password is case-sensitive)" << endl << endl;
    		main();
    	}
    }
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  5. #5
    It was just a txt file of his code, I posted all of his code anyways with modifications.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. includein cpp file in a cpp file
    By sawer in forum C++ Programming
    Replies: 7
    Last Post: 06-15-2006, 12:34 PM
  2. Replies: 2
    Last Post: 04-09-2006, 07:20 PM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. i do i get started on cpp file
    By jlmac2001 in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2003, 08:55 PM