[Solved] Scan input from file line by line [closed]

If you’re going to do this with fscanf, you want to use a scan set conversion, like: int a, b; char c[256]; fscanf(infile, “%d %d %[^\n]”, &a, &b, c); To scan all the lines in the file, you’d do something like: while (3 == fscanf(infile, “%d %d %[^\n]”, &a, &b, c)) process(a, b, c); fscanf … Read more

[Solved] Why is the & operator used after using scanf? [closed]

You have declared string as a single character but you fill it with a string. This invokes undefined behavior. You should change your code to : char string [20] = “default”; //20 is random, you should use the maximum length of the input you may have printf(“The default String is: %19s”, string); scanf(“%s”, string); printf(“You … Read more

[Solved] Issue in taking character input to C program

You can’t use strcasecmp to compare characters. It’s not completely wrong, but a single character is not a properly terminated string. If you must do comparison this way, use strncasecmp: if(strncasecmp(&gender,&word_M, 1) == 0) That will limit it to comparing one character. Also, your retry loop doesn’t check for ‘F’, but checks ‘M’ twice. solved … Read more

[Solved] C program goes into infinite loop with scanf [duplicate]

Check return value of scanf and clear input buffer in case of illegal input. Like this: #include <stdio.h> #include <stdbool.h> int main(void) { float sales, commission, earnings; int state; while(true) { printf( “Enter sales in dollars ( -1 to end ): ” ); if((state = scanf(“%f”, &sales )) != 1){ if(state == EOF) return 0; … Read more

[Solved] isnt everything where it should be, why the segmentation fault?

Here char *firstName[50]; firstName is array of 50 character pointer, and if you want to store anything into each of these char pointer, you need to allocate memory for them. For e.g for (int counter = 0; counter < 10; counter ++) { firstName[counter] = malloc(SIZE_FIRST); /* memory allocated for firstName[counter], now you can store … Read more

[Solved] Is it possible to run scanf like this? [closed]

I think this is what you are trying to do: int main() { int numList[5]; int i; for(i = 0; i < 5; i++) { printf(“Input number %d “,i); scanf(“%d”,&a[i]); } printf(“Your numbers: “); for(i = 0; i < 5; i++) { printf(“%d, “,a[i]); } printf(“\n”); } The method that I used for printing is … Read more

[Solved] I’m trying to make a very simple hangman program work. The while loop isn’t working [closed]

When the scanf(“%i”, &choice); statement is executed, it puts the integer entered by the user, into the int variable choice. However, it also leaves the newline character in the input buffer causing the user input to get thrown off. When the scanf(“%c\n”, &guess); statement is executed, the next character entered is placed on the input … Read more

[Solved] Name cannot be display [closed]

I have modified your code. Please see the snipet code below #include <stdio.h> #include <conio.h> #define MAX 25 char welcomeMsg[]= “Please enter your name without ‘*’ or # ” ; char errorMsg[]= “\nError, please re-type your name. “; void main(void) { int j; char input; char name[MAX]; j=0; puts(welcomeMsg); do { input = getche(); if(input … Read more

[Solved] decimal of numbers in c

The error is that %d in the format specifier of both scanf and printf represents decimal, so it expects an int (in the case of scanf, a pointer to int) Since you are declaring b and c as float, %d in the lines scanf(“%d”,&b); printf(“%d lbs is %d kgs.”,b,c); should be changed to %f respectively. … Read more

[Solved] How to handle regular expression?

My main question is, are you using C or C++ ? The outcome and the appropriate/expected answer will be given with the right information. As you are talking about C++ also, I will put a sample code to manage this using the library provided in C++ (since C++11 onwards). Be warned than as I am … Read more