• Log in or sign up for an account here: https://www.sythe.org/login

c++ string\function problem.

  • Thread starter Thread starter Card.Meep
  • Start date Start date
C

Card.Meep

Guest
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? :]
 
Le Monkey
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
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'?
 
SMR said:
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'?
You are correct but that's not related to the problem. :P

Bolded the problem. It goes through that without a pause. Please help? :]
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...
 
Le Monkey
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
Press Enter twice when you get to the getline() part. I've had trouble with that too. STL = buggy at times.
 
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.


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

int write(ofstream & HWND) {
string file;
int q;
cout << "What would you like to add to the file? \n" << endl;
getline (cin,file);
HWND << file;
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) {
getline (cin, file);
write(HWND);
}
HWND.close();
}
int main () {
ofstream myfile;
myfile.open ("c:/notes.txt");
write(myfile);
cin.clear();
cin.ignore(255, '\n');
cin.get();
return 0;
}

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 =)
 
Runescape Gamblingrunescape gambling
Back
Top