![]() |
Hi,
Long time since I've been here :) I have a problem with this code I'm writing :( I was hoping that someone could help me out :) I'm writing a simple address book to test structures and functions, and I want to allow the user to enter their details, and have the option whether to continue or exit the loop. I get this error, at the end of the loop, if I continue it prints out the First name: Last name: together, as you can see at the bottom of the text below - First name: rich Last name: morton Address1: 39 bay mews Address2: hout bay City: cape town Province: western cape Country: south africa ZIP: 8000 Continue? Quit? 1 First name: Last name: I can't figure out why this is happening?!? <annoyed> I tried moving the int cin to the last line, and also tried putting another cout but didn't work :( The code is below. I hope someone can help me :) I would really appreciate any help :) Cheers :) :
#include <iostream.h> |
No semicolons on the ends of for loop statements ;)
|
>No semicolons on the ends of for loop statements
A null statement is usually harmless provided it isn't placed somewhere that changes the logic of the code, such as immediately after the condition of a loop or if: :
for ( int i = 0; i < 10; i++ ); // A semicolon here is bad:
cout << "Continue? Quit?" << '\n';There are quite a few solutions to this problem, but the simplest is: :
cout << "Continue? Quit?" << '\n';:
cout << "Continue? Quit?" << '\n';:
#include <ios> // For streamsize |
Hey tempest, removing semi-conlons didn't work for me :(
hey Eggbert, I will have to be offline to read your reply, but when I do I will reply :) Cheers :) And thanks for the help guys :) |
Hi,
Thanks Eggbert, very interesting code you put there! But unfortunately I don't really understand it :) Would you mind explaining the three sections of code you posted? I would really appreciate that :) I did find a solution, and it seems to work without the code. It seems I was to blame for the bugs, first I was meant to put the break at the beginning of the loop: :
if( userOp == 0 )Secondly I forgot to put in the return statement!!! I don't think that matters, cos the code compiled without it. Does it? And, just to make sure, could you guys check the code below :
#include <iostream.h>Just to make sure :) And, thanks again for your help :) Very much appreciated. To Eggbert: I hope you don't mind explaining the bits of code you posted, they certainly look useful :) Cheers :) |
>Would you mind explaining the three sections of code you posted?
Happily: :
cout << "Continue? Quit?" << '\n';In the above code, I simply read a single character repeatedly until either end-of-file is encountered, an input error occurs, or a newline is found. The first two probably won't happen in interactive input unless you prompt for end-of-file as a terminator for input, so the real magic is happening with the test for '\n'. Because stream input in C++ is line oriented, we can guarantee that once a newline character is extracted from the stream, there will be nothing in the buffer and the next request for input will wait for the user to type something. :
cout << "Continue? Quit?" << '\n';:
#include <ios> // For streamsize>I did find a solution Notice how userOp is uninitialized on the first iteration of the loop. Accessing an uninitialized variable results in undefined behavior, so your code is broken even if it appears to work. |
Hey Eggbert!
Thanks very much for explaining, that certainly makes more sense to me now :) I appreciate your help, and I am sure that someday that will come in very handy :) Cheers :) OH, and yes, I moved the userOp variable into the loop. Thanks for pointing that out :) Btw How long have you been programming? |
>Btw How long have you been programming?
About 40 years. |
Not surprised, really. Oh, and I believe you can just use cin.ignore(), but I'm probably wrong.
|
>Oh, and I believe you can just use cin.ignore(), but I'm probably wrong.
You can, but that will only discard a single character. It only works if the next character on the stream is a newline. |
| All times are GMT -5. The time now is 1:55 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC