While loop

Jan 17, 2012 at 10:29am
Can someone help me with this.You said that for repeating program I use do-while,and when I say at the end of the program

cout << " Do you want to continue y/n : ";

I put that inside of while() but every time that program repeats like milion times ? HELP
Jan 17, 2012 at 1:16pm
Please copy full cycle code to codepad.org and give us a link
Jan 17, 2012 at 1:44pm
Jan 17, 2012 at 2:57pm
you have the condition in the while loop wrong.

"ope = 'n' " writes the character into the variable so its always true/possible and therefore an infinite loop.

you need to use either the operator "==" (which should work for chars but not sure now) or if you are using Strings the strcmp() method.

edit: also you have a strange method of putting the brackets, its hard to locate them in your code so maybe you can make them more visible by putting them in an own line.
Last edited on Jan 17, 2012 at 4:13pm
Jan 17, 2012 at 4:01pm
Thanks for that and one more question. In that link why does it say "Line 7: error: '_TCHAR' has not been declared " don't get that ?
Jan 17, 2012 at 4:15pm
do you have that error in your compiler or was it automatically added when you uploaded the file? (dont know much about codepad)

if its in your compiler, what OS are you using and what IDE (if you are using one)
Jan 17, 2012 at 4:36pm
Its shows when I put it in codepad
Jan 17, 2012 at 6:14pm
Sometimes in certain editors there are invisible characters.

Becareful.
Jan 17, 2012 at 8:15pm
well then I can only assume that codepad has something like a built in compiler which doesnt recognize the datatype _TCHAR for whatever reason.

I would suggest that you ignore that as long as it is working fine on your PC.
Jan 17, 2012 at 11:03pm
do it like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
do{
cout << "How are you?\n";
getline(cin, a);


cout << a << "thats cool"\n;
system("pause");
cout << " Do you want to continue y/n : ";
cin >> a;
}
while(a == y);

if(a == n){
return 0;
}
Jan 17, 2012 at 11:07pm
and why does it say that var ope is being used without initialization,how to fix that
Jan 17, 2012 at 11:11pm
how to fix that


char ope; // What is the value of ope? Who knows. It could be anything. It is uninitialised

char ope = 'z'; // What is the value of ope? The value is 'z'. It has been initialised
Jan 17, 2012 at 11:12pm
have you declared it as int ope, or long ope, char ope, string ope, float ope?
Jan 17, 2012 at 11:40pm
Tried to help you out. Has a flaw where ervything comes out to -54 or 54. lol im pretty sure 1+1=2 not 54.

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
#include <iostream>

using namespace std;
int main()
{
    int a, b;
    int Choice;
    int r;

    cout << "Choose a operation.\n";
    cout << "1) Addition\n";
    cout << "2) Subtraction\n";
    cout << "3) Multiplication\n";
    cout << "4) Division\n";
    cin >> Choice;
    
    if(Choice == 1){
              cout << "Enter a number:";
              cin >> Choice;
              cout << endl;
              cout << "Enter a second number:";
              cin >> Choice;
              cout << endl;
              r = a + b;
              cout << "The number = " << r;
              system("pause");
              }
    else if(Choice == 2){
         cout << "Enter a number:";
         cin >> Choice;
         cout << endl;
         cout << "Enter a second number:";
         cin >> Choice;
         cout << endl;
         r = a - b;
         cout << "The number = " << r;
         system("pause");
         }

}
Jan 17, 2012 at 11:55pm
Thank you people,my program finnaly works as I wanted :)
Topic archived. No new replies allowed.