printing a struct prints out random paths [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 2 years ago.
Improve this question
I'm trying to read a text file with details into a struct other when I try to print out the output of a struct, i just get met with random numbers or a
.
Can I please get some help where i am going wrong?
Tthe text file is set up as
4, 2, 10000
typedef struct
{
int bed;
int bath;
int price;
}house;
int main()
{
FILE* f;
linkedList* list = createLinkedList();
house sydney[4];
int total;
int ii;
f = fopen("house", "r");
fscanf(f, "%d", &total);
for (ii = 0; ii < total; ii++)
{
fscanf(f, "%d%d%d", &house[ii].bed, &house[ii].bath, &house[ii].price);
printf("Price: %d", house[ii].price);
}

Try this:
fscanf(f, "%d, %d, %d", &house[ii].bed, &house[ii].bath, &house[ii].price);
Also I would suggest to check the return value of fscanf to make sure all the parameters were found.

Related

integer print wrong number [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 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");
}

Scanf the numbers into function in C [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 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", &note.pitch, &note.velocity, &note.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", &note.pitch, &note.velocity, &note.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", &note.pitch, &note.velocity, &note.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", &note1.pitch, &note1.velocity, &note1.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);
}

how can i copy string to another variable and output with if else statement [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 2 years ago.
Improve this question
#include<stdio.h>
#include<string.h>
int main(void)
{
//Assigning string to name1//
char name1[8] = "Aaeric";
//will be used to copy name1 to copyname//
char copyname[8];
//Assigning string to name2//
char name2[7] = "Mudus";
//prompt for input//
int i;
printf("Choose 1 or 0: ");
scanf("%i \n", &i);
}
//for copying string of name1 to copyname//
if (i = 0)
{
strcpy(copyname, name1);
puts(copyname);
return 0;
}
else
puts(name2);
}
I want to copy the string to another variable and output with if-else condition through this way only but I am getting error.
The output is the expected identifier or ‘(’ before ‘if’
You probably want this:
#include <stdio.h>
#include <string.h>
int main(void)
{
char name1[8] = "Aaeric";
//will be used to copy name1 to copyname//
char copyname[8];
char name2[7] = "Mudus";
int i;
printf("Choose 1 or 0: ");
scanf("%i", &i);
if (i == 0) // <<<<< use == instead of =
{
strcpy(copyname, name1);
puts(copyname);
return 0;
}
else
puts(name2);
}
poinless comments removed
code snippets moved into main
code formatted properly
scanf("%i \n", &i); replaced with scanf("%i", &i);
if (i = 0) replaced with if (i == 0)

Do not understand memory surfing in C [closed]

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 4 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
#include <array.h>
int col, str;
int *point;
void setArr()
{
printf("Input columns="); scanf("%d", &col);
printf("Input strings="); scanf("%d", &str);
int num[str][col];
for(int i = 0; i < str; ++i)
{
for(int j = 0; j < col; ++j)
{
scanf("%d", &num[i][j];
}
}
point = num;
}
int main(void)
{
setArr();
printf("First=%d\n", *point);
printf("Number=%d", *point);
}
Output:
Input columns=2
Input strings=2
1
2
3
4
First=1
Number=1740639104
Here we have code in C, that have to get exact number from array using pointer, but during many attempts I understand that there is something I do not understand or just do not know.So there is a problem(or it have to be like this), namely, I refer to pointer ,which points on first element two times and I get different results in each case. Why it happened and in which way I could solve it? Thanks,everyone.
With
point = num;
you are setting point to an address of a function local variable. All further access of that will be undefined behaviour.

Prime numbers for a given range [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
#include<stdio.h>
int main()
{
int i, j, a, b, c=0;
scanf("%d %d", &a, &b);
for(i=a; i<=b; i++)
{
for(j=1; j<=i; j++)
{
if(i%j==0)
{
c++;
}
}
if(c==2)
{
printf("%d\n", i);
}
}
return 0;
}
The program however does not print prime numbers for a given range. Please help.
You have to reset c to 0 after every iteration. The loops should look like this
for(i=a; i<=b; i++)
{
c = 0;
...
An advice, you don't have to go till the number everytime to check for primality, you can go till square root of that number.

Resources