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
here is the code I wrote.
#include <stdio.h>
#include <stdlib.h>
typedef struct note {
int pitch;
int velocity;
int channel;
} note;
int printnote(int pitch, int velocity, int channel);
int main() {
int size = 100;
note note;
struct note *ptr = malloc(size * sizeof(int));
printf("Input the values for the `pitch`, `velocity`, and `channel`\n");
scanf("%d %d %d", ¬e.pitch, ¬e.velocity, ¬e.channel);
printnote(note.pitch, note.velocity, note.channel);
free(ptr);
return 0;
}
int printnote(pitch, velocity, channel) {
printf("The MIDI Note is:\n");
printf("Pitch -> %d\n", pitch);
printf("velocity -> %d\n", velocity);
printf("channel -> %d\n", channel);
return 0;
};
When I run the code and type the numbers, it shows the wrong answers.
For example, I run the code, and it shows
Input the values for the `pitch`, `velocity`, and `channel`
5 5 5
The MIDI Note is:
Pitch -> 5
velocity -> 0
channel -> -429762432
The three numbers should be the same as the input numbers.
Can anyone help me?
There is a subtle typo in your scanf() conversion string:
scanf("%d %d %d", ¬e.pitch, ¬e.velocity, ¬e.channel);
You used the unicode full width percent sign % (\uff05) instead of the ASCII % character. scanf does not recognise this as a conversion specifier and tries to match the byte sequence used to encode %, (0xEF 0xBC 0x85 in UTF-8) and fails thus only converting the first input into note.pitch and leaving note.velocity and note.channel uninitialized, returning 1. Note how % looks different from % in the fixed font used for code, but identical in the font used for this text: % % % % % %.
Just replace % with the correct character:
scanf("%d %d %d", ¬e.pitch, ¬e.velocity, ¬e.channel);
Also note these remarks:
size and ptr are not used in main(),
you should check the return value of scanf() to detect invalid input. This check would have helped find the error,
the prototype in the definition of printnote is incorrect: the argument types are missing,
the ; after the } is useless,
using the same identifier note for the variable and its type is confusing.
Here is modified version:
#include <stdio.h>
typedef struct note {
int pitch;
int velocity;
int channel;
} note;
void printnote(int pitch, int velocity, int channel);
int main() {
note note1;
printf("Input the values for the `pitch`, `velocity`, and `channel`\n");
if (scanf("%d %d %d", ¬e1.pitch, ¬e1.velocity, ¬e1.channel) != 3) {
printf("invalid input\n");
return 1;
}
printnote(note1.pitch, note1.velocity, note1.channel);
return 0;
}
void printnote(int pitch, int velocity, int channel) {
printf("The MIDI Note is:\n");
printf("pitch -> %d\n", pitch);
printf("velocity -> %d\n", velocity);
printf("channel -> %d\n", channel);
}
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 8 months ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
//odd number generator with int k as the max number
void genap_generator(){
int k;
printf("Masukkan batas bilangan genap = ");
scanf("%i", &k);
printf("Bilangan genap dari 0 sampai %i adalah :\n");
for (int i=0; i<=k;i+=2){
printf("%i\n", i);
}
}
int main(){
genap_generator();
system("pause");
}
I made a program to generate odd numbers with int k as the max number but when i print the integer it doesnt print kprint error
in the line where you want to print k: printf("Bilangan genap dari 0 sampai %i adalah :\n");, you didnt pass k.
The line should be: printf("Bilangan genap dari 0 sampai %i adalah :\n", k);.
What the function does print right now is what is placed on the stack where k should have been.
Also, you are printing all the even numbers, if you want to print the odd numbers start from i=1.
Welcome to our community! Could you use English language in your code next time?
The problem was with printing in printf, you did not declare which variable you wanted to print. Here is the working code:
#include <stdio.h>
#include <stdlib.h>
//odd number generator with int k as the max number
void genap_generator()
{
int k;
printf("Enter an even number limit = ");
scanf("%i", &k);
// Had to declare, which variable to print
printf("The even numbers from 0 to %i are:\n", k);
for (int i = 0; i <= k; i += 2)
{
printf("%i\n", i);
}
}
int main()
{
genap_generator();
system("pause");
}
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 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 6 years ago.
Improve this question
In my function when I set *area = 2 or to any other int, my program works as intended. For some reason I cannot calculate the length by the width and have it out put properly, all I get are 0's. What am I missing? Is it something in the call? Also is %f an appropriate conversion for double or is there something better to use? My code is below:
#include <stdio.h>
void area_perimeter(double width, double length, double *area, double *perimeter);
int main(){
double width, length, are, peri;
printf("Enter the width of the rectangle: ");
scanf("%f",&width);
printf("Enter the length of the rectangle: ");
scanf("%f",&length);
area_perimeter(width, length, &are, &peri);
printf("The area of the rectangle is: %f\n",are);
printf("The perimeter of the rectangle is: %f\n",peri);
return 0;
}
void area_perimeter(double width, double length, double *area, double *perimeter){
*area = length * width;
*perimeter = (*area * 2);
}
You invoked undefined behavior by passing a pointer to object having wrong type: %f in scanf()callls for float*, but you passed double*. Use %lf to read double.
Using %f in printf() to print double is good. C99 compiler will also accept %lf.
Try this:
#include <stdio.h>
void area_perimeter(double width, double length, double *area, double *perimeter);
int main(void){
double width, length, are, peri;
printf("Enter the width of the rectangle: ");
if (scanf("%lf",&width) != 1) {
fputs("read error width\n", stderr);
return 1;
}
printf("Enter the length of the rectangle: ");
if (scanf("%lf",&length) != 1) {
fputs("read error length\n", stderr);
return 1;
}
area_perimeter(width, length, &are, &peri);
printf("The area of the rectangle is: %f\n",are);
printf("The perimeter of the rectangle is: %f\n",peri);
return 0;
}
void area_perimeter(double width, double length, double *area, double *perimeter){
*area = length * width;
*perimeter = (*area * 2);
}
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 6 years ago.
Improve this question
#include <stdio.h>
#include <conio.h>
int main(void){
int n, i, a[10], sum = 0;
for(i = 0; i < 10; i++){
printf("Enter the marks of %dth student ", i + 1);
scanf("%d", a[i]);
sum = sum + a[i];
}
printf("The total sum is %d", sum);
return 0;
}
Is there an error in my program?
Everytime I run the program, after entering the marks for the first student, I get an error saying that my program has stopped working!
This happens for most of my programs where I have used arrays!
It should be
scanf("%d",&a[i]);
Pass-by-pointer, not by value. Unfortunately, some compilers cannot perform compile-time type safety checks on calls to scanf(). So basically scanf() is treating your (uninitialized value in) a[i] as a pointer, which leads to undefined behavior.
Try this:
#include <stdio.h> //stdio not Stdio
int main(void){
int n,i,a[10],sum=0;
for(i=0;i<10;i++){
printf("Enter the marks of %dth student ",i+1);
scanf("%d",&a[i]); // &a[i] not a[i]
sum=sum+a[i];
}
printf("The total sum is %d\n",sum);
return 0;
}
scanf needs a pointer, not the value.
You invoked undefined behavior by passing data having the wrong type to scanf(). You have to pass int* to scanf(), not int, for %d.
I also corrected the #includes and added input error check.
Try this:
#include<stdio.h>
int main(void){
int n,i,a[10],sum=0;
for(i=0;i<10;i++){
printf("Enter the marks of %dth student ",i+1);
if(scanf("%d",&a[i])!=1){
fputs("read error\n",stdout);
return 1;
}
sum=sum+a[i];
}
printf("The total sum is %d\n",sum);
return 0;
}
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
Hello I am trying to write C code that prompts the user for a positive integer and then prints out the number of digits in that number. Assuming that the user enters a positive integer, no error checking is needed.
My problem is after 'ctrl-d' is pressed the output is always:
"# of digits = 1"
If the user types 1023 the program should print:
"# of digits = 4"
I am new to C and am trying to get a feel for how scanf works. Thank you!
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int count = 0;
char n;
printf("Enter positive int!\n");
while(scanf("%c",&n) != EOF);
{
count++;
}
printf("# of digits = %d\n", count);
return 0;
}
You may use %d format specifier enter an decimal integer with scanf, then apply following function:
int getNumOfDigits(int num, int base /*= 10*/)
{
int count = 0;
if (num < 0) {
num = -num;
}
while (num > 0) {
count++;
num /= base;
}
return count;
}
It is untested, but general idea is to divide by ten, then you go with number of digits of an int number:
printf("# of digits = %d\n", getNumOfDigits(number, 10));
Note that you should also check returned value from scanf for error handling (when number is not in correct format). For instance:
if (scanf("%d", &number) != 1) {
printf("Incorrent input for decimal number!\n");
exit(EXIT_FAILURE);
}
You could simply read in a string and then use strlen() to check its length:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_LEN 64
int
main(int argc, char** argv)
{
char buf[MAX_LEN] = {0};
scanf("%s", buf);
fprintf(stdout, "string length: %lu\n", strlen(buf));
return EXIT_SUCCESS;
}
If you need to validate the input (i.e., to ensure it is a number), you could read in the string as above, and before printing the string length, loop over each character and use isdigit() to test if that character is an integer. If it isn't, then the input is either a negative number or some other non-numerical character.
Here is one way to find the number of digits by using log10()
#include <stdio.h>
#include <math.h>
int main (void)
{
unsigned number;
scanf("%u", &number);
printf("%.0f\n", ceil(log10((double)number+1)));
return 0;
}
And here is another way, sprintf() returns the number of characters written
#include <stdio.h>
#include <math.h>
int main (void)
{
unsigned number;
char str[20];
scanf("%u", &number);
printf("%d\n", sprintf(str,"%u", number));
return 0;
}