I am having a problem with a program I was asked to make for a class and unfortunately I was only able to turn in a portion of the program. I would still like to learn what I was missing though so I do not fall further behind so any help would be appreciated.
I have read the sections in my textbook that are supposed to pertain to the functions I need but none show how to tie them together. It covers outputting to files but only from cout and not from user inputs. I need to ask for user input for various questions then write them to a text file but the inputs need to be inserted in certain spots in the text.
I understand the inputs and how to store them (I think) but writing them to a file is where I am lost. Any help would be appreciated.
Here is a much simpler version of the code I need to fix.
int main()
{
ofstream outputfile;
outputfile.open("test.txt");
string num;
cout << "type a number: ";
getline(cin,num);
outputfile << "I have <num> apples.";
outputfile.close();
return 0;
}
...............................................................................
Where <num> would need to be inserted to the file. This will happen multiple times and even with some integers and calculations but I am fairly certain that I can handle that.
Never mind, problem solved although I am unsure if this is the easy way to do it. I ended up with,
outputfile << "I have " << num << " apples.";
but this way the file has to be written all at once after all user inputs have been stored as variables then recalled and copied to the text file along with the fout statement. Too bad I did not catch this soon enough to get a full grade but at least now I know for in the future.
but this way the file has to be written all at once after all user inputs have been stored as variables then recalled and copied to the text file along with the fout statement.
That's probably how you should do it anyway. Just note that the user could enter something like "argle" and it would still be accepted since you asked for a string, not an integer.
Thanx for the reply.I tried to use int and short but both were causing my next string to be skipped. Well at least the user input portion of it, it was just going to the next string and continuing fine from there. I tried a cin.ignore(255,'/n') after the next string but it just made the program stop there. The forum I got the cin from says its a patch for a glitch in c++. I am not sure if it is something I am doing or just a glitch. If you know the answer I would appreciate the help on that.
The cin.ignore() should go right after the getline if I'm not mistaken. And as firedraco pointed out, if the user enters a value larger than what int can store, you will have overflow in the input stream. The cin.ignore() should handle the overflow and ignore one extra line return character in the buffer. Between getting the user input for the first number and the cin.ignore(), this should make it basically flawless.
The cin.ignore() worked fine for the intended purpose as the int cout was asking for an age. I inserted it after my cin >> num; . I will however take into consideration that I may in the future need to be prepared for larger numbers. Now that I had the obvious pointed out to me I am not sure how I missed these mistakes but I guess thats why I am taking a class. After rereading that section (for the 10th time) since knowing how to use it, it made perfect sense. Thank you for the help.