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 5 years ago.
Improve this question
Not doing anything special. Just taking a user's input between 1 - 500, and then printing the number using for loop for each iteration. It crashes at the for loop. It does not print anything at all.
#include <stdio.h>
int forCounter() {
int num;
int count = 0;
printf("Pick a positive number (1 - 500): ");
scanf("%d", &num);
while (num < 1 || num > 500) {
printf("Out of range, try again (1 - 500): ");
scanf("%d", &num);
}
int i = num;
for (i; count <= i; count++) {
printf(count);
}
getchar();
return 0;
}
I take first input applied to 'num' and check if it's above or below allowed amount with the while loop.
When that's done it leaves and should start the for loop with i taking over for num. I tried using num in the place of i but it didn't work so I tried using a separate variable to see if it'd work.
I get two warnings seen in the image providedTwo warnings
You have to specify the format of output in printf.
int printf(const char *format, ...)
Your code:
#include <stdio.h>
int forCounter(void) {
int num;
printf("Pick a positive number (1 - 500): ");
scanf("%d", &num);
while (num < 1 || num > 500) {
printf("Out of range, try again (1 - 500): ");
scanf("%d", &num);
}
for (int i = 1; i <= num; i++) {
// printf(count); --> Bad
printf("Value = %d\n", i);
}
getchar(); // this will return immediately
return 0;
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
For example, if I want to call switch statement in for loop, I got an error.How can I correct this issue?If I want to move my switch statement outer part in determined condition which is to be using more than calling switch statement. Thanks in advance.
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#define SIZE 50
int i = 0;
int main() {
char usertxt[SIZE], myoperator[SIZE];
printf("addition='+',subtraction='-',multiplication='*',division='/'\n");
usertxt[0] = 0;
int x, myarray[SIZE];
printf("How many numbers should be entered? ");
scanf_s("%d", &x);
for (i = 0; i < x; i++) {
scanf_s(" %c", &myoperator[i], 1);
switch (myoperator[i]) {
case '+':printf("Addition operation\n");
printf(" Enter your number: ");
scanf_s("%d", &myarray[i]);
usertxt[i] = printf("%d%c", myarray[i], myoperator[i]);
break;
case '-':printf("Subtraction operation\n");
printf("Enter your numbers: ");
scanf_s("%d", &myarray[i]);
usertxt[i] = printf("%d%c", myarray[i], myoperator[i]);
break;
case '*':printf("Multiplication operation\n");
printf("Enter your numbers: ");
scanf_s("%d", &myarray[i]);
usertxt[i] = printf("%d%c", myarray[i], myoperator[i]);
break;
case '/':printf("Division operation\n");
printf("Enter your numbers: ");
scanf_s("%d", &myarray[i]);
usertxt[i] = printf("%d%c", myarray[i], myoperator[i]);
break;
default :if (myoperator[i] == '\0') {
break;
};
}
}
}
As #Gerhardh said, welcome to SO.
But I really think that the logic behind your code isn't good enough.
I take some time to understand your goal and I make a new clearer version.
While testing and understanding your code, I've encountered the following problems:
Implicit declaration of function 'scanf_s' is invalid in C99
Declarations of global variables as counters and their usage inside the loop were the main reason why your code doesn't work as expected.
The printf() function is used for printing the output. It returns the number of characters that are printed. If there is some error then it returns a negative value. You used it like it returns a char or even a string.
Switch statement is useless if you have to do always the same operations.
Char reading doesn't work correctly.
I'm gonna attach here the updated code. Hope that it could be helpful and that actually it has the same "goal" that your code has.
#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#define SIZE 500
#define nOperator 4
int main() {
char usertxt[SIZE], myoperator[nOperator];
printf("addition='+',subtraction='-',multiplication='*',division='/'\n");
usertxt[0] = 0;
int nOperations;
int x, myarray[SIZE];
printf("How many numbers should be entered? ");
scanf("%d", &x);
printf("How many operations would you like to try? ");
scanf("%d", &nOperations);
for (int i = 0; i < nOperations; i++) {
fseek(stdin, 0, SEEK_END);
printf("\n\nEnter operation #%d: ", i + 1);
myoperator[i] = getchar();
for (int j = 0; j < x; j++) {
printf("Enter number #%d: ", j + 1);
scanf("%d", &myarray[j]);
}
int k = 0;
for (int m = 0; m < x; m++) {
if (m == x - 1) {
usertxt[k] = myarray[m];
printf("%d", myarray[m]);
} else {
usertxt[k] = myarray[m];
usertxt[k + 1] = myoperator[i];
printf("%d%c", myarray[m], myoperator[i]);
}
k++;
}
printf("\n\n");
}
}
As you can see, I tried to stay as much as next to your code idea and implementation.
For instance, I've not deleted the initial pragma and define. Probably you are going to use them later.
I've used fseek(...) as suggested here:
How to clear input buffer in C?
in order to read correctly char and integers together.
First thing first, it reads the numbers that you want to use and the number of operations that you want to do. After that, it takes the numbers associated with a certain operation, and at the end, it prints the expression as you are doing in your own code.
If you want to add the result, you just need a little edit in the code to sum all the numbers in the array or counting them while reading from input.
Variable Counters Description:
i is for 'myoperator' array
k is for 'usertxt' array
m is for 'myarray' array
Next steps:
Add the result variable to display the result of the operation
Use dynamic memory
Note that:
fseek(...) works on some systems; if not, then it is not surprising as nothing guarantees that it will work when standard input is an interactive device (or a non-seekable device like a pipe or a socket or a FIFO, to name but a few other ways in which it can fail).
If you need that it has to be portable, then check the link that I've placed before.
Hope that it was helpful.
Cheers,
Denny
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 years ago.
Improve this question
I use the following code to count the amount of digits in a while loop, so "0" should be 1, "10" should be 2 etc. - however the code does not seem to work. Can you please help me?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x;
int division;
int counter=0;
printf("Enter a number : ");
scanf("%d",&x);
do
{
division=x/10;
counter++;
}
while(division!=0);
printf("This number contains : %d digits",counter);
return 0;
}
please change division=x/10; to x /= 10 and corresponding while condition. x is not changed your code, thus you get stucked in you while loop forever
This line:
division = x / 10;
Will be performed forever since the condition given in the while logic never becomes false.
If you do:
do {
x = x / 10;
counter++;
} while (x != 0);
It'll work.
Enhanced version of your code:
#include <stdio.h>
int main() {
int x;
int counter = 0;
printf("Enter a number : ");
// looping until a correct format is provided
while (scanf("%d", &x) == 0) {
printf("Incorrect values, enter again: ");
fseek(stdin, 0, SEEK_END);
}
do {
x = x / 10;
counter++;
} while (x != 0);
printf("This number contains : %d digits.", counter);
return 0;
}
The intention behind the "enhanced version" is to verify if the input is correctly given as formatted in coding (i.e. accepting an integer and nothing else) which isn't in your program.
Also, you don't need to include stdlib.h for your own code. That works without it too.
You'll then get the following sample output:
Enter a number : asdlfjal;sdk
Incorrect values, enter again: asdf sdf
Incorrect values, enter again: 33334
This number contains : 5 digits.
You are not changing the division value. This should work
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x;
int division;
int counter=0;
printf("Enter a number : ");
scanf("%d",&x);
do
{
x=x/10;
counter++;
}
while(x!=0);
printf("This number contains : %d digits",counter);
return 0;
}
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 4 years ago.
Improve this question
The program should have the following guidelines:
The numbers are read from the standard input.
The first number is the length of the sequence (n) followed by n numbers.(ie, if you put '54321' the length of the sequence is 5 numbers)
If n is 0 or negative, the program displays the message “Error_1” followed
by a new line on the standard input.
If the length is shorter than n, it displays “Error_2” followed by a new line and quits.
I'm finding point number 2 difficult
My code is:
#include <stdio.h>
int main() {
int i,j,k;
printf("Enter a Number:\n");
scanf("%d", &i);
if (i <= 0 ) {
printf("Error_1\n");
} else if(){
printF("Error_2\n")
}
}
#include <stdio.h>
int main() {
int i,j,k;
printf("Enter a Number:\n");
scanf("%d", &i);
if (i <= 0 ) {
printf("Error_1\n");
} else{
scanf("%d",&j);
k=0;
while(j>0)
{
k++;
j=j/10;
}
if(k<i)
printf("Error_2\n");
}
}
so what i did was, i found out the length of number entered and if the length does not match with provided length , it prints error2. i found out the length by continously dividing the number by 10 till it becomes 0.
Check this out. You can use log base 10 to compute the length of the sequence. Easier and clener.
To compile the code use -lm flag.See the following command:
gcc sampleFilename.c -lm
#include <stdio.h>
#include<math.h>
int main() {
int i,num, length;
printf("Enter a Number:\n");
scanf("%d", &i);
if (i <= 0 ) {
printf("Error_1\n");
}
else{
printf("Enter the number: ");
scanf("%d",&num);
length=(int) log10(num)+1;// compute the length of the number .. read about log10
if (length < i ) //length of the sequence is less than the number 'i'
printf("Error_2\n");
}
}
there is something you have to understand
your code:
"if (i <= 0 ) {
printf("Error_1\n");
} else if () {/* I'm talking about this*/
printF("Error_2\n")
}"
commend: /* its mean else if (its empty!!) you should write condition in the if, what you need to do its just use if and not else or else if
https://www.programiz.com/c-programming/c-if-else-statement*/
currect code below:
#include <stdio.h>
int main()
{
int num,cnt=0;
printf("Enter length: "\n);
scanf("%d", &num);
while(num > 10) //checking first number
{
cnt++;
num /= 10;
}
if(num <= 0)
{
printf("Error_01\n");
}
if(num == (count + 1)!)
{
printf("Error_02");
}
return 0;
}
Try this below code,
Hope this will work
#include <stdio.h>
int main()
{
int i,j,k;
printf("Enter any number: \n");
scanf("%d", &i);
k = 0;
if(i <= 0)
{
printf("Error_01\n");
}
printf("Enter value number: \n");
scanf("%d", &j);
while(j != 0) // check till first digit
{
k++;
j /= 10;
}
if(k != i)
{
printf("Error_02\n");
}
return 0;
}
by the way this was referred from
https://codeforwin.org/2016/10/c-program-to-count-number-of-digits-in-number.html
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 5 years ago.
Improve this question
So basicly I have this assignement in C, I have to input numbers until I enter 0, and after I enter 0 I have to print 1st and 2nd min number from all that numbers and I can't use arrays. I get that I have to use do-while loop for input but I can't figure out how to find two smallest from all of them. I think that thing can be done with if loops but don't know how to make it as I have only one variable to enter numbers into it (int a). And in input I have error when I enter 0 I'm able to enter one more number before program quits.
#include <stdio.h>
int main() {
int a;
do {
printf("Enter numbers: ");
scanf("%d\n", &a);
//what to do here
}while(a != 0);
You need to add 2 variables to hold the smallest values detected so far. Like
int smallest = INT_MAX;
int second_smallest = INT_MAX;
Then in the loop you need to test if the new input value is smaller than the values stored so far. Something like:
if (a <= smallest)
{
second_smallest = smallest;
smallest = a;
}
else if (a < second_smallest)
{
second_smallest = a;
}
You can use two variables to do what you need
#include <stdio.h>
#include <limits.h>
int main(void)
{
int a = INT_MAX;
int min_1 = INT_MAX;
int min_2 = INT_MAX;
int valid;
do
{
if (a < min_1)
{
min_2 = min_1;
min_1 = a;
}
else if (a < min_2)
{
min_2 = a;
}
printf("Enter numbers: ");
valid = scanf("%d", &a);
}
while ((a != 0) && (valid == 1));
if (valid == 1)
{
printf("Minimum numbers entered are: %d %d\n", min_1, min_2);
}
else
{
fprintf(stderr, "Error in data input\n");
}
}
So:
use limits.h defines to init min variables to the highest value for int type INT_MAX.
for each loop you must test if entered number is a minimum
you must check that user input is valid: check scanf return value.
remove \n in the format string of scanf.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've been asked to write a program that accepts a list of numbers until a non-numeric is encountered (up to 30 numbers), putting the numbers into an array, and keeping track of how many numbers were inserted. Then it should scan through the array to find the largest number, and print the largest.
This is what I've come up with:
#include<stdio.h>
int main()
{
const int INPUT = 30 ;
int size [INPUT];
int i, big;
printf("Type integer numbers, followed by q to quit: ");
while (scanf("%d", &size[INPUT]) != 'q')
{
for(i=0;i<size;i++)
scanf("%d",&INPUT[i]);
big = INPUT[0];
for(i=1;i<size;i++)
{
if(big<INPUT[i])
big=INPUT[i];
}
printf("The largest number is %d",big);
return 0;
}
Besides the problems, I listed in the comments. You seems to be comfused by the varaible names~ Anyway, I made some code for you.
#include<stdio.h>
int main()
{
const int MAX_INPUT = 30 ;
int input[MAX_INPUT];
int size=0, big;
printf("Type integer numbers, followed by q to quit: ");
while(size < MAX_INPUT){
if(scanf("%d", &input[size]) != 1){
break;
}
++size;
}
if(size ==0){
return 0;
}
big = input[size-1];
while( size-- > 0)
{
if(big<input[size]){
big=input[size];
}
}
printf("The largest number is %d\n",big);
return 0;
}
Tested with GCC 4.1.2 and Linux.
Return value of scanf:
Upon successful completion, these functions return the
number of successfully matched and assigned input items
further, you are mixing the size and input, you actually want the size to be a constant and input to be an array:
const int SIZE = 30 ;
int input[SIZE];
So the while loop should look like:
while (scanf("%d", &input[some_index]) == 1)
and of course this is wrong:
scanf("%d",&INPUT[i]); // should be ==> &input[i]