Flashcard program outputting random data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mohaakilla51
    New Member
    • Jul 2007
    • 39

    Flashcard program outputting random data

    Alright guys, I am working on a flashcard script... Previously I had it so that it onlty had predefined categories. People were complaining, so now I am trying to make it to where it reads flashcards.txt and then gets the categories from there.

    Anyhow, I understand how to do this, and there is nothing wrong with my syntax, because it compiles properly, but whenever it runs, it just starts outputting a bunch of random text, although I did recognize some OS info (at least I think I did.) I am using Win XP SP2.

    flashcard.txt:
    Code:
    topic random|topic.txt
    topic bob|bob.txt
    important parts of main.cpp:
    [code=cpp]
    int menu(vector<str ing> menu1)
    {
    int topic;
    string topics;
    system("CLS");
    cout << "============== =============== ====" << endl;
    cout << "= Liberty Flash Cards =" << endl;
    cout << "=-------------------------------=" << endl;
    cout << "= =" << endl;
    system("pause") ;
    //Somewhere in the following for loop is where the error is
    for(int i=0; i < menu1.size(); i++)
    {
    topics = append(menu1[i], " ", 25);
    cout << "= " << i+1 << ".) " << topics;
    if(i>8)
    {
    cout << "=" << endl;
    }else{
    cout << " =" << endl;
    }
    }

    cout << "= =" << endl;
    cout << "============== =============== ====" << endl;
    cout << "What topic would you like to study: ";
    cin >> topic;
    if(!cin)
    {
    fixcin();
    return 0;
    }
    return topic;
    }

    ///////////////////////////////////////////////////////////////
    //main has been edited to only include the important parts. //
    ///////////////////////////////////////////////////////////////
    int main()
    {
    ifstream themen("flashca rd.txt");
    vector<string> menu1;
    vector<string> file;
    if(themen.is_op en()==0)
    {
    cout << "You must create a \"flashcard.txt \" file \nand place it in the same folder as this program\n";
    system("pause") ;
    return 1;
    }
    while(!themen.e of())
    {
    getline(themen, temp1, '|');
    getline(themen, temp2);
    // This was done to exclude the | character...
    menu1.push_back (temp1.substr(0 ,temp1.length()-2));
    file.push_back( temp2);
    }
    topic = menu(menu1);
    while(topic <1 || topic >= menu1.size())
    {
    topic = menu(menu1);
    }
    }
    [/code]
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Originally posted by mohaakilla51
    int menu(vector<str ing> menu1)
    {
    int topic;
    You do realize, I hope, that the menu1 vector is a copy of the one on main()??

    So any changes made in this function will be tothe copy that is destroyed when the function completes.

    You should be passing your vector by reference.

    Comment

    • mohaakilla51
      New Member
      • Jul 2007
      • 39

      #3
      Is there really any need to do that? The Changes that I made to it in menu() were few, and really only so that It would look better on the output. That isn't what is causing my problem is it?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Well, change the argument to menu() to be a reference and see if it makes a difference. I always want to be sure I am dealing with exactly one instance of my vectors. Especially when they are large and making copies takes a lot of time.

        Comment

        Working...