freelanceprogrammers.org Forum Index » C
Looping a program!
Joined: 02 Feb 2005
Posts: 2
Looping a program!
Hey all,
I`m a beginner at C++, and I`m trying to learn it, what I cant
figure out is how to make a program loop, ie. to ask at the end
whether the program should start over or not, I use a dev c++, I
tried using a do-while loop and a while loop but the best result I
get was inputtin the new value of the variable used to loop the
program and then the program terminates. So, would you please help
me?
Regards,
M.A.N
Joined: 25 Jul 2003
Posts: 48
Looping a program!
Hi,
> -----Original Message-----
> From: blade8472 [mailto:blade8472@...]
> Sent: Wednesday, February 02, 2005 10:34 AM
> To: Programmers-Town@yahoogroups.com
> Subject: (PT) Looping a program!
>
> Hey all,
> I`m a beginner at C++, and I`m trying to learn it, what I cant
> figure out is how to make a program loop, ie. to ask at the end
> whether the program should start over or not, I use a dev c++, I
> tried using a do-while loop and a while loop but the best result I
> get was inputtin the new value of the variable used to loop the
> program and then the program terminates. So, would you please help
> me?
> Regards,
> M.A.N
Show your codes. You can ignore the rest of the codes that is not related
to your looping problem.
Explain what result you got from running your code and why it did not meet
your requirement.
Shyan
Joined: 02 Feb 2005
Posts: 2
Looping a program!
Hi,> -----Original Message-----> From: blade8472 [mailto:blade8472@...]> Sent: Wednesday, February 02, 2005 10:34 AM> To: Programmers-Town@yahoogroups.com> Subject: (PT) Looping a program!> > Hey all,> I`m a beginner at C++, and I`m trying to learn it, what I cant> figure out is how to make a program loop, ie. to ask at the end> whether the program should start over or not, I use a dev c++, I> tried using a do-while loop and a while loop but the best result I> get was inputtin the new value of the variable used to loop the> program and then the program terminates. So, would you please help> me?> Regards,> M.A.NShow your codes. You can ignore the rest of the codes that is not relatedto your looping problem.Explain what result you got from running your code and why it did not meetyour requirement.Shyan
Hey, thanks for replying here are my codes:
while(res!=`n`){
.........
printf("
Do you want to start over? (Y/N)
"); scanf("%c", &res); }
When I run the program, it prints the question but it doesn`t wait to take an answer!__________________________________________________Do You Yahoo!?Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Joined: 25 Jul 2003
Posts: 48
Looping a program!
Reply embedded...
> From: Mohamed Nassar [mailto:blade8472@...]
> Sent: Friday, February 04, 2005 10:57 AM
> To: Programmers-Town@yahoogroups.com
> Subject: (PT) Re: Looping a program!
>
> Hey, thanks for replying here are my codes:
> while(res!=`n`){
>
>.........
>
> printf("
Do you want to start over? (Y/N)
");
> scanf("%c", &res);
> }
Did you use scanf() earlier? There might be characters in the buffer, most
likely the `
`.
> When I run the program, it prints the question but it doesn`t wait
> to take an answer!
First you clear the input buffer until the `
`:
scanf("%*[^
]");
Then read the response:
scanf(" %c", &c);
Notice the space before %c. This will eat away any leading whitespace
characters.
Note: Don`t think of using fflush(stdin) to clear input buffer. fflush() is
not meant for stdin and will not works on many implementation.
HTH
Shyan
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
English Courses in England - reservation calendar script
Land Surveying -Stonex instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
English Courses in England - reservation calendar script
Land Surveying -Stonex instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







