c++ madlib

Discussion in 'Computer Science & Culture' started by SwedishFish, Mar 6, 2003.

Thread Status:
Not open for further replies.
  1. SwedishFish Conspirator Registered Senior Member

    Messages:
    1,908
    For my comp sci homework, I need to create a program that takes a madlib from a file and lets the user input words to replace [noun], [adjective], etc. My first attempt made sense on paper but didn't work. Here is my latest attempt:

    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;

    int main()
    {
    string word,story,open="[",close="]";
    char type[14];
    char cha;
    int count=0;
    fstream fin("madlib.txt");
    while(fin>>cha) //appears to be reading the entire file into cha"
    {
    if(cha==open[0])
    {
    count=0;
    while(cha!=close[0])
    {
    type[count]=cha;
    count++;
    }
    cout<<"Input a(n) "<<type<<": ";
    cin>>word;
    story+=word;
    }
    story+=cha;
    }
    cout<<story<<endl;

    return 0;
    }

    I'd greatly appreciate a push in the right direction or any help that could be offered.
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. SwedishFish Conspirator Registered Senior Member

    Messages:
    1,908
    oops, that last post got cut off. now how did that happen? i'll have to find the program and repost it. anyway, can someone give me a push in the right direction?
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. Reid Registered Senior Member

    Messages:
    97
    To start

    replace
    while(fin>>cha)

    with
    while( fin.get(cha) )
    {
    ...
    }
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
Thread Status:
Not open for further replies.

Share This Page