3 Replies - 134 Views - Last Post: 23 November 2011 - 12:25 PM Rate Topic: -----

#1 hulla  Icon User is offline

  • sqrt(-1) am awesome


Reputation: 49
  • View blog
  • Posts: 727
  • Joined: 05-March 11

Semantics error? Program skips to the end.

Posted 23 November 2011 - 11:02 AM

I'm doing something wrong but I don't know what. The code seems to do nothing but end. It does print the final message though. I really feel like I'm doing something stupidly stupid lol.
#include <iostream>
#include <fstream>

using namespace std;

bool nextChar(char &param);

int main(void)
{
    ifstream inFile;
    bool hasRunBefore;
    inFile.open("C:\\Program Files\\ID tracker\\has run.txt");
    if(inFile)
    {
        cout << "Running this program can cause data-loss. If you have stored \
                 any data already, please save it before continuing.\nPress \
                 enter to continue . . ." << endl;
        cin.sync();
        cin.get();
    }
    else
    {
        ofstream outFile;
        outFile.open("C:\\Program Files\\ID tracker\\has run.txt");
        outFile << "Do not delete this file. You may modify it though.";
        outFile.close();
    }
    char char1, char2;
    char1 = char2 = 96;
    ofstream outFile;
    string pathName;
    while(nextChar(char1))
    {
        while(nextChar(char2))
        {
            pathName = (string) "C:\\Program Files\\ID tracker\\Database\\" + char1 + "\\" + char2 + ".txt";
            outFile.open(pathName.c_str());
            cout << "Created " << pathName << endl;
            outFile.close();
        }
    }
    cout << "The database has been constructed.";
    cin.sync();
    cin.get();
    return 0;
}

bool nextChar(char &param)
{
    // If the char is 'Z' then break outta main's loop.
    // Else increment it and return true to remain in the loop.
    if(param == 122)
        param = 48;
    else if(param == 57)
        param = 96;
        return false;
    ++param;
    return true;
}



Thank you for reading this topic.

Is This A Good Question/Topic? 0
  • +

Replies To: Semantics error? Program skips to the end.

#2 hulla  Icon User is offline

  • sqrt(-1) am awesome


Reputation: 49
  • View blog
  • Posts: 727
  • Joined: 05-March 11

Re: Semantics error? Program skips to the end.

Posted 23 November 2011 - 11:10 AM

Oh the comment in the nextChar(char&) function is old. Ignore it lol. The function turns 'Z' chars into '0' chars and returns false when it becomes a '9' char.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,753
  • Joined: 25-December 09

Re: Semantics error? Program skips to the end.

Posted 23 November 2011 - 11:18 AM

Well since your nextChar() function always returns false, your two while loops will never execute.

Jim
Was This Post Helpful? 1
  • +
  • -

#4 hulla  Icon User is offline

  • sqrt(-1) am awesome


Reputation: 49
  • View blog
  • Posts: 727
  • Joined: 05-March 11

Re: Semantics error? Program skips to the end.

Posted 23 November 2011 - 12:25 PM

Oh I get it I missed out the braces. :( Thank you! :D
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1