This question already has an answer here:
C: gets() skips the first input [closed]
(1 answer)
Closed 6 years ago.
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main()
{
int n,i;
char a[10][100];
printf("\n Enter the no. of strings:");
scanf("%d",&n);
printf("\n enter the %d numbers:",n);
for(i=0;i<n;i++)
{
printf("\n %d",i);
gets(a[i]);
}
for(i=0;i<=n;i++)
{
puts(a[i]);
}
return 0;
}
If n = 3 then it takes only two strings at index 1 and 2 it skips 0, why doesn't it take input at 0 ?
Here a is my array of strings.
The reason for wrong behavior is that scanf does not read the ENTER which is necessary to confirm the input of n. If you add dummy call of gets it does:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main()
{
int n,i;
char a[10][100];
printf("\n Enter the no. of strings:");
scanf("%d",&n); gets(a[0]);
printf("\n enter the %d numbers:",n);
for(i=0;i<n;++i)
{
printf("\n %d",i);
gets(a[i]);
}
for(i=0;i<n;++i)
{
puts(a[i]);
}
return 0;
}
Please diff my version with the original one. I did fix another issue in the output loop.
Related
This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 4 months ago.
I'm trying to build a word guesser where you guess a word from the given choice. The words are stored in an array. The player is losing even though they must win.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void guess(int N) {
int number, guess, numberofguess = 0;
int myval[] = {"R22", "B3", "R33", "B232", "R12",
"B45", "R2", "B12", "R45", "B32"};
srand(time(NULL));
number = rand() % N;
printf("choose a number "
"{'R22','B3','R33','B232','R12','B45','R2','B12','R45','B32'}\n");
char str[20];
gets(str);
printf("your guess is %s\n", str);
if (str == myval[number]) {
printf("you win\n");
} else
printf("you lose\n");
printf("the number is %s", myval[number]);
}
main() {
int N = 9;
guess(N);
return 0;
}
I want to make the code in such a way when the player enters the code and it matches with the random output the if statement works.
Use strcmp while comparing the strings in C.
When creating an array from strings, you should create it as char*.
Using gets is not good practice
You should specify the return type of all functions, including main(), which should return an int.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
void guess(int N) {
int number, guess, numberofguess = 0;
const char* myval[] = {"R22", "B3", "R33", "B232", "R12",
"B45", "R2", "B12", "R45", "B32"};
srand(time(NULL));
number = rand() % N;
printf("choose a number "
"{'R22','B3','R33','B232','R12','B45','R2','B12','R45','B32'}\n");
char str[20];
gets(str);
printf("your guess is %s\n", str);
if (strcmp(str,myval[number]) == 0) {
printf("you win\n");
} else
printf("you lose\n");
printf("the number is %s", myval[number]);
}
int main() {
int N = 9;
guess(N);
return 0;
}
I am a beginner in c and i was writing this piece of code. This is the first time im using doubles so it might be related.
The code gives the print statement in the main function, then when it enters my function ReadVector() it stops working.
I want to to learn and fix my mistake, any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void ReadVector(double *x,int size){
printf("Enter the values of vector: \n");
for(int i=0;i<size;i++)
scanf("%f",*(x + i));
}
int main(){
int m;
printf("Enter the size of vector: ");
scanf("%d",m);
double *arr= (double*)malloc(m*sizeof(double));
ReadVector(arr,m);
}
Try this one:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void ReadVector(double *x,int size){
printf("Enter the values of vector: \n");
for(int i=0;i<size;i++)
scanf("%lf", &x[i]);
}
int main(){
int m;
printf("Enter the size of vector: ");
scanf("%d",&m);
double *arr= (double*)malloc(m*sizeof(double));
ReadVector(arr,m);
}
I'm writing a program that will sort words you input alphabetically, but I found it impossible to progress because the loop doesn't work as intended.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main()
{
char string[50][50];
int i, n;
printf("Insert the number of strings: ");
scanf("%d ", &n);
for(i=0; i < n; i++)
{
printf("Insert %d. string: ", i+1);
fgets(string[i],50,stdin);
}
return 0;
}
I tried using gets() and tried to use fgets(), but the result is the same. It prints:
Insert 1. string: Insert 2. string:
Then you can insert strings, but 1 less than specified.
you have semicolon after for loop !!!
The program should accept input values on stdin until EOF is reached. The input is guaranteed to be well-formed and contain at least one valid floating point value.
Sample input:
3.1415
7.11
-15.7
Expected output:
3 3 4
7 7 8
-16 -16 -15
Done.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
for(;;)
{
float b;
scanf("%f", &b);
printf("%g %g %g\n",floor(b), round(b), ceil(b));
int i=0;
int result = scanf("%d", &i);
if( result == EOF)
{
printf("Done.\n");
exit(0);
}
}
return 0;
}
My program only runs one time. After that it outputs 0 1 1
If you want to stay with the scanf, you can use the return value to detect a erroneous/EOF input:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
int result = 0;
for(;;)
{
float b;
result = scanf(" %f", &b);
if(result == EOF)
break;
printf("%g %g %g\n",floor(b), round(b), ceil(b));
}
printf("Done");
return 0;
}
Try it here.
Also a look at this question is helpful.
I think your second scanf is the problem, like others have already told you. How about doing it this way? The getchar() call seems to pick up the EOF more reliably.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (void) {
float b;
for (;;) {
scanf("%f",&b);
printf("%g %g %g\n",floor(b),round(b),ceil(b));
if (getchar() == EOF)
break;
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
long long int a[10^9],sum=0;
int n,i,length;
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(0<=a[i]<=10^10)
{
scanf("%lld",&a[i]);
}
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("%lld",sum);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
i dont know the reason why i am getting the segentation fault this code runs fine for this input 1000000001 1000000002 1000000003 1000000004 1000000005
Issues in your code:
0<=a[i]<=10^10 is not correct, should change to 0<=a[i] && a[i]<=(10^10)
^ is a bitwise xor, not power,
In your for loop, you always compare before read element of a[], so you need to read first, then compare.
use unsigned long long, don't need int at end.
Check this code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX_NUM 1000000000ULL
#define MIN_NUM 0ULL
int main() {
int n,i;
printf("input number count: ");
scanf("%d",&n);
unsigned long long a[n],sum=0;
for(i=0;i<n;i++) {
printf("input number[%d]: ", i);
scanf("%llu",&a[i]);
if(a[i]<MIN_NUM || a[i]>MAX_NUM) {
a[i] = 0;
printf("\t(ignored, due to out of range [%llu, %llu])\n", MIN_NUM, MAX_NUM);
}
}
for(i=0;i<n;i++) {
sum+=a[i];
}
printf("\nsum: %llu\n",sum);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}