I have created a prompt which prompts the user for an integer and I have set up a loop to check for if it is an integer or not. My "bug" is that a user can enter an "integer" and "space" and "enter" and it does not give any error and assumes that "All is FINE!". I have gotten the value from the ascii table of 'SPACE' and put it as a check in my parameter of while, but it does not work.
Here is my code:
int x, y, boolean, i;
char buff[256];
printf("Enter the first integer value: ");
scanf("%s", buff);
i = 0;
boolean = 0; //initializing our boolean var that will eventually decide if we have an error or not
I want the user to be able to enter a command then a character, like for example: push r....I want the command to be stored in the array command, and the character to be stored in the variable c.
Now I wonder what the best way to get rid of the space is, using scanf or getchar (see below for code, only thing that I changed between the 2 versions is the statement before the comment "get rid of space")? Or maybe it doesnt matter?
Code:
include <stdio.h> #define MAX 200 void push(char c); // Puts a new element last in queue char pop(void); // Gets the first element in queue static char s[MAX]; }
I have to make a program for school to evalute poker hand.I am not asking about the logic of the program,I am asking because I have to take the input from the console,five lines which can be the cards 2,3,4..10 and the problem is with J,Q,K,A ,because I dont know how to tell scanf to expect integer or char ? If I type J,Q,K,A the program crash ,
I am trying to write a terminal-like chat application in Linux. I would like to use a FIFO queue to print out the messages in terminal. The queue would be populated from 2 sources- stdin and messages sent from the other user over TCP. I have meet an obstacle that I cannot handle...
Lets say I would like to take user input using fgets and put it into a buffer. Then queue it if the buffer is not empty or print if it is. The problem is that when I use fgets or scanf, my input is instantly printed to the terminal..If i do:
Code:
fgets(message, 100, stdin); printf
("%s", message The string under message is printed twice :|. Is there a way to prevent this?
I wanted to input some numbers with scanf function, i can enter some numbers and if I input -1 to the scanf, the input must end. And the scanf function has limited input, the max that I can input is 40 numbers.example if enter 1 2 4 6 5 4 -1 the scanf function will ended and the result will be appear.I wanted to know how the scanf function is like that would be best for this problem, Code: scanf("%d", &n); the result if I input those number will be like
I don't know how many numbers will be the input only that its going to be up to 10000. EOF should be active. I tried it like this:
Code:
int i = 0; int del[10000] while (scanf("%d",del[i])!=EOF) { i++; }
But it seems the value of i doesn't increment, could you provide some tips on how to scanf the inputs into an array if I don't know how many numbers will I have?
when i run it , it skips scanf and not allowing user to enter input.tried adding getchar(), fflush but nothing worked for me. //file name : sort_array_of_structure.c
Code:
/*Write a program to accept records of the different states using array of structures. The structure should contain char state, population, literacy rate, and income. Display the state whose literacy rate is highest and whose income is highest.*/
#include <stdio.h> #define M 50 struct state { char name[50]; long int population; float literacyRate; float income; }
I tried to scanf the input by the user into the function but it does not read my input. Read on one of the thread, it said that to scanf a input into a double, need to use %1f instead to %d (which normally used. I tried changing it to %1f and it still did not work.
Code: #include <stdio.h> #include <math.h> /* function main begins program execution */ int main( void ) { double amount; /* amount on deposit */
The problem that I want to make an array " vertextDegree [nbColours] " with " nbColours " elements in it ,but the "nbColours" unknown and I have to get it get it from a file .
Code:
int nbEdges,nbVetices, nbColours ; typedef struct st_graphVertex { int index; int colour; int val ; int vertexDegree[nbColours]; // it won't work because nbColours unknown // here and I want get it from file in the main struct st_graphVertex *next; t_edgeList *out; }t_grapheVertex;
I'm trying to make a very simple reaction game where a random number flickers on the screen for a fraction of a second and the user has to then enter the number before another comes on the screen after a random amount of time. However I dont know how i would make it so that the user cannot enter anything after a certain amount of time has passed, below is my code?
Also FYI, clock_start is at 5100 because by the time the program actually gets to scanning in the first number the time is at an absolute minimum of 5050 milliseconds however obviously this is an impossible number to reach due to processing, my machine clocks in at 5080.
Code:
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h> int main(void){
I have created a program using a switch statement that uses scanf() to accept a users input. The input variable is defined as an integer and scanf() is also looking for an integer.
One computer I run the program and it will return a value of 0 for all non-numerical inputs such as 'z', and the output thinks the value 0 was input into scanf(). On another computer when I input 'z', the compiler stores this as 198864884, and my program executes correctly giving an error.
int i; while (scanf("%i", &i)) printf("%i ", i); printf(" done ");
i tried several combination of Ctrl+D and "Enter", it's not terminating, Ctrl+C just "cancels" the whole thing without printing "done", i'm running this on linux installed on a PC
Below given is the code, which allocates memory for a structure dynamically and stores value in its member. The problem is in the last scanf statement which reads 'ch'. The code will be in infinite loop as it doesnt executes the last scanf statement. The solution for this is (i use this) to add one more similar scanf statement for 'ch' in the very next line. If i do so, it executes the statement, reads 'ch' and then continues.
I want to know why it behaves like that..
struct student{ int usn; }; int main(){ char ch; struct student *s; s=(struct student *)malloc(sizeof(struct student));
However when i removed this portion below, and replacing it with choice='Y';, the loop works just fine although my intention is to continue the loop only when 'y' is entered.
Code: #include <stdio.h> int main(void) { char c; int i=1; while (scanf("%c", &c)==1) printf("loop sequence %i: %c(%i)
[Code] ......
Done it seems a "carriage return" serves two purposes here, one is to signal the program to read in the character typed in before the "carriage return", another serves as a second character typed, how can i do this cleanly, that is without having to use a "carriage return" as the second character to signal "I've typed in the first character already".
I'm trying to make a character array, and be able to custom fill it by using the scanf() function to ask the user for each character spot in the array.
Code: #include <stdio.h> int main() { int i; char character_array[11]; char *charpointer; charpointer = character_array;
[Code] .....
And this is the output i get when i run it:
Code: root@kali-Tulips:~# ./myown what letter would you like in the [0] spot of the array? : a what letter would you like in the [1] spot of the array? : what letter would you like in the [2] spot of the array? : b what letter would you like in the [3] spot of the array? : what letter would you like in the [4] spot of the array? :
[Code] .....
Obviously I'm trying to grasp C programming and am very new. I don't understand why this isn't working, and i think its more than my lack of knowledge of C syntax. I believe the correct memory is allocated but when examined with gdb i don't find what i expect....