The program asks the user for his password(12345), and if the user types the wrong password, the program keeps asking. The program runs on my compiler, but everything I type is wrong, what's wrong?
Thanks

10-03-2005

rockytriton

hmm, try giving the address of x, I'm pretty sure you need that, though it's been a while since I've used scanf

10-03-2005

Alastor

what's an address?

10-03-2005

itsme86

Use the address-of operator. e.g.: scanf("%d", &x);

10-03-2005

Alastor

B-e-a-utiful!!

10-03-2005

cwr

main needs to return int, not void. When you've fixed that, also put a return 0; at the end of your main function.

The other problem with your code is that your scanf will repeat if something is entered that's not a valid integer. This is because scanf will stop scanning when it hits the invalid character, and when you call it again, it will immediately hit it and stop, ad infinitum.

Thanks for the tip, but if you type a non-integer (2.3, for example) it goes into an infinite loop, one of the problems that you told me about (though I made the changes you said?) so I'm not sure what happens now (lol)
Thanks

10-03-2005

cwr

Alastor, before you post to a thread, please read all posts in it.

The reason it goes into an infinite loop is in my post directly above your last post. Read the FAQ URL I posted.

10-04-2005

Alastor

I read a few parts of the FAQ, but I thought that if I did...

Quote:

main needs to return int, not void. When you've fixed that, also put a return 0; at the end of your main function.

it would solve the problem, without seeing that it would still go into an infinite loop

Quote:

The other problem with your code is that your scanf will repeat if something is entered that's not a valid integer

. I tried making X a double, but it didn't work... man, next time I read that scanf sucks I'll say: true that!

10-04-2005

cwr

No, the FAQ I gave you explains directly why scanf behaves the way it does, and why you get an infinite loop. The infinite loop has nothing to do with your incorrect declaration of main.

Also, scanf doesn't suck, it's just misunderstood. It stops reading when it hits a character that is unexpected for the format it has been given, this is documented and expected behaviour. If you don't like that behaviour, read the whole line using something like fgets and then use sscanf on the string, or similar as desired. If you'd bothered to read the FAQ I gave you in the first place, your problem would have been fixed more than a day ago.