Thread: program complies but quits..

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    7

    program complies but quits..

    My program complies but then quits out will being executed.
    I was wondering if someone would be willing to compile and see if it does the same thing for you.



    Heres my source code (the attached file is the text file that the program needs) -
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <time.h>
    #include <memory.h>
    #include <conio.h>
    
    #define RAND_SIZE   50
    #define NUMBERS		20
    
    struct info
    {
    char question[50][80], cor_ans[50][60], wro1[50][60], wro2[50][60], wro3[50][60];
    int ran[NUMBERS], dcounter, nright, nwrong;
    };
    
    void readfile (info data);
    void out (info data);
    void right (info data);
    void wrong (info data);
    void game_over (info data);
    
    int main(void)
    {
    	int flags[RAND_SIZE];
    	int numbers[NUMBERS];
    	int i, choice;
        info data;
    
    	memset(flags, 0, sizeof(flags));
    	memset(numbers, 0, sizeof(numbers));
    
    	srand(time(NULL));
    
    	for(i = 0; i < NUMBERS; i++)
    	{
    		int num = rand() % RAND_SIZE;
    
    		if(flags[num])
    		{
    			i--;
    			continue;
    		}
    
    		flags[num] = 1;
    		data.ran[i] = num;
    	}
    
    cout <<"\t\t***********************************************\n";
    cout <<"\t\t*                                             *\n";
    cout <<"\t\t*                                             *\n";
    cout <<"\t\t*        Ryan's Dune Trivia Extravganza       *\n";
    cout <<"\t\t*                                             *\n";
    cout <<"\t\t*                                             *\n";
    cout <<"\t\t***********************************************\n\n\n\n";
    cout << "Would you like to -\n";
    cout << "   1. Play game\n";
    cout << "   2. Quit\n";
    cin >> choice;
    switch (choice)
      {
    case 1: readfile(data);
    	   break;	 
    case 2:
           return 0;
    default:
            cout<<"Error, bad input, quitting\n";
            system("PAUSE");
    }
      return 0;
    }
    
    void readfile (info data)
    {
    int counter;
    ifstream infile;
    
    data.dcounter = 0;
    infile.open("Questions.txt", ios::in);  // Open file for input of data
    
    for (counter = 0; counter < 50; counter++)
    {
    infile.get (data.question[counter], 80);
    infile.ignore(80, '\n');
    infile.get (data.cor_ans[counter], 60);
    infile.ignore(80, '\n');
    infile.get (data.wro1[counter], 60);
    infile.ignore(80, '\n');
    infile.get (data.wro2[counter], 60);
    infile.ignore(80, '\n');
    infile.get (data.wro3[counter], 60);
    infile.ignore(80, '\n');
    }
    infile.close();
    
    out (data);
    }
    
    void out (info data)
    {
    int choice;
    system("cls");
    cout << data.question[data.ran[data.dcounter]]; // The question is asked
    if (data.ran[data.dcounter] < 25)               // Order if number is less than 25
    {
    cout << "\n1: " << data.cor_ans[data.ran[data.dcounter]];
    cout << "\n2: " <<data.wro1[data.ran[data.dcounter]];
    cout << "\n3: " <<data.wro2[data.ran[data.dcounter]];
    cout << "\n4: " <<data.wro3[data.ran[data.dcounter]];
    cin >> choice;
    switch (choice)
      {
    case 1: right(data);
    	   break;	 
    
    default:
           wrong(data);
       }
    }
    
    if ((data.ran[data.dcounter] >= 25) && (data.ran[data.dcounter] < 50))   // Order if number is greater or equal to 25
    {                                              // but less than 50
    cout << "\n1: " <<data.wro3[data.ran[data.dcounter]];
    cout << "\n2: " <<data.wro1[data.ran[data.dcounter]];
    cout << "\n3: " <<data.wro2[data.ran[data.dcounter]];
    cout << "\n4: " <<data.cor_ans[data.ran[data.dcounter]];
    cin >> choice;
    switch (choice)
      {
    case 4: right(data);
    	   break;	 
    
    default:
           wrong(data);
       }
    }
    if ((data.ran[data.dcounter] >= 50) && (data.ran[data.dcounter] <75))   // Order if number is greater or equal to 50
    {                                             // but less than 75
    cout << "\n1: " <<data.wro3[data.ran[data.dcounter]];
    cout << "\n2: " <<data.wro1[data.ran[data.dcounter]];
    cout << "\n3: " <<data.cor_ans[data.ran[data.dcounter]];
    cout << "\n4: " <<data.wro2[data.ran[data.dcounter]];
    cin >>choice;
    switch (choice)
      {
    case 3: right(data);
    	   break;	 
    
    default:
           wrong(data);
       }
    }
    if ((data.ran[data.dcounter] >= 75) && (data.ran[data.dcounter] <100))  // Order if number is greater or equal to 25
    {
    cout << "\n1: " <<data.wro1[data.ran[data.dcounter]];
    cout << "\n2: " <<data.cor_ans[data.ran[data.dcounter]];
    cout << "\n3: " <<data.wro3[data.ran[data.dcounter]];
    cout << "\n4: " <<data.wro2[data.ran[data.dcounter]];
    cin >> choice;
    switch (choice)
      {
    case 2: right(data);
    	   break;	 
    
    default:
           wrong(data);
       }
    }
    }
    void right (info data)
    {
    
     cout <<"\n\nNice One. You were correct, my friend!";
     data.nright++;                                // Increases the number correct
     cout <<"\nYour current score is "<<data.nright << " out of a possible " << data.dcounter+1;
     system("PAUSE");
     data.dcounter++;
     if (data.dcounter == 20) {game_over(data);}  //Determines whether or not the game is over
     if (data.dcounter < 20 ) {out (data);}
    }
    
    void wrong (info data)
    {
    
    
     cout << "Ouch!! What a waste of water. Your are incorrect";
     //cout << "\nThe correct asnwer was: " << data.cor_ans[data.ran[data.dcounter];
     cout <<"\nYour current score is "<<data.nright << " out of a possible " << data.dcounter+1;
     system("PAUSE");
     data.nwrong++;
     data.dcounter++;
     if (data.dcounter == 20) {game_over(data);}  //Determines whether or not the game is over
     if (data.dcounter < 20 ) {out (data);}
    
    }
    
    void game_over(info data)
    {
    double percentR, percentW;
    char choice;
    
    percentR = (data.nright / 20) * 100;   // Calculates the percentage right and wrong
    percentW = (data.nwrong / 20) * 100;
    cout <<"The Game is over!!!!\n\n";
    cout <<"Your final score is " << data.nright <<" out of 20 or " <<percentR <<"%\n\n" ;
    cout <<"You aswered "<< data.nwrong <<" questions incorrectly\n\n";
    system("PAUSE");
    }

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    it compiles and runs fine on mine

    i'm using MSVC++ 6.0 on WinNT

    it works.. but it's full of bugs

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    7
    Thanks for trying. Must be something with Dev C++ 4. If its working on MSVC++ 6.0, I should be able to run and fix the bugs at school.

    Regards

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM