It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming C and C++ Forum

Logical c++ error

Logical c++ error

Postby Vankelmod on Tue Feb 27, 2007 2:28 pm

Hiya all. This is a program that I've been working on for quite some time and its nearly finished. However there is a hidden flaw in it which I've been unable to discover. Basicly its main goal is to read words from a file, scramble the letters around and print to screen, after which the user guesses the original word. The frequency of added words depend on time and somewhere during the process no more words are added and after that nothing whatsoever happens. This is the code, even though it's quite alot:

Code: Select all
#include<iostream>
#include<conio.h>
#include<fstream>
#include<list>
#include<vector>
#include<windows.h>
#include<ctime>
#include<math.h>


using namespace std;

struct hscr
{
       string recordholder;
       long score;
};

struct part
{
       string word;
       string scrm;
       int xpos;
       int ypos;
};

void begin(vector<string>& wordlist);
void addWord(vector<part>& data, vector<string> wordlist);
void removeWord(vector<part>& data, int n, int level, long& score, bool take, int& taken, int& missed);
void update(vector<part>& data, long score);
void checkKB(vector<part>& data, int level, long& score, string& temp, int& taken, int& missed);
void highscore(long score);
void gotoXY(int x, int y);

int flag =0;

int main()
{   
    vector<part> inGame;
    vector<string> wordlist;//Read the wordlist from file.
    srand(time(0));
    begin(wordlist);

    bool loop = true;
    long score = 0;
    int level;
    int q = 0;
    int missed=1, taken=1;
    string temp = "";
   
    long start = clock()/CLOCKS_PER_SEC;
    update(inGame, score);
    addWord(inGame, wordlist);
    long now;
    while(loop)
    {
         now = clock()/CLOCKS_PER_SEC;
         level = (score+50)/50;
         if(level > 10)
              level = 10;
         if((now-start)% 2/*((100-level)/10)*/)
         {
              if(q == 11-level)
              {
                        addWord(inGame, wordlist);
                   q = 0;
              }
              for(int n=0; n<inGame.size(); n++)
              {
                   if(inGame.at(n).ypos >= 25)
                   {
                        bool take = false;
                        removeWord(inGame, n, level, score, take, taken, missed);
                   }
              }
              checkKB(inGame, level, score, temp, taken, missed);
              //if(flag==1)
                   update(inGame, score);
              flag =0;
              if((double)taken/missed < 1)
              {
                   gotoXY(30,0);
                   cout <<"GAME OVER";
                   loop = false;
                   vector<part> miss;
                   part temp;
                   ifstream inFil("missed.txt");
                   while(getline(inFil, temp.scrm) && getline(inFil, temp.word))
                        miss.push_back(temp);
                   inFil.close();
                   getch();
                   system("cls");
                   gotoXY(25, 5);
                   for(int k=0; k<miss.size(); k++)
                   {
                        cout << miss.at(k).scrm <<'\t'<< miss.at(k).word <<endl;
                        cout << "                        ";
                   }
              }
              start = clock()/CLOCKS_PER_SEC;
              q++;   
         }
    }
    highscore(score);
    getch();
    cout << "Programmet avslutas";
    getch();
    return 0;
}

void begin(vector<string>& wordlist)
{
    ifstream readFile;
    char file[30];
    //cout << "Specify directory file: ";
    //cin.get(file, 30);
    //cin.ignore(1000, '\n');
    readFile.open("b.txt");
    if(!file)
        cout << "Access error!\n";
    else
    {
         string temp;
         while(getline(readFile, temp))
              wordlist.push_back(temp);
    }
    ofstream rensa("missed.txt");
    rensa<<"";
    rensa.close();
   
    system("cls");
    cout << "Score: 0\n";
    for(int x = 0; x<80; x++)
         cout << '_';
}

void addWord(vector<part>& data, vector<string> wordlist)
{
     part temp;
     char straw;
     int size = wordlist.size();
     temp.word = wordlist.at(rand()%wordlist.size());
     temp.scrm = temp.word;
     while(temp.word == temp.scrm)
     {
          for(int i=0; i<temp.scrm.size(); i++)
          {
               int swap = rand()%temp.scrm.size();
               straw = temp.scrm.at(i);
               temp.scrm.at(i) = temp.scrm.at(swap);
               temp.scrm.at(swap) = straw;
          }
     }
     temp.ypos = 2;
     temp.xpos = rand()%(80-temp.scrm.size());
     data.push_back(temp);
     flag =1;
}

void removeWord(vector<part>& data, int n, int level, long& score, bool take, int& taken, int& missed)
{
     score +=(long)((20-data.at(n).ypos)*data.at(n).word.size()*(level));
     if(!take)
     {
         ofstream utFil("missed.txt", ios::app);
         utFil << data.at(n).scrm <<'\t'<< data.at(n).word <<endl;
         utFil.close();
         missed++;
     }
     else
         taken++;
     gotoXY(data.at(n).xpos, data.at(n).ypos);
     for(int k=0; k<data.at(n).scrm.size(); k++)
          cout << " ";
     vector<part>::iterator it = data.begin();
     for(int i=0;  i<n; i++)
          it++;
     data.erase(it);
     flag =1;
}

void update(vector<part>& data, long score)
{
     gotoXY(7, 0);
     cout << score;
     for(int j=0; j<data.size(); j++)
     {
         gotoXY(data.at(j).xpos, data.at(j).ypos);
         for(int k=0; k<data.at(j).scrm.size(); k++)
              cout << ' ';
         data.at(j).ypos++; 
     }
     for(int m=0; m<data.size(); m++)
     {
          gotoXY(data.at(m).xpos, data.at(m).ypos);
          cout << data.at(m).scrm;
     }
}

void checkKB(vector<part>& data, int level, long& score, string& temp, int& taken, int& missed)
{
     char c;
     while(kbhit())
     {
          gotoXY(30, 0);
          int m;
          c = getch();
          temp += c;
          cout << temp;
          if(c == 13)
          {
               for(int n=0; n<data.size(); n++)
               {
                    if(temp.substr(0,data.at(n).word.size()) == data.at(n).word)
                    {
                         bool take = true;
                         removeWord(data, n, level, score, take, taken, missed);
                    }
               }
               temp = "";
               gotoXY(30, 0);
               cout << "                       ";
          }
          else if(c == '\b')
          {
               temp.erase(temp.at(temp.size()-1));
               gotoXY(29+temp.size(), 0);
               cout << " ";
          }
          flag =1;
     }
}

void highscore(long score)
{
    list<hscr> hscore;
    hscr lowscore;
    ifstream high("highscore.txt");
    list<hscr>::iterator it;
    it = hscore.begin();
    while(getline(high, lowscore.recordholder))
    {
         high >> lowscore.score;
         high.ignore(1000, '\n');
         hscore.push_front(lowscore);
         it++;
    }
    high.close();
   
    lowscore.score = score;
    gotoXY(30, 0);
    cout << "Name: ";
    //cin.ignore(1000,'\n');
    getline(cin, lowscore.recordholder);
    hscore.insert(it, lowscore);
   
    system("cls");
    it = hscore.begin()++;
    list<hscr>::iterator it2;
    while(it != hscore.end())
    {
         it2 = hscore.begin();
         while(it2 != it && (*it2).score > (*it).score)
         {
              it2++;
         }
         hscore.insert(it2, *it);
         hscore.erase(it++);
    }
    gotoXY(0, 5);
    it = hscore.begin();
    for(int y = 0; y<10; y++)
    {
         cout <<(*it).recordholder <<"\t\t" <<(*it).score <<endl;
         it++;
    }
    ofstream printOut("highscore.txt");
    it= hscore.begin();
    for(int x=0; x<10; x++)
    {
         printOut << (*it).recordholder <<endl << (*it).score <<endl;
         it++;
    }
    printOut.close();
}

void gotoXY(int x, int y)
{
     COORD c;
     c.X=x;
     c.Y=y;
     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);     
}


I hope someone can help me with this, it's really quite annoying. Any questions just ask.
Vankelmod
 
Posts: 10
Joined: Sun Nov 05, 2006 2:59 pm

Postby Vankelmod on Wed Feb 28, 2007 8:57 pm

by the way, in order to get this program to work you will have to place a file in the same directory named "b.txt" with its content on the format of a single word on each line. E.g:

hope
dodge
hedgehog
Vankelmod
 
Posts: 10
Joined: Sun Nov 05, 2006 2:59 pm

Postby Vankelmod on Sun Apr 08, 2007 8:02 pm

since nobody's made any effort to answear, this topic can now be concidered out of date. Besides, I'm currently working on rewriting this program to OOP and hopefully the issues will vanish :D (yeah right).
Vankelmod
 
Posts: 10
Joined: Sun Nov 05, 2006 2:59 pm


Who is online

Users browsing this forum: No registered users and 0 guests