I need help with a do while loop

Pages: 12
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <windows.h>
#include <cmath>
#include <fstream>

using namespace std;
double x, y;
string z, s, t;
char d;
string l = ("wget.exe "), k = ("color ");

void multiplication()
{
    system("cls");
    cout << ("Multiplication") << endl;
    cin >> x;
    cin >> y;
    cout << ("The answer is:") << x*y << endl;
    Sleep(3000);
}
void file()
{
    ofstream a("file.txt", ios::app);
    system("cls");
    cout << ("File Writer") << endl;
    cout << ("Enter a word: ");
    cin >> t;
    cout << t << (" will be printed 100 times in a text file.") << endl;
    int i = 0;
          do
    {
        cout << t;
        a << t << endl;
    }
    while(i<=100, i++);
}
void color()
{
    system("cls");
    cout << ("Console ColorChanger.") << endl;
    cout << ("choose a console color:") << endl;
    cout <<   ("0 = Black       8 = Gray") <<endl;
    cout <<   ("1 = Blue        9 = Light Blue") <<endl;
    cout <<   ("2 = Green       A = Light Green") <<endl;
    cout <<   ("3 = Aqua        B = Light Aqua") <<endl;
    cout <<   ("4 = Red         C = Light Red") <<endl;
    cout <<   ("5 = Purple      D = Light Purple") <<endl;
    cout <<   ("6 = Yellow      E = Light Yellow")<<endl;
    cout <<   ("7 = White       F = Bright White") <<"\n";
    cout << ("color: ");
    cin >> s;
    k = k + s;
    system(k.c_str());
}
void wget()
{
    system("cls");
    cout << ("Enter the URL for the website that you wish to download to download:") << endl;
    cout << ("URL: ") << endl;
    cin >> z;
    l = l + z;
    system(l.c_str());
}
void squareroot()
{
    system("cls");
    cout << ("SquareRoot") << endl;
    cin >> x;
    sqrt(x);
    cout << ("The answer is:") << y << endl;
    Sleep(3000);
}
void subtraction()
{
    system("cls");
    cout << ("Subtraction") <<endl;
    cin >> x;
    cin >> y;
    cout << ("The answer is:") << x-y << endl;
    Sleep(3000);
}
void addition()
{
    system("cls");
    cout << ("Addition") <<endl;
    cin >> x;
    cin >> y;
    cout << ("The answer is:") << x+y << endl;
    Sleep(3000);
}
void division()
{
    system("cls");
    cout << ("Division") <<endl;
    cin >> x;
    cin >> y;
    cout << ("The answer is:") << x/y << endl;
    Sleep(3000);
}

int main()
{
    system("title OmniTool");
    system("cls");
    cout << ("Please choose a mode.") << endl;
    cout << ("Press M to goto Multiplication") << endl;
    cout << ("Press D to goto Division") << endl;
    cout << ("Press A to goto Addition") << endl;
    cout << ("Press S to goto Subtraction") << endl;
    cout << ("Type R to goto SquareRoot") << endl;
    cout << ("Press C to change color.") << endl;
    cout << ("Press W to goto Website Downloader.") << endl;
    cout << ("Press Q to exit.") << endl;
    cout << ("Press F to goto file writer.") << endl;
    cout << ("Mode: ");
    cin >> d;
    switch(d)
    {
    case 'm':
        multiplication();
        break;
    case 'd':
        division();
        break;
    case 'a':
        addition();
        break;
    case 's':
        subtraction();
        break;
    case 'r':
        squareroot();
        break;
    case 'w':
        wget();
        break;
    case 'q':
        return 0;
    case 'c':
        color();
        break;
    case 'f':
        file();
        break;
    }
}

And this is the code I am having trouble with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void file()
{
    ofstream a("file.txt", ios::app);
    system("cls");
    cout << ("File Writer") << endl;
    cout << ("Enter a word: ");
    cin >> t;
    cout << t << (" will be printed 100 times in a text file.") << endl;
    int i = 0;
          do
    {
        cout << t;
        a << t << endl;
    }
    while(i<=100, i++);
}
Last edited on
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.

hi I revised the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <windows.h>
#include <cmath>
#include <fstream>

using namespace std;
double x, y;
int i;
string z, s, t;
char d;
string l = ("wget.exe "), k = ("color ");

void multiplication()
{
    system("cls");
    cout << ("Multiplication") << endl;
    cin >> x;
    cin >> y;
    cout << ("The answer is:") << x*y << endl;
    Sleep(3000);
}
void file()
{
    system("cls");
    cout << ("File Writer");
    cout << ("Enter a word: ");
    cin >> t;
    while (true);
    ofstream a("file.txt", ios::app);
    a << t << endl;
    cin >> i;
    for (int i; x<i; x++)
    {
        ofstream a("file.txt", ios::app);
        a << t << x << endl;
        a.close();
    }
}
void color()
{
    system("cls");
    cout << ("Console ColorChanger.") << endl;
    cout << ("choose a console color:") << endl;
    cout <<   ("0 = Black       8 = Gray") <<endl;
    cout <<   ("1 = Blue        9 = Light Blue") <<endl;
    cout <<   ("2 = Green       A = Light Green") <<endl;
    cout <<   ("3 = Aqua        B = Light Aqua") <<endl;
    cout <<   ("4 = Red         C = Light Red") <<endl;
    cout <<   ("5 = Purple      D = Light Purple") <<endl;
    cout <<   ("6 = Yellow      E = Light Yellow")<<endl;
    cout <<   ("7 = White       F = Bright White") <<"\n";
    cout << ("color: ");
    cin >> s;
    k = k + s;
    system(k.c_str());
}
void wget()
{
    system("cls");
    cout << ("Enter the URL for the website that you wish to download to download:") << endl;
    cout << ("URL: ") << endl;
    cin >> z;
    l = l + z;
    system(l.c_str());
}
void squareroot()
{
    system("cls");
    cout << ("SquareRoot") << endl;
    cin >> x;
    sqrt(x);
    cout << ("The answer is:") << y << endl;
    Sleep(3000);
}
void subtraction()
{
    system("cls");
    cout << ("Subtraction") <<endl;
    cin >> x;
    cin >> y;
    cout << ("The answer is:") << x-y << endl;
    Sleep(3000);
}
void addition()
{
    system("cls");
    cout << ("Addition") <<endl;
    cin >> x;
    cin >> y;
    cout << ("The answer is:") << x+y << endl;
    Sleep(3000);
}
void division()
{
    system("cls");
    cout << ("Division") <<endl;
    cin >> x;
    cin >> y;
    cout << ("The answer is:") << x/y << endl;
    Sleep(3000);
}

int main()
{
    system("title OmniTool");
    system("cls");
    cout << ("Please choose a mode.") << endl;
    cout << ("Press M to goto Multiplication") << endl;
    cout << ("Press D to goto Division") << endl;
    cout << ("Press A to goto Addition") << endl;
    cout << ("Press S to goto Subtraction") << endl;
    cout << ("Type R to goto SquareRoot") << endl;
    cout << ("Press C to change color.") << endl;
    cout << ("Press W to goto Website Downloader.") << endl;
    cout << ("Press Q to exit.") << endl;
    cout << ("Press F to goto file writer.") << endl;
    cout << ("Mode: ");
    cin >> d;
    switch(d)
    {
    case 'm':
        break;
        multiplication();
    case 'd':
        division();
        break;
    case 'a':
        addition();
        break;
    case 's':
        subtraction();
        break;
    case 'r':
        squareroot();
        break;
    case 'w':
        wget();
        break;
    case 'q':
        return 0;
    case 'c':
        color();
        break;
    case 'f':
        file();
        break;
    }
}


However, I cannot figure out this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void file()
{
    system("cls");
    cout << ("File Writer");
    cout << ("Enter a word: ");
    cin >> t;
    while (true);
    ofstream a("file.txt", ios::app);
    a << t << endl;
    cin >> i;
    for (int i; x<i; x++)
    {
        ofstream a("file.txt", ios::app);
        a << t << x << endl;
        a.close();
    }
}
What I meant about forward declaration was this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <windows.h>
#include <cmath>
#include <fstream>

using namespace std;
double x, y;
int i;
string z, s, t;
char d;
string l = ("wget.exe "), k = ("color ");

void ShowMenu();
void ProcessMenu();
void multiplication();
void file();
// all the other ones in a similar fashion

int main()
{
ShowMenu();
 ProcessMenu(char MenuOption);
cin >> d;
}

void ShowMenu()
{

system("title OmniTool");
    system("cls");
    cout << ("Please choose a mode.") << endl;
    cout << ("Press M to goto Multiplication") << endl;
    cout << ("Press D to goto Division") << endl;
    cout << ("Press A to goto Addition") << endl;
    cout << ("Press S to goto Subtraction") << endl;
    cout << ("Type R to goto SquareRoot") << endl;
    cout << ("Press C to change color.") << endl;
    cout << ("Press W to goto Website Downloader.") << endl;
    cout << ("Press Q to exit.") << endl;
    cout << ("Press F to goto file writer.") << endl;
    cout << ("Mode: ");
}

void ProcessMenu(char MenuOption)
{
switch(d)
    {
    case 'm':
        break;    //this is in the wrong place
        multiplication();
    case 'd':
        division();
        break;
    case 'a':
        addition();
        break;
    case 's':
        subtraction();
        break;
    case 'r':
        squareroot();
        break;
    case 'w':
        wget();
        break;
    case 'q':
        return 0;
    case 'c':
        color();
        break;
    case 'f':
        file();
        break;
    }

}
void multiplication()
{
    system("cls");
    cout << ("Multiplication") << endl;
    cin >> x;
    cin >> y;
    cout << ("The answer is:") << x*y << endl;
    Sleep(3000);
}

//the other definitions down here as well


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.

However, I cannot figure out this:


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
1
2
3
string z;   //description of what this for and what is does
string s;   //description of what this for and what is does
string WordToOutput;   //A word that is output to a file 


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:

1
2
3
4
5
6
for (int i; x<i; x++)
    {
        ofstream a("file.txt", ios::app);
        a << t << x << endl;
        a.close();
    }


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
I wrote it but its still confusing.
Well you read that post quickly. :)

Have you thought about the things I've mentioned?
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.
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.
Also, I revised my code again.

Here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <windows.h>
#include <cmath>
#include <fstream>

using namespace std;
double x, y;
int i;
string z, s, t;
char d;
string l = ("wget.exe "), k = ("color ");

void multiplication()
{
    system("cls");
    cout << ("Multiplication") << endl;
    cin >> x;
    cin >> y;
    cout << ("The answer is:") << x*y << endl;
    Sleep(3000);
}
void file()
{
    system("cls");
    cout << ("File Writer");
    cout << ("Enter a word: ");
    cin >> t;
    cout << ("enter a number");
    cin >> x;
    ofstream a("file.txt", ios::app);
    for (x; i<x; i++);
    {
        a << t << x << endl;
    }
}
void color()
{
    system("cls");
    cout << ("Console ColorChanger.") << endl;
    cout << ("choose a console color:") << endl;
    cout <<   ("0 = Black       8 = Gray") <<endl;
    cout <<   ("1 = Blue        9 = Light Blue") <<endl;
    cout <<   ("2 = Green       A = Light Green") <<endl;
    cout <<   ("3 = Aqua        B = Light Aqua") <<endl;
    cout <<   ("4 = Red         C = Light Red") <<endl;
    cout <<   ("5 = Purple      D = Light Purple") <<endl;
    cout <<   ("6 = Yellow      E = Light Yellow")<<endl;
    cout <<   ("7 = White       F = Bright White") <<"\n";
    cout << ("color: ");
    cin >> s;
    k = k + s;
    system(k.c_str());
}
void wget()
{
    system("cls");
    cout << ("Enter the URL for the website that you wish to download to download:") << endl;
    cout << ("URL: ") << endl;
    cin >> z;
    l = l + z;
    system(l.c_str());
}
void squareroot()
{
    system("cls");
    cout << ("SquareRoot") << endl;
    cin >> x;
    sqrt(x);
    cout << ("The answer is:") << y << endl;
    Sleep(3000);
}
void subtraction()
{
    system("cls");
    cout << ("Subtraction") <<endl;
    cin >> x;
    cin >> y;
    cout << ("The answer is:") << x-y << endl;
    Sleep(3000);
}
void addition()
{
    system("cls");
    cout << ("Addition") <<endl;
    cin >> x;
    cin >> y;
    cout << ("The answer is:") << x+y << endl;
    Sleep(3000);
}
void division()
{
    system("cls");
    cout << ("Division") <<endl;
    cin >> x;
    cin >> y;
    cout << ("The answer is:") << x/y << endl;
    Sleep(3000);
}

int main()
{
    system("title OmniTool");
    system("cls");
    cout << ("Please choose a mode.") << endl;
    cout << ("Press M to goto Multiplication") << endl;
    cout << ("Press D to goto Division") << endl;
    cout << ("Press A to goto Addition") << endl;
    cout << ("Press S to goto Subtraction") << endl;
    cout << ("Type R to goto SquareRoot") << endl;
    cout << ("Press C to change color.") << endl;
    cout << ("Press W to goto Website Downloader.") << endl;
    cout << ("Press Q to exit.") << endl;
    cout << ("Press F to goto file writer.") << endl;
    cout << ("Mode: ");
    cin >> d;
    switch(d)
    {
    case 'm':
        multiplication();
        break;
    case 'd':
        division();
        break;
    case 'a':
        addition();
        break;
    case 's':
        subtraction();
        break;
    case 'r':
        squareroot();
        break;
    case 'w':
        wget();
        break;
    case 'q':
        return 0;
    case 'c':
        color();
        break;
    case 'f':
        file();
        break;
    }
}


The Problematic code:

1
2
3
4
5
6
7
8
9
10
11
12
13
void file()
{
    system("cls");
    cout << ("File Writer");
    cout << ("Enter a word: ");
    cin >> t;
    cout << ("enter a number");
    cin >> x;
    ofstream a("file.txt", ios::app);
    for (x; i<x; i++);
    {
        a << t << x << endl;
    }
Last edited on
Also x is determined by the users input as to allow a custom loop.
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.


I was just trying to promote some good programming practice.

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.


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:

1
2
3
4
string word = "cat";
int counter = 0;
for(counter = 0;counter < 10; counter++)
       cout << word << endl;


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.
What about:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void file()
{
    system("cls");
    cout << ("File Writer");
    cout << ("Enter a word: ");
    cin >> t;
    cout << ("enter a number");
    cin >> x;
    ofstream a("file.txt", ios::app);
    int i = 0;
    for(i = 0; i < x; i++);
    {
        a << t << n++ << endl;
    }
    a.close();
}
Also in:

 
for(i = 0; i < x; i++);


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.
I also need a way to return to the main menu once the part that was selected completes.
1
2
3
4
for(i = 0; i < x; i++);
    {
        a << t << n++ << endl;
    }


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.
The problem is the loop wont complete it just exits.
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'.
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.

The problem is the loop wont complete it just exits.


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.

I also need a way to return to the main menu once the part that was selected completes.


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?
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:

1
2
3
word1
word2
word3


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.
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:
1
2
3
4
string theStringToPassToSystem = "wget.exe";
cin >> x;
theStringToPassToSystem = theStringToPassToSystem + x;
system(theStringToPassToSystem.c_str());


I thought that if I changed this
 
theStringToPassToSystem = "wget.exe";

to this:
 
z = "wget.exe";

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
Pages: 12