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 5 years ago.
Improve this question
I want to enter value to my array and between 0 and 101 and trying to put zero or 101 will return an error but putting anything from 1-100 will allow me to enter a second number.
but i am stuck when write code to add a second number the loop keeps going in 1 and i cant figure out my error. any help would be apreciated
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
char letter;
int main()
{
int i;
int grades[12] ;
for ( i = 0; i < 12; i++)
do{//loops the following peice of code if values over 100 or less than 0 are entered
printf("Please Enter Grade 1:\n");
scanf("%f", & grades[i]);
if (grades > 100 || grades < 0)
printf("Invalid Entry please enter another between 0 and 100:\n");
}while(grades < 100 || grades > 0);
}
The mistake here is you're comparing the whole array to a value. What you want is one element, plus you need to test that you're outside of the acceptable bounds:
while(grades[i] > 100 || grades[i] < 0);
As Jonathan points out below, the logic in your code tests that it's within, which means only valid answers will loop forever, the opposite of what you wanted.
Related
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 2 months ago.
Improve this question
for (int i = 1; i < 22; i++){
if(i<=99 && i>=0){
printf("enter an age");
scanf("%d", &ages[i]);
}
else{
printf("enter a valid number");
}
}
When I enter a number outside the if statement such as 999, my program still accepts it instead of printing enter a valid number message. thanks in advance.
When I enter a number outside the if statement such as 999, my program still accepts it instead of printing enter a valid number message. thanks in advance.
I think as the 'i' is the index of the 'ages' array, you are allowing values with a range of 0-99. So, if the value is out oof this range also, it will still be stored in the 'ages' array. It won't display the warning message (enter a valid number) also. Instead, you can try something like this:
for (int i = 0; i < 22; i++){
printf("enter an age: ");
scanf("%d", &ages[i]);
if(ages[i] > 99 || ages[i] < 0){
printf("enter a valid number\n");
i--;
}
}
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 4 years ago.
Improve this question
Cheers guys. I am having trouble with this code I produced. It functions everything fine but when it prints the number when I guessed correctly, it prints a number like -3529583 which I don't understand. Shed some light?
#include <stdio.h>
#include <ctype.h>
#include <time.h>
int main()
{
int y, iRandomNum1; // Declare the three variables
y = 0;
iRandomNum1 = 0;
srand(time(NULL)); // Randomize function
iRandomNum1 = rand() % 10; // Randomize and collect 1 to 10 Value
while (iRandomNum1 != y) {
printf("Guess a number from 1 to 10\n");
scanf("%d", &y);
}
printf("\nCorrect Guess! Congrats! The answer is %d.\n", &y);
return 0;
}
You are display the address of the variable &y instead of the variable itself in your printf just remove the & symbol and it should be ok
https://stackoverflow.com/users/2173917/sourav-ghosh had the answer before me in comment
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 5 years ago.
Improve this question
I am beginner in c programming.I just want to know why this loop is not working properly.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
char x[8];
char t;
for (i = 0; i < 8; i++) {
scanf("%c", &t);
x[i] = t;
}
return 0;
}
Because when any input given from keyboard we need to press enter to confirm completion of input. This enter stay in buffer and if next input is char or string, stores enter in string or char var and do not wait to input that char or string. In this case, first input given at execution it stores char in X[0] and enter in x[1] and so on. So executes loop 8 time but it seems to 4 time because it ask input only four times. To check that put one printf in loop
it executes 8 times.
Whenever you press enter to submit, you are entering a whitespace character that is consuming one of your loop iterations.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 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 want to know why when running this code I get an infinite output. Here is the code:
#include <stdio.h>
int main(){
int num;
printf(" enter a number\n");
scanf(" %d", &num);
for( num = 0 ; num <= 10 ; num+=num){
printf(" %d",num);
}
return 0;
}
num+= num never incrementing the num. It is always adding 0 to 0. Also num = 0 in for loop overriding user input for num.
num+=num always adds 0 to 0(num) and hence the value of num never increments. Thus num always less than 10 and loop never exits.
while num++ would lead to num > 0, num += num with initial num = 0 what you get is num += 0, and the loop never reaches 10. Just use num++
int num;
printf(" enter a number\n");
scanf(" %d", &num);
for( num = 0 ; num <= 10 ; num++){
printf(" %d",num);
}
num+=num meaning is num=num+num and num start from 0 so num=0 always so go in infinite loop.change it num++.
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 7 years ago.
Improve this question
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! The input begins with the number t of test cases in a single line (t <= 10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n - m<=100000) separated by a space.
I don't know how to solve the problem with advanced concepts so I solved it by using just loops.
The time limit to this problem is 6.00s
#include <stdio.h>
int main(void)
{
int a[1],b[1],j,i,test,k,flag;
scanf("%d",&test);
for(i=1;i<=test;i++)
{
for(k=0;k<1;k++)
{
scanf("%d %d",&a[k],&b[k]);
}
for(j=a[0];j<=b[0];++j)
{
flag=0;
for(k=2;k<j;++k)
{
if(j%k==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("\n%d",j);
}
}
}
return 0;
}
Couple of suggestions that will improve performance.
You don't need to check all the way to b[0]. You need to check only up to sqrt(b[0]).
Update the loop so that you check only odd numbers not all numbers.
Replace
for(j=a[0];j<=b[0];++j)
{
by
int stop = sqrt(b[0]);
// Start with an odd number and keep incrementing j by 2 to keep it that way
for(j= (a[0]/2)*2+1; j <= stop; j +=2 )
{