Question:
c program-do you wish to continue(y/n)?
nikhil k
2007-08-14 11:34:05 UTC
i wish to put a do you wish to continue (y/n) to print first 10 numbers and continue to print next 10 numbers if user says 'y'
main()
{
char ans='y';
int i=0;int n;
while(ans=='y')
{
do
{
i=i+1;
printf("%d",i);
}
while(i<=n);
n=n+10;
printf("do you wish to continue (y/n)");
scanf("%c",&ans);
}
//what do i use here getche getchar putchar i am not able to figure out the exact difference between getche and getchar not just the definitions in programs their application//
getch();
}
ignore other errors
Five answers:
i_am_the_next_best_one
2007-08-14 23:50:56 UTC
ans=getchar();



is what you exactly need.



getche may work.



but using getch(), u wont be able to see what user inputed during execution of the program.



ans=getch();

putchar (ans);



along with putchar(); getch() gives similar result.
Amit
2007-08-16 22:20:09 UTC
Probably you are asking about why that getch is there at the end of main, right?



see as people explained above getch accepts a charater from user but does not display it. Now when you run the program and display your numbers, after showing 10 numbers you ask user "do you wish to continue (y/n)".



If user says "y" i.e. yes then program continues so user can see the next 10 numbers.



But what will happen when user press 'n'?

program will come out of loop and there comes the getch() in picture.



Now if that getch() is not there then the program will come out of loop and terminate, so user will not be able to understand whether it was a normal termination or abnormal termination.



So it is general practice to keep one getch() [as it does not echo input on screen] at the end, which waits for user to press final key for exiting program. This assures user that program behaved normally and terminated properly after pressing 'n'.



Generally programmers have a bye bye message before that getch() which makes that getch() more meaningful. :)



But please understand that this is not compulsory and your program will not be affected even if you remove this.
?
2016-05-18 01:36:17 UTC
He should not be learning c. The firefox project is the cumulative effort of a whole open-source team which includes some very very skilled programmers. The best he could do is download the whole firefox source code and try to build it. Which itself is no small feat, not to mention needing to download the various software to build it. Nevertheless, there are too many technologies involved in the browser. The easiest way to building his own browser, is to succumb to using Microsoft .NET technologies. There are a few ways to doing that: 1. Pay between $600 - $900 for Visual Studio 2005. 2. Pay an equivalent amount or more for a similar product from Borland. 3. Buy an OEM or education version for much less. 4. Recommended: Download from Microsoft the free Visual Studio Express for Visual Basic, C# and C/C++. Why? Because all the components are already found in .NET. He would just need to drag the browser component from the component library tray and drop it onto the visual programming space. Would he learn how to program that way? Would he be able to create a browser that behaves the way he wants? Yes and yes, because he could modify the behaviour and configuration of the components he uses. He gets to see a browser running, even before he wrote any code, just by dropping the browser component and clicking the "build" button. He could start with Visual Basic. Perhaps, he wants to have a godzilla pounding the foreground while a the contents of a slow website is coming in. Perhaps, he could start with making the URL bar (aka address bar) being able to read in phrases like "My mommy's website" which his lookup table would actually link to the actual address of his mommy's website. May be he wants a File Explorer component tab together with his Browser component tab. May be, he wants to use the File Explorer component tab to store the hierarchy of his Favourites. And then, after many many many months he could progress to C# or C/C++. By that time, he might decide he would rather create a simple game which would then lead him to a future of becoming a game programmer, which would make more sense. Of course, one of the most annoying traits of Visual Basic programmers who move to C# or C++ is bringing the whole baggage of BASIC style spaghetti programming with them rather than learning to accept the conveniences offered by object-oriented programming. I am sure certain quarters would brand this recommendation as betrayal on my part, but I don't think the kid is ripe for learning java and using hotspot components.
alpa s
2007-08-15 03:26:46 UTC
the major difference between getche n getch is getchar echoes the character which u have entered tht is it dislpays in on the screen but getch doesnt .getchar is like getche only.putchar is used to display .n here u shuld use getch only .ok
kiru
2007-08-14 12:05:03 UTC
You need not use getch() there since you are already scanning a variable(trying to get a character) at the end... You may just use exit(0).....


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...