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);
Related
I'm new to learning C, and practicing some basic code.
I keep getting either a 0 or a very large random number for the bill multiplication at the end. I think I've narrowed it down to an issue with my "quant" value but I can't figure it out. Here's the first part of the programme (that only calculates for the pizza options so far):
#include <stdio.h>
int main()
{
int num, cho, quant, bill;
printf("Select a number from the menu:\n1. Pizza\n2. Burger\n3. Sandwiches\n4.
Beverages \n");
scanf("%d",&num);
if(num==1)
{
printf("Select a number for the food you want to order, and the quantity. \n");
printf("1. Pepperoni\t\tRs 1000 \n2. Fajita\t\tRs 1000\n3. Hot n spicy\t\tRs 1100\n4.
Chicken Tikka\tRs 1200\n");
scanf("%d,%d",&cho,&quant);
{
if(cho==1)
cho=1000;
else if(cho==2)
cho=1000;
else if(cho==3)
cho=1100;
else if(cho==4)
cho=1200;
}
bill=cho*quant;
printf("Please take your food item(s). Total bill = Rs %d \nThank you.",bill);
}
getchar();
getchar();
getchar(); getchar(); getchar();
return 0;
}
The way you have your scanf for cho and quant right now expects the user to enter two numbers separated by nothing but a comma, i.e. 2,10. If you do that format of input, your code works fine. However, if this format is broken the scanf does not properly read in a number to one or both of your variables, and they are left unitialized with potentially garbage information in them (hence the really large random numbers you're seeing).
You should probably change that line to
scanf("%d%d",&cho,&quant)
I am new to C, so excuse me for asking a seemingly easy question. but I have a loop which loops depending on the user, and inside the loops it asks for a name and an age, however when I go to print, it only prints the last entry and not all the entries I want.
#include <stdio.h>
int main()
{
int size,age;
char name [30];
printf("How long to loop for: ");
scanf("%d", &size);
for (int i=0; i<size; i++)
{
printf("Enter first name: ");
scanf("%s", name);
printf("Enter %s's age: ",name);
scanf("%d",&age);
printf("name: %s, age: %d\n", name,age);
}
return 0;
}
Right now your name and age variables will only hold the last input they received, thats why you're program only prints the last index. You need an array of int as well as an array of char [] to "hold" each input they receive, otherwise those inputs get lost and you only get input for the last index.
your variables should be initialized like:
int size;
printf("Enter size: ");
scanf("%d", &size);
int age [size];
char name[size][30];
in order to get user input and print each entry you will need to access each index by looping through the array size.
to access each index:
age[i]
name[i]
Please Note: using scanf to get a string from user input is generally bad, as it can cause problems if you happen to use a space(such as inputting your full name for example) in your input, and it can cause a buffer overflow if your string is longer than the buffer. Instead you can use fgets, but for your question it isn't necessary, just something to considered in the future in case you want to have spaces in your inputs.
Are you sure that the first scanf actually reads a number.
You want the following to check
if (scanf(" %d", &size) != 1) { // Note space - Eats white space/new lines
printf("Error - invalid size\nExiting\n");
return -1;
}
Prevent buffer overruns
Use:
scanf(" %29s", name); // See above
See point 1 for age
I think this will solve your problems
Okay so if I am understanding you correctly, you are looping through the for loop size number of times and each time you are setting the contents of the variables age and name. It sounds like you are expecting each iteration through the loop to save your entered instance of age and name but this is not the case.
Since you only have one age and one name variable they are going to store whatever the most recently taken input was. So this means whatever was entered on the last loop is what was last written into the variables name and age.
If you then try to print the contents of those variables you are going to see whatever was most recently entered into those variables. you would need more than one name and more than one age variable to write into if you were trying to save multiple entries.
Okay so Anthony, maybe you need to try a little bit of review when it comes to allocating memory. So when we declare the variable
int age;
we are telling the compiler to allocate enough memory to store one single integer that we will call 'age' in our program. since we only asked for enough memory to store one integer, every time you scanf() into the age variable, the single integer is written into that space.
What you want is to store a new integer every time you do another run through your for loop. So, you could declare an array of integers as follows.
int age[20];
Now we have asked the compiler to allocate enough space to store 20 different integers. there will an integer in position age[0] and another integer in age[1] and another in age[2] so on. So now you could use a loop to index through your array and store each entered age from stdin into a different position in the array as follows
for(int i=0; i<20; i++)
{
printf("Please enter your age: ");
scanf("%i", &age[i]);
}
So this loop starts with i=0 and so enters the input from stdin to age[0], the next iteration through the loop has i=1 and so enters the stdin input to age[1] and so on.
Hopefully this clarifies things a bit more?
A major fault is that your printfs will not be displayed as they are buffered.
You need a fflush after them
i.e.
printf("How long to loop for: ");
fflush(stdout);
And
printf("Enter first name: ");
fflush(stdout);
etc...
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.
So I have this mini project from my college where I have to make a simple billing system in C.
Now, starting with the code, here's how I am asking the data that is to be inputted.
printf("How many items do you have in your basket?:\n");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("Enter the name of the item:\n");
scanf("%s",&item_name[i]);
printf("Enter the MRP printed on the box:\n");
scanf("%f",&mrp[i]);
printf("Enter the quantity:\n");
scanf("%f",&qty[i]);
}
Now, suppose someone has 2 items in his basket and so, he'll enter two names. At the end, how do I print two names ??
for (i=0;i<n;i++)
{
printf("%s\n",item_name[i]);
printf("Quantity:%.2f\n",qty[i] );
printf("MRP:%.2f\n",mrp[i] );
}
The code printed above only prints the first letter of the string.
It prints properly, it's just your scanf which is wrong for char arrays. You don't have to pass the address of the pointer. As opposed to integer or doubles, passing the pointer is sufficient.
scanf("%s",&item_name[i]);
should be
scanf("%s",item_name[i]);
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.