why can not understand why the loop just goes on and on

Write your question here.

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
#include<iostream>
#include<conio.h>
using namespace std;
int a,b;
char id2;
int f1();
int main()
{

  cout<<"PASSWORD= ";
  while(a!=13)
  {
   
    int f1();
    if(a<400)
    {
      cout<<"&";
    }

  }
}

int f1()
{
  a=getch();
  if(a==0||a==240){a=500;b=getch();}
}
It will end when a == 13 (int value 13).

You need to check what value is going into a.

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
#include<iostream>
#include<conio.h>
using namespace std;
int a,b;
char id2;
int f1();
int main()
{

  cout<<"PASSWORD= ";
  while(a!=13)
  {
   
    int f1();
    if(a<400)
    {
      cout<<"&";
    }

  }
}

int f1()
{
  a=getch();
  std::cout << "value of a: " << a << std::endl;
  if(a==0||a==240){a=500;b=getch();}
}


You've been here ages now. Start thinking about how to solve these problems for yourself. Debugging starts simple. Just output the values and see what they are.


int getch();
that's the prototype of `getch()', note how you are calling it
a=getch();
you simply write the name of the function, and between parenthesis pass all the needed parameters.

now see what you're doing with `f1()'.
1
2
3
  while(a!=13)
  {
    int f1();
¿why do you have `int' there? that's not a function call, which should be simply f1();

So you've got an infinite loop because `a' never changed because `f1()' was never called.


Also, don't litter your code, use a debugger.
you are absolutely right
see what i am doing with int f1()
that's not a function call which should be simply f1();
so i have got an infinite loop because a never has changed because
Last edited on
Topic archived. No new replies allowed.