I am now using a do while loop however it still exits after writing the first line to the file. |
This is wrong - it's not a valid test expression for the do while.
Don't use do while's they are as confusing as hell and aren't neccessary. What makes it even more confusing is that the while part isn't on the same line as the closing brace for the do, so anyone might easily think that it's while loop with a null statement. A for loop is perfect here.
Did you know that while loops can be written as for loops and vice-versa?
for(start-expression; end condition; increment expression) {
//do stuff
}
start-expression;
while( end condition) {
//do stuff
increment expression
}
|
I thought that if I changed this
theStringToPassToSystem = "wget.exe";
to this:
z = "wget.exe";
that it would not work. |
No. You can use almost anything for a variable name - I am saying use names that mean something. theStringToPassToSystem is very good. z isn't. Your explanation is backwards to justifying the use of short names.
theStringToPassToSystem = theStringToPassToSystem + x;
can be written like this which is less error prone for humans:
theStringToPassToSystem += x;
This works because in C++ a string is an object and += is a valid operator for that object. c_str() is a function that operates on the object so it wouldn't matter if the string was called ATripToMars.
for(i=0; i!=o; i++);
is another reason why short names are bad.
The problem is inside the loop, there was nothing wrong with this
for(i = 0; i < x; i++);
actually there is, i have just spotted it. It is the semicolon after the ). This means that he body of the loop is a null statement, so it won't do anything. The code in braces will only execute once because it isn't part of the for loop any more. Your compiler should have given a warning about that, turn all your compiler warnings on.
My earlier advice said to check that opening a filestream actually works using code is a good idea. Read up about using filestreams look at the examples to see how to do it.
Try this: I leave it up to you to do the checking on the filestream.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
void file()
{
ofstream OutputStream("file.txt", ios::app);
system("cls");
cout << ("File Writer") << endl;
cout << ("Enter a word: ");
string TheWord;
cin >> TheWord;
cout << TheWord << (" will be printed 10 times in a text file.") << endl;
int i = 0;
for(i = 0; i < 10; i++) {
a << t << endl;
}
}
|
I left the braces in the body of the for loop even though they aren't needed. It will save you one day when you add extra code in.
Hope this helps