I need help with a do while loop

Pages: 12
Maybe it is because 'x' is a double and you are comparing it to 'i' which is an int.

Instead of using 'x' in this function create another int variable for the number the user enters. This way you are comparing int to int and not an int to a double.
Thanks for the advice Soxki I will try cahing some things around
Oh and here is yet another revision of my code.
Thankyou for the help this has actually the most support I have received on a forum before and this site has taught me allot about c++.

Well heres 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
#include <iostream>
#include <windows.h>
#include <cmath>
#include <fstream>

using namespace std;
double x, y;
string z, s, t;
int o;
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 >> o;
    ofstream a("file.txt", ios::app);
    int i = 0;
    for(i = 0; i < o; i++);
    {
        a << t << i << 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':
        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 part that is not working properly:
1
2
3
4
5
6
7
8
9
10
11
12
void file()
{
    system("cls");
    cout << ("File Writer");
    cout << ("Enter a word: ");
    cin >> t;
    cout << ("enter a number");
    cin >> o;
    ofstream a("file.txt", ios::app);
    int i = 0;
    for(i = 0; i < o; i++);
    {
Oh and another question how do I type "|" character on windows because it it would make it easeir to use:
 
ios::binary | ios::app
I tried some thing else that does not appear to work but 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
146
147
#include <iostream>
#include <windows.h>
#include <cmath>
#include <fstream>

using namespace std;
double x, y;
string z, s, t;
int o;
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") << endl;
    cout << ("Enter a word: ");
    cin >> t;
    cout << (" Enter a number: ");
    cin >> o;
    fstream a("file.txt",ios::out | ios::in | ios::app);
    int i;
    for(i=0; i!=o; i++);
    {
        a << t <<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':
        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;
    }
}


Again the code that is giving trouble(although I think I have made progress)

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void file()
{
    system("cls");
    cout << ("File Writer") << endl;
    cout << ("Enter a word: ");
    cin >> t; // get string to be written to file repeat for duration of loop
    cout << (" Enter a number: ");
    cin >> o;
    fstream a("file.txt",ios::out | ios::in | ios::app);
    int i;
    for(i=0; i!=o; i++); // allow user input from variable o to set loop duration
    {
        a << t <<endl;
    }
    a.close();
}
The problem is that it only writes the string once but not the amount of times set by variable o. After the first string is written to file the it exits with code 0x00.
I am now using a do while loop however it still exits after writing the first line to the file.

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++);
}
Here is what i believe you are looking for. I changed to ofstream from ios::app to ios::out so that the program automatically overwrites the current data(you may not care about this change but it was driving me nut); i changed the number of iteration in the while loop to five just for debugging (you have to change it back to 100). Let us know if it works. The function declares a string object only when it is called (you may not care about this one).

void file()
{
string t;
ofstream a("file.txt", ios::out);
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
{
++i;
cout << t << endl;
a << t << endl;
}
while(i<5);
}

I am now using a do while loop however it still exits after writing the first line to the file.


 
while(i<=100, i++);


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

Thank you!!!!!!!!!!!!!!!!!!
This has actually been the most confusing problem that I had to deal with.
I am actually gong to have the compiler warn when code will never be executed. Thanks.
I am actually gong to have the compiler warn when code will never be executed.


A warning about code that is not executed won't catch this problem. The problem is that for loop go round and round doing nothing and then code in braces is done once. Not sure whether there are warnings about null statements.

I recommend turning all warnings on and extra warnings and compile to the ANSI standard

I guess you just have to be really careful with your typing.

If you use an IDE it should highlight syntax errors, but it might not in this case because it is legal.

The other thing to do, is to put the opening brace straight after the ) like I have above.

Now that it's working, could you post all your code again - I would like to see if you have done all the things I suggested. If you do then you could get quite a few extra marks for your assignment.
Topic archived. No new replies allowed.
Pages: 12