Adblock breaks this site

c++ string\function problem.

Discussion in 'Programming General' started by Card.Meep, Aug 26, 2008.

  1. Card.Meep

    Card.Meep Guest

    Referrals:
    0
    c++ string\function problem.

    Ok:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int write() {
          string file;
          int q;
      cout << "What would you like to add to the file? \n" << endl;
      getline (cin,file);
      ofstream myfile;
      myfile.open ("c:/notes.txt");
       myfile << file;
      myfile.close();  
      cout << "Saved: " << file << " to c:/notes.txt!" << endl;
      cout << "Would you like to add more? (1 == yes) (2 == no)" << endl;
      cin >> q;
        if (q == 1) {
            write();
            }
            }
    int main () {
        write();                  
      cin.clear();   
      cin.ignore(255, '\\n');   
      cin.get();  
      return 0;
    And when I run it, it works the first time but when it repeats it doesn't ask for input.

    What would you like to add to the file?

    LOL NOTES
    Saved: LOL NOTES to c:/notes.txt!
    Would you like to add more? (1 == yes) (2 == no)
    1
    What would you like to add to the file?

    Saved: to c:/notes.txt!

    Would you like to add more? (1 == yes) (2 == no)

    Bolded the problem. It goes through that without a pause. Please help? :]
     
  2. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus
    c++ string\function problem.

    I'm not sure whether this is related to the problem, however, the cin.ignore() line - shouldn't it just be '\n' instead of '\\n'?
     
  3. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    c++ string\function problem.

    You are correct but that's not related to the problem. :p

    I've analyzed it pretty thoroughly and as far as I can tell it's just C++ being quirky like when sometimes you need to do two getchars() because it skips the first one...
     
  4. Govind

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus
    c++ string\function problem.

    Press Enter twice when you get to the getline() part. I've had trouble with that too. STL = buggy at times.
     
  5. Cherry65

    Cherry65 Forum Addict
    Banned

    Joined:
    Jan 31, 2007
    Posts:
    447
    Referrals:
    0
    Sythe Gold:
    0
    c++ string\function problem.

    srry it was wrong...ill check it again and fix it
     
  6. Code zombie

    Code zombie Guest

    Referrals:
    0
    c++ string\function problem.

    Ok, firstly close your main() functions brackets "}", anyway I have found your problem


    allow me to explain

    the function getline() searches the input buffer until it encounters a newline, it loads in that entire string discarding the newline character.

    The cin function does not discard the newline character. it leaves it there in the buffer.

    Now you used two different input getting functions the cin and getline function, you have the user prompted for input over here:

    cout << "What would you like to add to the file? \n" << endl;
    getline (cin,file);


    then once again down here:

    cout << "Saved: " << file << " to c:/notes.txt!" << endl;
    cout << "Would you like to add more? (1 == yes) (2 == no)" << endl;
    cin >> q;

    now the program waits for a newline character, when you press enter the character(which is converted to a number) before it is immediately loaded into q, BUT it does not clean up that newline character that you create when you press enter. now when you call the write function again it returns to this:

    cout << "What would you like to add to the file? \n" << endl;
    getline (cin,file);

    ok, getline does not wanna wait, it searches the buffer for that newline and aha it finds that newline that was not discarded by the cin function, with that it executes, passing nothing into file and proceeds once again downwards. Now I'am not someone with a masters degree in computer science so this is only 98% accurate. The solution to your problem is simple use a different function then cin, or clean the input buffer out before calling write again this is how I fooled around with your program to get it to work, please execuse the strange use of references, at the time I thought the problem was somewhere else.


    remeber the strange things I did to your ofstream varible have LITERALLY NOTHING to do with your problem, the only problem is that cin function. Try running this version it should work. hope that helped you, if not pm me for more help =)
     
< graphics? | C++ IDEs >


 
 
Adblock breaks this site