#include <stdio.h>
int main()
{
int i=0;
if (getc(stdin));
{
i++;
printf("%d\n",i);
}
return 0;
}
This is what I have tried ! But it is not working
please help me with this for my assignment!
I tried reading enter key as an input and if that is read. i is incremented and printed
Trying out your code with the "if" test (and removing the semicolon) only results in the reading and printing of the value of "1". Your question intimates that there should be some type of continuous looping, but the way your code is structured, that will not happen. The premise of your question would point at using a "while" loop along with some type of test to exit the "while" loop and end the program. With that as the basis, following is a snippet of code that would get you closer to your desired outcome.
#include <stdio.h>
int main()
{
int i=0;
char test;
while (1)
{
test = getc(stdin);
if (test == 'Q') /* To provide a way to gently end the execution of the program */
{
break;
}
if (test == '\n') /* Could use this test of the newline character to detect when enter has been pressed */
{
i++;
printf("%d\n",i);
}
}
return 0;
}
Some points to note.
A "while" loop was added to allow for a user to continually enter in characters and press the enter key (or just press the enter key). The value of "1" within the "while" loop equates to a Boolean value of "true" so this in effect makes this an endless loop.
A test was added to determine if the user enters the character "Q" which provides a way to break out of the loop and end the program - such a test and subsequent break is a customary method for exiting an endless loop.
A test was then added for sensing the pressing of the enter key - when sensed, the counter value is incremented and printed.
With that, following is some test output at the terminal.
#Dev:~/C_Programs/Console/LineNumber/bin/Release$ ./LineNumber
1
2
3
4
5
6
7
Q
#Dev:~/C_Programs/Console/LineNumber/bin/Release$
Give that a try to see if it meets the spirit of your project. One last note. This issue probably should be categorized as a "C" program issue and not a "C++" issue.
Related
#include<stdio.h>
int main()
{
int i=0;
for(;;)
{
if(i==10)
{
continue;
}
printf("%d",++i);
}
}
This above code makes the output as without print anything.But by my logic it prints the value from 1 to infinite times except 10.The continue statement doesn't run the below code after continue statement but the continue statement is inside the if condition then why it shouldn't run the statement that are below the continue statement?
The output of C-programs is normally buffered, which is likely the reason why you don't see anything. Try printf("%d\n", ++i) as the linefeed will flush the buffer.
Once i reaches 10, the number will not be increased any more, so your expectation that "it prints the value from 1 to infinite times except 10" will not be met.
For starters, I am new to C and programming in general. I have more experience with PowerShell and bash scripting, so apologies in advance for errors with headers, indentation, syntax, etc. Anyway, I'm trying to complete this program for a class, but I've had some trouble with one particular section, so I'm looking for some guidance as I'm pretty lost right now.
To provide some background: I'm supposed to allow a user to enter a user code between 6 and 10. This code uniquely identifies the user, who would then be asked for input for several other integer values, which would be totaled and averaged at the end. However, the user must be able to start the program again and enter another number (between 6 and 10); the user must then go through the previous process again to finish the program.
My problem is I cannot use if statements, break, continue, exit, abort or goto; I must use a do while loop to figure out when the user is done entering input; and I must provide error messages for when the user enters the wrong input, prompting them to enter it again.
With what I've posted below, I cannot figure out how to give the user an option to continue and/or exit without using if, break, continue, etc and while also prompting for error messages. I'm probably overthinking something but if anyone can provide some insight I would greatly appreciate it.
#include<stdio.h>
main()
{
int usercode; /* setting variables for user code */
do
{
printf ("Please enter your user code: ");
scanf("%1d", secid); /* user must input 1 digit code */
} while(secid >= 6 || secid <= 10); /* code must be between numbers 6 and 10 */
}
You don't use the loop for logging in, you use it for the other input
pseudocode:
get user id
do {
get a value
} while (value is not pause or quit)
The while at the end of a do while is like a repeated if. If will be asked again and again until it is false. Note that the block of do while will be executed at least once, because the cycle test is executed after the content of the block. You need int secid instead of int usercode. Inside the do while you need another do while to read the data and to calculate sum and avg in the process.
The code you are looking for looks like this:
#include<stdio.h>
int main(int argc, char **argv){
int secid = -1;
do {
printf("Please enter your user code: ");
scanf("%1d", &secid);
} while((secid < 6 || secid > 10) && printf("Error\n"));
printf("The user code was %d", secid);
return 0;
}
This question already has an answer here:
Read user input until ESC is pressed in C
(1 answer)
Closed 6 years ago.
let say i'm in an infinite while loop and i want the user to enter the data in integer form. Then the data will be passed to another function for some other purpose this process will keep continuing until the user input esc in the place of data and i want the loop to break at that point. how should i do that?
while(1)
{
printf("enter the data that need to entered\npress esc to exit\n");
scanf("%d",&n);
function(n);
}
i tried to use if-else but if i put the ascii value of esc as the data input it exits the loop which i don't want to happen?
As dingrite wrote in his other answer:
Your best bet is to create a custom "GetAsyncKeyState" function that will use #IFDEF for windows and linux to choose the appropriate GetAsyncKeyState() or equivalent.
No other way exists to achieve the desired result, the cin approach has its problems - such as the application must be in focus.
Take a look at this question: C++: execute a while loop until a key is pressed e.g. Esc?
Or you can try with this example found on other page
#include <stdio.h>
int main (void)
{
int c;
while (1) {
c = getchar(); // Get one character from the input
if (c == 27) { break; } // Exit the loop if we receive ESC
putchar(c); // Put the character to the output
}
return 0;
}
Hope this helps.
I found this book have many people suggest for newbie, but some code on it doesn't work, although I code it exactly like the code in the book but it still don't work
#include <stdio.h>
main()
{
int a ;
for (a = 0; getchar() != EOF ; ++a);
printf ("%d",a);
}
It looks like after the loop it ends immediately, code after the loop is not executed.
Is this book is too old? Is there any another book for self learning c programming?
int main( void )
{
int a ;
for (a = 0; getchar() != EOF ; ++a);
printf ("%d\n",a);
return 0;
}
On Unix platform run this code and when you want to exit introduce EOF by ctrl+d, if you are on windows then EOF is introduced by ctrl+z
So basically when you exit you will get the count of number of times your loop ran.
If you want to print out each input then you need to get rid of the ; at the end of for loop
int main( void )
{
int a ;
for (a = 0; getchar() != EOF ; ++a)
printf ("%d\n",a);
return 0;
}
I strongly suspect that the console closes immedeately after the loop ends. Try to insert something like system("pause") to prevent the console from closing.
The loop loops until you type EOF and prints the number of characters typed so far. To type EOF, you have to hit ctrl-z and return (in a test I had to do this after a return, so return, then ctrl-z, then return). If the console closes directly after the ctrl-z, you can add this system("pause") to wait for another key afterwards, so you see the output.
Build succeeds. If I open the output window, it reads:
Program ended with exit code: 0
But my program is such that this shouldn't be possible, without first having taken user input, done some stuff, and taken another user input, all in int main().
The first thing int main() does is loop through taking input for p, until the input is one (of two) desired options. So there's no way it should be able to exit immediately - it initialises p=0 and doesn't exit a while loop until p is 1 or 2.
Is there some hidden error that has allowed the build to succeed without it actually.. succeeding?
int main(){
//vars
while (TRUE){
//play computer or human?
while (!(p == 1 || p == 2)) {
printf("Single player or two player? (1/2): ");
scanf("%d", &p);
}
if (p==1) {
//play computer
}
else {
//snip
}
printf("%s won the game! Play again?", winner);
scanf("%s", playagain);
if (strncmp(playagain,"no",2)==0){
break;
}
}
return 0;
}
Print out what uppercase TRUE is defined as. I have seen some ambitious but inexperienced folks do weird things with it. You might not even be getting into your main while loop.
printf("TRUE is %d\n",TRUE);
If this is nonzero, then your problem is elsewhere.