While executing this, loop is directly jumping to the end and giving me below output.
1.Generate Bill
2.Generate Bills of last 10 Purchase
Select Option:1
Enter Number of Groceries:3
Enter GroceryName:XYZ <Hit Enter>
Enter GroceryName:
Enter GroceryName:
Could you please suggest me better way of doing it in c ?
I have already tried certain links on stack overflow on this, however it doesn't seem to be working for me.
void main()
{
int iPrice[1000];
char cGroceName[100][100];
int iOption;
int iGrocNum;
printf("\n1.Generate Bill");
printf("\n2.Generate Bills of last 10 Purchase");
printf("\nSelect Option:");
scanf_s("%d", &iOption);
switch (iOption)
{
case 1:
printf("\nEnter Number of Groceries:");
scanf_s("%d",&iGrocNum);
for (int i = 0; i <iGrocNum; i++)
{
printf("\nEnter GroceryName:");
scanf_s(" %s",cGroceName[i]);
}
scanf_s(" %s",cGroceName[i]);
should be
scanf_s("%99s", cGroceName[i], sizeof cGroceName[i]);
as scanf_s requires a third argument for certain format specifiers such as %s denoting the maximum size of its corresponding argument.
See scanf_s documentation.
Related
Code:
#include <stdio.h>
void main()
{
int s1,s2,s3,s4,s5,sum;
float per;
printf("Enter subject 1 marks out of 100 \n");
scanf("%d",s1);
printf("Enter subject 2 marks out of 100\n");
scanf("%d",s2);
printf("Enter subject 3 marks out of 100 \n");
scanf("%d",s3);
printf("Enter subject 4 marks out of 100\n");
scanf("%d",s4);
printf("Enter subject 5 marks out of 100\n");
scanf("%d",s5);
sum=s1+s2+s3+s4+s5;
per=sum/100;
if (per>60 && per<70){
printf("your percentage is %d and you get 10% schoolarship",per)
;}
else if (per>70.1 && per<90){
printf("your percentage is %d and you get 20% schoolarship",per)
;}
else {
printf("your percentage is %d and you get 30% schoolarship",per)
;}
}
Output:
I am trying to make a percentage calculator and it shows a weird output.
What am I doing wrong?
When you call scanf, it is important to pass in the address of the variable you want to store. Otherwise, scanf will not behave as you expect.
Right now, you are not passing in the address to scanf; but rather the variable itself.
So you should do something like:
scanf("%d",&s1);
instead.
https://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm
I recommend reading a little bit about how scanf works at the following link.
"Following is the declaration for scanf() function.
int scanf(const char *format, ...)"
Additionally, check out this link for a few examples of scanf:
https://www.programiz.com/c-programming/c-input-output
scanf("%d", &testInteger);
The syntax is format first, then pass in the address of where you want to store the data.
scanf() requires a pointer to the value, it should be scanf("%d",&s1);
I am attempting to build a form/gpa calculator for class, but when entering the amount of classes using scanf, the output is 6,487,576 regardless of what I enter.
int main()
{
int opt;
int c;
printf("*******************************\n");
printf("** Fanshawe Grade Calculator **\n");
printf("*******************************\n");
printf("Please Choose an Option:\n");
printf("[1] Enter Your Marks\n");
printf("[2] Quit\n");
scanf("%d", &opt);
switch(opt) {
case 1 :
printf("********************************************************\n");
printf("** Enter Your Marks For Your Courses (Up to 10 Only): **\n");
printf("********************************************************\n");
scanf("%i", &c);
printf("You Have Entered %i Classes!\n", &c);
/*int i;
for(i=1;i=c;i++) {
printf("Enter Your Mark for Class #%i\n", &i);
}*/
break;
case 2 :
printf("GoodBye!");
exit(0);
break;
}
return 0;
}
Help please!
When you use %i with scanf(), it can allow you to input hexadecimal and octal numbers as well (this isn't an issue as explained here).
However, you shouldn't use & while using printf() as it'll display the memory location of the variable instead of the value stored in it.
Try this:
case 1 :
printf("********************************************************\n");
printf("** Enter Your Marks For Your Courses (Up to 10 Only): **\n");
printf("********************************************************\n");
scanf("%d", &c);
printf("You Have Entered %d Classes!\n", c);
break;
printf("You Have Entered %i Classes!\n", &c) outputs the address of the variable c (&c takes the address), i.e. it outputs the number of the memory cell. Since you want to pass the value of the variable you should not use the operator of taking address of a variable &.
printf("You Have Entered %i Classes!\n", c);
There are two problems here. First, you need to include the following headers:
#include <stdio.h> // declares functions like printf and scanf
#include <stdlib.h> // declares functions like exit
If you don't include these headers, then the compiler does not know how to execute them or their exact formats. When you attempt to compile, the warnings will not be helpful. There are a few good websites for this; just search something like "c exit()" in Google and it will tell you which headers you need in the future. When I compiled this code, I used gcc main.c -Wall which forces all warnings to be displayed.
Secondly, you use the address of c in both cases. When you go to print the value of c, you are actually printing the address of c, not the value. scanf() takes the address(es) of the variables to store values in, but printf() takes the variables themselves. The code should look more like this:
scanf("%i", &c);
printf("You Have Entered %i Classes!\n", c);
It is also always a good idea to initialize your variables. This way, you know when something is assigned improperly, or not assigned at all. I forgot to assign my variables for a while, and when compiled on another machine, it didn't work. This is because different machines will initialize variables differently. Just change the code to the following:
int opt = 0;
int c = 0;
Ok, so i'm having a bit of trouble with managing my arrays / for loops (pretty new to C). I need to ask the user how many types of paint they want to enter, then take data three times for each paint, and output all the data at the end.
I seem to be ok with taking all the data from the user, it's mainly outputting all the data at the end that i'm struggling with. I'm not looking for a quick solution to this specific problem, as I want to learn how the arrays / for loops work when outputting data (if that makes any sense).
#include <stdio.h>
int main(void)
{
int amount, count;
int result1, result2, result3;
char paintname;
printf("Please enter how many paints you want to compare:\n");
scanf("%d", &amount);
for (count = 1; count <= amount; count++)
{
printf("Please enter the name of paint number %d:\n", count);
scanf("%s", &paintname);
printf("Please enter the first result of paint number %d:\n", count);
scanf("%d", &result1);
printf("Please enter the second result of paint number %d:\n", count);
scanf("%d", &result2);
printf("Please enter the third result of paint number %d:\n", count);
scanf("%d", &result3);
}
return 0;
}
If you're looking for how to store all the results, you should use an array for every result (and name) that is large enough to hold all the user inputs. This array has a size that is dynamic (that is, it is decided at run-time when the user inputs it), so it should be allocated dynamically by the use of malloc()/calloc() and then free()'d later on.
You have paintname declared as a char. That means it only holds one character. You need it hold multiple characters, i.e. a char array:
char paintname[50];
...
scanf("%s", paintname);
I want to make a program which take Roll No and Full name as input and simply display it
My code is . this code skip scaning value of n through gets function. Why this error occur and how to over come this?
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
char n[30];
printf("enter your roll no");
scanf("%d",&r);
printf("enter your full name");
gets(n);
printf("roll no is %d ",r);
printf("name is %s ",n);
getch();
}
while the below code scan the first gets value and skips the second one.
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
char n[30], f[30];
printf("enter your roll no");
scanf("%d",&r);
printf("enter your full name");
gets(n);
printf("enter your full name of your father ");
gets(f);
printf("roll no is %d ",r);
printf("name is %s ",n);
printf("father name is %s ",f);
getch();
}
The code DOES NOT skip scanning the value of 'n'.
I believe that when you run the program, you enter the Roll No and then press the ENTER key on your keyboard.
This is the cause.
As soon as you press the ENTER key, the escape sequence '\n' is saved in the array n. Your gets() command is executing perfectly.
In the second case, the variable 'n' stores the escape sequence and the next variable 'f' takes the string you enter next.
To make your code work just enter your scanf statement like this:-
scanf("%d ",&r);
Notice the space after %d.
Try this code-
#include<stdio.h>
int main(void)
{
int r;
char n[30], f[30];
printf("Enter your roll no");
scanf("%d ",&r); // I have inserted a space after %d
printf("Enter your full name");
gets(n);
printf("Enter your full name of your father ");
gets(f);
printf("\nRoll no is %d ",r);
printf("\nName is %s ",n);
printf("\nFather name is %s ",f);
return 0;
}
TIP:- You must try not to use gets() and puts()
You can read more about it here.
The simple solution for the problem is to add fflush(stdin); between scanf(); and gets();
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
char n[30],fn[30];
clrscr();
printf("\nEnter roll ");
scanf("%d",&r);
fflush(stdin);
printf("\nEnter name ");
gets(n);
printf("\nEnter father name ");
gets(fn);
printf("\n\nRoll %d",r);
printf("\nname %s",n);
printf("\nfather name %s",fn);
getch();
}
Using scanf instead of gets will solve your problem:
scanf("%s", n); // Read in your name
Please note that when reading in any string like this you should use safe functions that are passed the length of the string (for example scanf_s from MSDN).
I don't know why it gets skipped but what you could do to avoid any other confusion like fflush(stdin) or fgets etc etc.
Just use gets(string) on the next line. So when it skips the first gets command it goes onto the other one.
Try that
Cheers,
;)
I just had the same problem two hours ago, but to solve this situation easily, all you have to fo is to add a "getchar()" after the "scanf()" and before the "gets()", so that the extra "\n" goes to the "getchar()" and you can type as you want in the next "gets()".
I was also facing the same problem as mentioned above.. so with the help of the answers mentioned here and using hit and trial method, I found that when we press enter after giving input to any variable using scanf(), \n is stored in the next gets() function.. and next time it doesn't take any input from the keyboard.. so to avoid this just use getchar() in between the scanf() nd gets() nd also remember that getchar() takes only 1 character.. so don't give any extra input to scanf() as again this will be stored and will be used in gets() nd the problem will remain the same....
hope this will help..
thank u..
I hope this isn't too basic of a question for stack overflow. But I have a query which is trying to determine the amount of grades in an array, and then ask for user input of each of those grades. It looks like this:
#include <stdio.h>
int main (void)
{
int size;
printf ("Enter The Amount Of Grades In Your Array: ");
scanf("%i", &size);/*Stores Amount Of Grades In The Array*/
char myGrades[size];
int i;
for (i = 0; i < size; ++i)
{
printf ("Enter the grade:");
scanf ("%c",&myGrades[i]);
}
return 0;
}
I expect the first line after int i to read "Enter The Grade:" but instead it looks like "Enter The Grade:""Enter The Grade:"
I don't understand why it says enter the grade the second time without first asking for my input on the first "enter the grade". Any suggestions would be much appreciated!
Your first scanf is leaving the \n behind, and then automatically reading it again the next time as if you'd pressed enter (so the newline is stored in your array). You can get around this by using " %c" instead. The space will get rid of any newlines or spaces before the character you want.