1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

file processing help me

Discussion in 'C++' started by necko, May 15, 2012.

  1. necko

    necko New Member

    Joined:
    Mar 10, 2012
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    please help me in my problem
    why this program can't compute the correct answer ?
    and also how can i random the question if I run it again :)

    and also this how can i sort the score highest to lowest using file processing


    this is the code
    please help me :))

    advance thank you for those who help me :))





    Code:
    #include "stdafx.h"
    #include "iostream"
    #include "fstream"
    #include "iomanip"
    #include "string"
    #include <conio.h>
    using namespace std;
    
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int ans;
    	string name;
    	char ans1, ans2, ans3,ans4,ans5,ans6,ans7,ans8,ans9,ans10;
    	int correct, wAns, score;
    	ifstream input;
    	ofstream output;
    
    	cout<<"**********This is a Quiz Type Game***********";
    	cout<<"\n\n\nEnter Your Name:";
    	cin>>name;
    	system("cls");
    
    	cout<<"**********This is a Quiz Type Game***********";
    	cout<<"\n\nMr/Mrs:"<<name<<endl;
    
    	
    	correct = 10;
    
    	cout << "Question #1:" << endl;   
    	cout << "Which of the following is NOT a relational operator in C++?" << endl;
    	cout << "	A. ==" << endl;
    	cout << "	B. <" << endl;
    	cout << "	C. !=" << endl;
    	cout << "	D. &&" << endl << endl; 
    	cout << "Answer:  "; 
    	cin >> ans1;
    	
    	system("cls");
    	cout << "Question #2:" << endl;   
    	cout << "Which of the following is a valid logical expression in a conditional statement?" << endl;
    	cout << "	A. age = 56" << endl;
    	cout << "	B. age !== 56" << endl;
    	cout << "	C. age <= 56" << endl;
    	cout << "	D. age => 56" <<  endl;
    	cout << "Answer:  "; 
    	cin >> ans2;
    	
    	system("cls");
    	cout << "Question #3:" << endl;   
    	cout << "Which of the following is the \"OR\" boolean operator?" << endl;
    	cout << "	A. !" << endl;
    	cout << "	B. ||" << endl;
    	cout << "	C. &&" << endl;
    	cout << "	D. ~" << endl ;
    	cout << "Answer:  "; 
    	cin >> ans3;
    
    	system("cls");
    	cout<< "Question #4: " <<endl; 	
    	cout<< "You can use C++ as a procedural, as well as an object-oriented, language"<<endl;
    	cout<< "A.True					B.	False"<<endl;
    	cout<< "Answer: ";
    	cin >> ans4;
    
    	system("cls");
    	cout<< "Question #5: " <<endl;
    	cout<< "A default catch block catches"<<endl;
    	cout<< "A.	all thrown objects"<<endl;
    	cout<< "B.	no thrown objects"<<endl;
    	cout<< "C.	any thrown object that has not been caught by an earlier catch block"<<endl;
    	cout<< "D.	all thrown objects that have been caught by an earlier catch block"<<endl;
    	cout<< "Answer:  ";
    	cin>>	ans5;
    
    	system("cls");
    	cout<< "Question #6: "<<endl;
    	cout<<"Which of the following are valid characters for a numeric literal constant?"<<endl;
    	cout<<"A.	a comma"<<endl;
    	cout<<"B.	a dollar sign ($)"<<endl;
    	cout<<"C.	a percent sign (%)"<<endl;
    	cout<<"D.	a space"<<endl;
    	cout<<"E.	None of the above"<<endl;
    	cout<<"Answer:";
    	cin>>ans6;
    
    
    	system("cls");
    	cout<<"Question #7: "<<endl;
    	cout<<"A C++ program contains a function with the header int function(double d, char c)"<<endl;
    	cout<<"Which of the following function headers could be used within the same program?"<<endl;
    	cout<<"A.	char function(double d, char c)"<<endl;
    	cout<<"B.	int function(int d, char c)"<<endl;
    	cout<<"C.	both (a) and (b)<<"<<endl;
    	cout<<"D.	neither (a) nor (b)"<<endl;
    	cout<<"Answer: ";
    	cin>>ans7;
    
    	system("cls");
    	cout<<"Question #8: "<<endl;
    	cout<<"When the compiler cannot differentiate between two overloaded constructors, they are called?"<<endl;
    	cout<<"A.	overloaded			B.	destructed"<<endl;
    	cout<<"C.	ambiguous			D.	dubious"<<endl;
    	cout<<"Answer: ";
    	cin>>ans8;
    
    	system("cls");
    	cout<<"Question #9: "<<endl;
    	cout<<"If you design a class that needs special initialization tasks, you will want to design a(n) _____"<<endl;
    	cout<<"A.	housekeeping routine"<<endl;
    	cout<<"B.	initializer"<<endl;
    	cout<<"C.	constructor"<<endl;
    	cout<<"D.	compiler"<<endl;
    	cout<<"Answer: ";
    	cin>>ans9;
    
    	system("cls");
    	cout<<"Question #10: "<<endl;
    	cout<<"The _____ mode tells C++ to open a file for input"<<endl;
    	cout<<"A.	add::ios			B.	in::file"<<endl;
    	cout<<"C.	ios::app			D.	ios::in"<<endl;
    	cout<<"		E.	ios::out"<<endl;
    	cout<<"Answer: ";
    	cin>>ans10;
    	
    	if (ans1 != 'D' || ans1 != 'd'){
    		correct = correct - 1;
    	}
    	
    	if (ans2 != 'C' || ans2 != 'c'){
    		correct = correct - 1;
    	}
    
    	if (ans3 != 'B' || ans3 != 'b'){
    		correct = correct - 1;
    	}
    
    	if (ans4 != 'A' || ans4 != 'a'){
    		correct = correct - 1;
    
    	}
    
    	if (ans5 != 'C' || ans5 != 'c'){
    		correct = correct - 1;
    	
    	}
    
    	if (ans6 != 'E' || ans6 != 'e'){
    		correct = correct - 1;
    	
    	}
    
    	if (ans7 != 'B' || ans7 != 'b'){
    		correct = correct - 1;
    	
    	}
    
    	if (ans8 != 'C' || ans8 != 'c'){
    		correct = correct - 1;
    
    	}
    
    	if (ans9 != 'C' || ans9 != 'c'){
    		correct = correct - 1;
    
    	}
    
    	if (ans10 != 'D' || ans10 != 'd'){
    		correct = correct - 1;
    	
    	}
    	
    	
    	
    	score = (correct / 10) * 100;
    	
    	
    	
    	cout << "Your Results:" << endl;
    	cout << left << setw(20) << "Question #" << left << setw(22) << "Your Answer" << left << setw(14) << "Correct Answer";
    	cout << endl << right << setw(5) << "1" << right << setw(21) << " " << left << setw(23) << ans1 << left << setw(7) << "D";
    	cout << endl << right << setw(5) << "2" << right << setw(21) << " " << left << setw(23) << ans2 << left << setw(7) << "C";
    	cout << endl << right << setw(5) << "3" << right << setw(21) << " " << left << setw(23) << ans3 << left << setw(7) << "B";
    	cout << endl << right << setw(5) << "4" << right << setw(21) << " " << left << setw(23) << ans4 << left << setw(7) << "A";
    	cout << endl << right << setw(5) << "5" << right << setw(21) << " " << left << setw(23) << ans5 << left << setw(7) << "C";
    	cout << endl << right << setw(5) << "6" << right << setw(21) << " " << left << setw(23) << ans6 << left << setw(7) << "E";
    	cout << endl << right << setw(5) << "7" << right << setw(21) << " " << left << setw(23) << ans7 << left << setw(7) << "B";
    	cout << endl << right << setw(5) << "8" << right << setw(21) << " " << left << setw(23) << ans8 << left << setw(7) << "C";
    	cout << endl << right << setw(5) << "9" << right << setw(21) << " " << left << setw(23) << ans9 << left << setw(7) << "C";
    	cout << endl << right << setw(5) << "10" << right << setw(21) << " " << left << setw(23) << ans10 << left << setw(7) << "D";
    
    	
    	cout << endl << endl << "You got " << correct << " out of 10 questions correct." << endl;
    	cout << "Your percentage score is: " << score << "%";
    	
    
    	system("pause");
    
    	return 0;
    }
     
  2. necko

    necko New Member

    Joined:
    Mar 10, 2012
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    i can compute now the correct answer but the score percentage is always appear in 0
     
  3. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    your char comparison should be && and not || or maybe use toupper() or tolower() and test that way.

    Code:
    if(tolower(ans1) != 'a')
       correct -= 1;
    
    score should be a float, I'd think. Looks like your getting integer division, so in your equation try something like
    Code:
    score = (correct / 10.0) * 100;
    
    for random questions, you'd probably need an array of strings or use a container class object like a vector.

    for the file, write your score(s); read back in the data storing it into a array or whatever, sort that array, and then rewrite the data to the file. How you go about that would depend on what data you need to keep in the file.
     
  4. necko

    necko New Member

    Joined:
    Mar 10, 2012
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Thank You :))
     

Share This Page