#include <stdio.h>
struct member {
char name[20];
int age;
char sex;
int height;
};
int main(void)
{
int i,j;
struct member input[5]={0,};
int tot, rank, max=0;
int max2[5]={0,};
float res;
for (i=0; i<5; i++)
{
scanf("%s ", input[i].name);
scanf("%d %c %d", &input[i].age, &input[i].sex, &input[i].height);
getchar();
}
scanf("%d", &rank);
max =input[0].height;
for(i=1;i<=5;i++) {
for(j=0;j<5;j++) {
if(max>=input[j].height)
max=max;
else
max=input[j].height;
}
max2[i]=max;
for(j=0;j<5;j++) {
if(max==input[j].height)
input[j].height*=(-1);
}
max=-1;
}
for (i=0; i<5; i++)
{
if(max2[rank]== input[i].height)
printf("%s %d %c %d\n",input[i].name, input[i].age, input[i].sex,input[i].height);
}
fflush(stdin);
getchar();
return 0;
}
The result printed of above my code is nothing...Even the height inputted becomes negative number..
What's wrong with this program?
Input & Print should be the same like example image.......
please help!
The first scanf should be fixed
scanf("%s ", input[i].name);
In the second scanf, the input[i].sex is a char type so you have to use "%c" instead of %s
scanf("%d %s %d", &input[i].age, &input[i].sex, &input[i].height);
should be
scanf("%d %c %d", &input[i].age, &input[i].sex, &input[i].height);
You have a lot of problems here:
1) You're taking the wrong parameters from scanf():
scanf("%s ", input[i].name); // You don't need a & for a string
scanf("%d %c %d", &input[i].age, &input[i].sex, &input[i].height); // you need %c for a
// character
2) You're using for(i=1;i<=5;i++) to access an array of 5 elements, by doing this you're overflowing the array (it should be 0 to 4, not 1 to 5)
3) This is a nit-pick but:
if(max>=input[j].height)
max=max;
that is totally pointless. You don't need to set a variable to itself, just invert the logic (<) and only do the else case.
4) You set all the input[x].height's to the negative of the value they're originally set to here:
input[j].height*=(-1);
Then you check to see if that is the same as the original values which you stored in max2[] before printing here:
if(max2[rank]== input[i].height)
printf("%s %d %s %d\n",input[i].name, input[i].age, input[i].sex,input[i].height);
Well, that's never going to happen, so you'll never print anything
5) fflush(stdin); is not a well defined operation on most systems and could lead to undefined behavior, so don't do it.
Related
#include<stdio.h>
int main(){
char name[3];
float price[3];
int i,page[3];
printf("enter the name price and book\n");
for(i=0;i<3;i++){
scanf("%s",name[i]);
printf("enter character:\n");
}
for(i=0;i<3;i++) {
scanf("%f",&price[i]);
printf("enter floating point number:\n");
}
for(i=0;i<3;i++) {
scanf("%d",&page[i]);
printf("enter digit:\n");
}
printf("\n");
for(i=0;i<3;i++) {
printf("%s\n",name[i]);
}
for(i=0;i<3;i++) {
printf("%f\n",&price[3]);
}
for(i=0;i<3;i++){
printf("%d\n",&page[i]);
}
return 0;
}
When I was trying this code, I thought this was quite simple to do but I realized, there is something which I am missing in the code. The main problem with this code is it is not scanning the values of price and pages. I don't understand where I am mistaken.
So, please correct my code so that it will print the values of name price and pages.
There are a number of issues in your code. First and foremost, the %s specifier (for both the scanf and printf functions) expects a string argument (that is, a nul-terminated array of char for printf or an array sufficiently large to hold the input characters plus that terminator, for scanf); however, you are attempting to read (and print) a single char value in each of the relevant for loops.
To fix this, use the %c format specifier, instead of %s. However, when you use this, the newline character that is generated when you press the Enter key will be left in the input buffer, and that will be read as the actual char input on the next iteration of the first for loop. To clear any such newline (or, indeed other whitespace) from the input before the real input, add a space in the format string before the %c. Also, when using this, you will need to pass the address of each name element: scanf(" %c", &name[i]);.
Further, the printf function takes the actual values of the variables to be output, rather than their addresses – so remove the & from the arguments in your printf calls. (Also, and I assume it's a typo, the price[3] expression should be price[i] – the former attempts to access an out-of-bounds element of the price array.)
Another issue is that, in each of your input loops, you call the scanf function before you display the relevant prompt. In the code below, I have reversed your printf and scanf lines in each of those input loops.
Here's a possible fixed version:
#include<stdio.h>
int main()
{
char name[3];
float price[3];
int i, page[3];
printf("enter the name price and book\n");
for (i = 0; i < 3; i++) {
printf("enter character:\n");
scanf(" %c", &name[i]);
}
for (i = 0; i < 3; i++) {
printf("enter floating point number:\n");
scanf("%f", &price[i]);
}
for (i = 0; i < 3; i++) {
printf("enter digit:\n");
scanf("%d", &page[i]);
}
printf("\n");
for (i = 0; i < 3; i++) {
printf("%c\n", name[i]);
}
for (i = 0; i < 3; i++) {
printf("%f\n", price[i]);
}
for (i = 0; i < 3; i++) {
printf("%d\n", page[i]);
}
return 0;
}
That's the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int linhas=0, col=0, num=0, i=0, pos1[100];
int pos[100];
scanf("%d %d %d", &linhas, &col, &num);
int matriz[linhas][col];
for(i=0; i<num;i++){
scanf(" %c%d", &pos[i], &pos1[i]);
}
for(i=0;i<num;i++){
pos[i] -= 97;
}
return 0;
}
It's quite simple, I declared 2 arrays, one to store the value of a char(pos[]), and the other to store integer values(pos1[]), and it works:D.
The thing is, if I declare a matrix ex: matrix[linhas][col], my code does not really store the values of a char, and if I take it off, it starts to store normally, also, it does not matter whether if I declare the matrix right after getting the rows and colums (linhas and col) or if I declare it at the end of the code. I don't know what the problem is, and I'd appreciate any hints.
int pos[100];
scanf("%d %d %d", &linhas, &col, &num);
int matriz[linhas][col];
for(i=0; i<num;i++){
scanf(" %c%d", &pos[i], &pos1[i]);
}
The %c format specifier will read in a character, but it requires the address of a character to read it into. You pass it the address of an int.
The simplest fix is to change pos to char pos[100];. Another possible fix is this:
for(i=0; i<num;i++){
char c;
scanf(" %c%d", &c, &pos1[i]);
pos[i] = c;
}
I want to make simple billing software but I don't know to fix this problem
#include <stdio.h>
int main()
{
char a[20];
int i, j, b;
i = 0;
printf("How many item you have?\n>>> ");
scanf("%d", &j);
for (int i = 0; i < j; i++)
{
printf("Type the name of item no. %d?\n>>> ", i + 1);
scanf("%c", &a);
printf("Type the item quantity?\n>>> ");
scanf("%d", &b);
}
return 0;
}
This code is only for asking questions, as you can see. In this code everything is fine but, when I run this code, the output is:
How many item you have?
>>> 4
Type the name of item no. 1?
>>> Type the item quantity?
>>>
Everything seems fine but I haven't entered the item name and the loop is asking the 2nd question directly. How is it even possible?
The %c format specifier for scanf reads a single character. To read a string (array) of characters, use the %s format specifier. Also, for such arrays, you don't need the & (address of) operator, as the array name itself will 'decay' to a pointer to its first element:
#include <stdio.h>
int main()
{
char a[20];
int i, j, b;
i = 0;
printf("How many item you have?\n>>> ");
scanf("%d", &j);
for (int i = 0; i < j; i++) {
printf("Type the name of item no. %d?\n>>> ", i + 1);
scanf("%19s", a); // The "19" limits input size and allows space for the nul-terminator
printf("Type the item quantity?\n>>> ");
scanf("%d", &b);
}
return 0;
}
Im doing some work for Uni and wrote a programm which stores Integers in an char Array and converts them to their ASCII Values and prints them at the end. My code did not work before and only started working when i changed "%c" to "%i" in my scanf line. My question: Why does it have to be "%i" when i wanna store those Numbers in an char Array and not an Int Array. Thanks!
My code:
#include <stdio.h>
int main()
{
int i; /counter
char numbers[12];
printf("Please enter 12 Numbers\n");
for(i = 0; i < 12; i++){
printf("please enter the %i. Number\n", i+1);
scanf("%i", &numbers[i]);// <-- changed "%c" to "%i" and it worked.why?
}
for(i = 0; i < 12;i++){
printf("The %i.ASCII value is %i and has the Char %c\n", i+1, numbers[i], numbers[i]);
}
return 0;
}
%c is for reading a single character. So for example if you type in "123", scanf will read '1' into the char variable and leaves the rest in the buffer.
On the other side %i is the specifier for int and will therefore lead to undefined behavior when trying to read in a char.
I think what you are looking for is the %hhi specifier, which reads a number into a char variable.
#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
int mult;
int n;
int ans;
ans = mult * i;
printf("Please enter a multiple you want to explore.");
scanf("%d", &mult);
printf("Please enter the number which you would want to multiply this number till.");
scanf("%d", &n);
for(i = 0; i<n; i++) {
printf("%d x %d = %d \n", &mult, &i , &ans);
}
return 0;
}
Hi guys, this is a simple code which is supposed to help the user to list the times table for n times. However, i am receiving undefined behaviour and I am quite stumped as to what is wrong with my implementation of my "for" loop.
I am receiving this as my output.
6356744 x 6356748 = 6356736
for n times in my consoles.
I want to ask
Is anything wrong with the logic of my code? (i assume i do have a problem with my code so please do enlighten me)
Would it be better(or even possible) to use pointers to point to the memory addresses of the mentioned variables when i have to change the value of the variables constantly? If yes, how do i go around doing it?
Thanks!
In printf you must provide integers. You are now giving the addresses of integers. So change
printf("%d x %d = %d \n", &mult, &i , &ans);
to
printf("%d x %d = %d \n", mult, i, ans);
and to make the table, replace ans with just mult*i, so:
printf("%d x %d = %d \n", mult, i, mult*i);
You should also check the return value of scanf to check if it has succeeded reading your input:
do {
printf("Please enter a multiple you want to explore.");
} while (scanf("%d", &mult)!=1);
do {
printf("Please enter the number which you would want to multiply this number till.");
} while (scanf("%d", &n)!=1);
The things you see are the values of the variables memory location.
Change your lines inside for loop as below
ans = mult * i;
printf("%d x %d = %d \n", mult, i, ans);
There are some mistakes in your code .
you are using the & operator in print statement which is used to print the address of the variable.
Initiate the loop with the value '1' instead of '0' & execute the loop till 'i' less than equal to 'n'.
instead of using the ans variable outside the loop , use it inside the loop as it evaluate the multiplication result in each iteration of the loop.
#include <stdio.h>
int main()
{
int i;
int mult;
int n;
int ans;
printf("Please enter a multiple you want to explore.");
scanf("%d", &mult);
printf("Please enter the number which you would want to multiply this number till.");
scanf("%d", &n);
for(i = 1; i<=n; i++) {
ans = mult*i ;
printf("%d x %d = %d \n", mult, i , ans);
}
return 0;
}