| ostar2 (71) | |||||
|
I am trying to write a string to a text file in a loop . It compiles correctly without errors. However, it currently only writes one line any help would be great. Thanks. Anyway here is the code:
And this is the code I am having trouble with:
| |||||
|
Last edited on
|
|||||
| TheIdeasMan (1757) | |
|
Hi ostar2, 2 problems here: Your case's should have break; at the end of each one so that control doesn't fall through to the next case. goto's are really bad form. You need to put the switch statement and the menu stuff into a while loop with an appropriate test condition. Goto's are really bad because the can easily turn things into a huge mess. The cout's that print the menu could go into function of their own, and if the case's have lots of code in them they could be functions as well. Hope that helps a bit. | |
|
|
|
| ostar2 (71) | |||||
hi I revised the code:
However, I cannot figure out this:
| |||||
|
|
|||||
| TheIdeasMan (1757) | ||||||||
What I meant about forward declaration was this:
Your code will fail if the user enters a capital letter, which is what you asked for in the menu. Convert the response to lower case . You have global variables, which seems easy, but is bad style because it gets messy.
Did you write it, or is it copied from somewhere? while (true);Can you explain what this does? Look up the while statement in the reference pages on this site . Try to figure out how you can rearrange the file() function. Here's another style issue: string z, s, t;do this instead
I Don't use single characters for variables - it can lead to confusion. Give things a meaningful name - a word or words joined together. t could be WordToOutput like I have shown above. cin >> i;You haven't said to the user what this for. The program inexplicably seems to stop - the user might not realise they have to input something. I am confused about what you are doing with this:
What is x equal to at the start of the loop? These shouldn't be in the for loop, they will be executed multiple times. ofstream a("file.txt", ios::app);a.close(); | ||||||||
|
Last edited on
|
||||||||
| ostar2 (71) | |
| I wrote it but its still confusing. | |
|
|
|
| TheIdeasMan (1757) | |
|
Well you read that post quickly. :) Have you thought about the things I've mentioned? | |
|
|
|
| ostar2 (71) | |
|
The idea is that the program writes a word to the file a certain amount of times. The amount of times depends on the the users input. so for instance if the user if the enters 10 for the loop and cat for the word it would output the word cat 10 ten times. | |
|
|
|
| ostar2 (71) | |
| also this a personal project and I am aware that a capital letter will cause a failure however this program is only being created because I need the practice so no one else will be using it. | |
|
|
|
| ostar2 (71) | |||||
|
Also, I revised my code again. Here it is:
The Problematic code:
| |||||
|
Last edited on
|
|||||
| ostar2 (71) | |
| Also x is determined by the users input as to allow a custom loop. | |
|
|
|
| TheIdeasMan (1757) | |||||
I was just trying to promote some good programming practice.
So which variable x or i refers to the number of times the word is printed? You have both x and i in the for loop and it's confusing me. If you want to print something 10 times do this:
To be honest the logic is rather messed up in the file function. I am not trying to be sarcastic - I think you need to think carefully about the steps needed to do this task. | |||||
|
|
|||||
| ostar2 (71) | |||
What about:
| |||
|
|
|||
| ostar2 (71) | |||
Also in:
x is how long the loop goes for. My reasoning for this that with each execution another word is added to the text file. Another thing is n++ which add adds a number on the end of each word. Each word receives a number depending on what number word is so for instance word1 word2 word3 and so on. | |||
|
|
|||
| ostar2 (71) | |
| I also need a way to return to the main menu once the part that was selected completes. | |
|
|
|
| Soxki (6) | |||
Try using i + 1 instead of n++ (not sure where n came from). Every time you go through your loop i will be increased by one giving you the effect your looking for. | |||
|
|
|||
| ostar2 (71) | |
| The problem is the loop wont complete it just exits. | |
|
|
|
| Soxki (6) | |
|
The loop looks like it should run fine, maybe something is wrong with code between the { }. As far as returning to the main menu after the selection completes i would consider putting a Do-While loop around your main program that runs until the user enters a 'q'. | |
|
|
|
| TheIdeasMan (1757) | |||
|
The "a" variable confused me for a bit. If you had named it OutputFile, it wouldn't have. Naming variables properly is really important - give them meaningful names not single characters. I hardly ever use a single character for a variable name, even for loop counters or array subscripts.
It's writing to a file, did you look in the file? Is there a problem with the file? If you open files you should check that it worked.
In main the ShowMenu and ProcessMenu should be in a while loop with a carefully selected test-expression. have you reorganised the code with forward declarations of the functions at the top & function definitions after main? | |||
|
|
|||
| ostar2 (71) | |||
|
It writes to the file 1 time its supposed to take the input and then use that to determine the duration of the loop it is not doing this. It is also supposed to take the out put from the counter and add it on to the end of each word. So if I typed in word for the first part and 3 for the second part the output should be:
how ever the file only contains one word and not the specified number because the loop is not writing the words the specified amount of times to the text file. | |||
|
|
|||
| ostar2 (71) | |||||||
Also, I find it easier to understand single variables because sometimes I think their reserved functions. A good example would be I thought this code(kindly provide by Moschops during a previous issue) this:
I thought that if I changed this
to this:
that it would not work. I just thought it was best that I clarify why I use single char variables and not real world words. | |||||||
|
Last edited on
|
|||||||