Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello!
suppose that program
void main()
{
oop:
    int a;
    for(int i=0; i<5; i++)
    {
        cout<<"ABC"<<endl;;
    }
    cout<<"Press 1 to display again"<<endl;
    cin>>a;
c:
    if(a==1)
    {
        goto oop;
    }
    getch();
}

now, when i give a numeric value to 'a', then program goes on nicely...but when i give it a character...like; a,b,z etc then the screen starts to blink and infinite loop runs.
i know ASCII codes & i hv tried the condition
else if(a>100||a<0)
{
    cout<<"Try again";
    goto c;
}

but it still doesn't work for me.
please help me out sir!
Posted
Updated 6-Mar-11 23:40pm
v2

This is perfectly expected behavior. What would you expect, conversion from character to integer? You can do it, but then the input character "1" will return integer equal not to 1 but ASCII value of "1" which is 0x31.

How to do things correctly? First, you need to decide what you really want on input (you cannot do both, because there cannot be universal application of input, integer or character(s) to be converted to ASCII). Decide what you want first.
Secondly, learn just a bit about C++ programming and them try again. Right now, you're not even trying to code in C++. Look at your "goto"? Who would do such things. This is a clear indication that you're a bit confused about programming activity in general. Start some book of on-line course with elementary coding exercises.

See my recommendations: I have a problem with my program. Please help![^].

Good luck,
—SA
 
Share this answer
 
v4
Comments
sabih toor 6-Mar-11 16:31pm    
thanks! but i am doing electrical engineering and i want to learn a bit of c++ for microcontrollers.
i am creating some simple data bases rite now. but is there any solution of avoiding this infinite loop. if yes! then plz tell me. i hope u will not mind.
Sergey Alexandrovich Kryukov 6-Mar-11 20:16pm    
As soon as you're doing programming at this moment, consider yourself a software developer (:-) and apply appropriate requirement to yourself. I know from experience that electrical engineering education just kills the ability to program in very many engineers, but you have to overcome that.

How else could you proceed? The primary problem is not infinite loop, but this happens on your "goto". Re-write the code using loop construct (see C reference, I don't know). Never use goto, as a rule of thumb. Infinite loop is such a simple case is ridiculous.
--SA
Olivier Levrey 7-Mar-11 5:41am    
My 5.
Sergey Alexandrovich Kryukov 7-Mar-11 14:57pm    
Thank you.
--SA
Here's a good article talking about the issue you're experiencing:
http://www.cplusplus.com/forum/articles/6046/[^]

As you'll learn from the article, cin with an incorrect input type results with undefined behavior, plus the newline character is not removed. In addition, your infinite loop is caused by an exception in cin coupled with your use of goto statements (which as you've seen from SA in his answer, is generally considered bad practice).
 
Share this answer
 
Comments
Olivier Levrey 7-Mar-11 5:42am    
My 5.
Sergey Alexandrovich Kryukov 7-Mar-11 14:57pm    
Very useful and important information, my 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900