why does the loop execute just one time? [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 months ago.
Improve this question
I would like to know why the loop below just execute one time?
Here is my code.
for(i=0;i<MAX;i++)
{
printf("name:");
gets(student[i].name);
printf("math score:");
scanf(" %d",student[i].math);
fflush(stdin);
}

At least the code contains a typo. You have to write
scanf(" %d", &student[i].math);
^^^
Also this call
fflush(stdin);
has undefined behavior.
And the function gets is unsafe and is not supported by the C Standard. Instead use function fgets or scanf.

Related

C: what's wrong with the code in the image? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I wrote a program to show the capital letter of a given small letter, but in the output console it's showing an error.
#include <stdio.h>
int main
{
char small_letter, capital_letter;
printf("Please enter a small letter: ");
small_letter = getchar();
capital_letter = small_letter - 32;
printf("The capital letter is: %c\n", capital_letter);
return 0;
}
The error seems to indicate you have multiple main functions. It looks like both of your files are being compiled together. In order to compile try renaming or deleting one of your main functions.
To explain a little further this is a linker error caused when the linker is not sure what is meant. In C you can declare functions with the same signature multiple times but you cannot define them multiple times.

Error when running C-program after first input [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
The program shuts down after I enter the first input
#include<stdio.h>
int main(void)
{
int biology,chemistry;
printf("\nEnter marks for maths");
scanf("%d",biology);
printf("\nEnter marks for tech1");
scanf("%d",chemistry);
return(0);
}
C function parameters are always "pass-by-value", which means that the function scanf only sees a copy of the current value of whatever you specify as the argument expression.
If you passed biology, then it would only see an uninitialized value. On the other hand &biology is a pointer value that refers to the variable i.e scanf can use this to modify biology.
The scanfwould need to be modified as follows
scanf("%d", &biology);
scanf("%d", &chemistry);
To understand this in detail read Why does scanf require &
You are passing incorrect arguments to scanf() calls. You must pass the address of the variables (see scanf()'s documentation) to match %d format.
scanf("%d", &biology);
...
scanf("%d", &chemistry);
You should also check the return code to see if the scanf() calls succeeded.

Error in code, What's wrong? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I've got an Problem. Whats the error in this code? It will give an Segmentation fault: 11
{
double os;
double windows = 2;
printf("Network info\n");
printf("Are you on Linux, OS X, or Windows? (1,2,3): ");
scanf("%s", os);
printf("checking..\n");
if (os == windows){
printf("Getting informatin for windows..\n");
system("ipconfig");
}else{
printf("Getting info for either osx or Linux..\n");
system("ifconfig");
}
}
In your code, you should change
scanf("%s", os);
to
scanf("%lf", &os);
as, os is of double type. Using wrong format specifier (or wrong argument type) invokes undefined behavior.
Read the man page of scanf() for further information.
FWIW, for an integer value, it's best to use int datatype.

How do you type something on the same line of text in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I was wondering how can you type something in one line in C?
This is the normal way it outputs.
Output
>
<text>
Instead
> <text>
From your tags, I'm guessing you're using printf(). Simply leave out the "\n" character, which means "newline.".
In other words.
printf("> "); printf("text\n");
This will print:
> text

Very simple program in C not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm relatively new to coding and am having a problem with a very small piece of code. It seems as if this should be simple to resolve, and I'm bothered that I can't figure it out myself. I was building a program to conduct a variety of conversions that I have to perform all the time and it was ouputting garbage. I backtracked and am testing all my functions. It seems that my functions weren't working, so I began testing each individual function as to whether or not it was correct.
I have one conversion here that I was running as a test code. It should take user int input and calculate ft from an input of miles. That seems pretty simple right? I thought so to.
Can someone please provide some insight as to why the very simple code below doesn't work?
#include <stdio.h>
int main(void)
{
float miles;
printf("Enter value in miles: ");
scanf(" %d", &miles);
printf("\n\n%.0d miles is equal to %.0d ft.", miles, ((miles)*5280));
return(0);
}
Use %f instead of %d in scanf function
You need just an integer. Change
float miles;
into
int miles;

Resources