Printing Characters with my market program (C) - c

I wrote a market program that can sum up the price of the items you bought. Now I want to also print at the end the item that got selected and the quantity. How can I do that?
#include <stdio.h>
int main() {
int c,l=0,b=0,q=0;
float sum=0;
printf("Welcome to MH-Mart\n");
while (l<1) {
printf("1.Bread\n");
printf("Select a Category(If your done enter 11):\n");
scanf("%d",&c);
if(c==11){
printf("YOUR TOTAL SUM=%f\n",sum);
return 0;
break;
}
if (c==1) {
printf("Bread:\n1.Pita 1=1₪״\n2.Baguette 1=2.5₪
\n3.WhiteBread1=1.5₪\nPlease select what you want:\n");
scanf("%d",&b);
printf("How many you want:\n");
scanf("%d",&q);
if(b==1){
sum=sum+(q*1);
}
if(b==2){
sum=sum+(q*2.5);
}
if(b==3){
sum=sum+(q*1.5);
}
}
}
}

Related

How do i print out the names of the items inputted?

i'm new to programming so i'm having a hard time figuring out how to output the names of the items inputted in my loop, all other calculations are executing but i keep getting unknown values when the names of the item is outputted.
#include <stdio.h>
int main()
{
int count,num,a;
char *item [15][100];
float price,net_total, tax, total_pay,grandT;
do{
printf("Please enter the name of the item:");scanf("%s",&item[a]);
printf("\n");
printf("Please enter the price of the item: $");scanf("%f",&price);
net_total+=price;
tax=0.14*net_total;
grandT=net_total+tax;
printf("\nSelect '1' to continue or '0' to exit:\n");
scanf("%d",&num);
count++;
}
while (num!=0);
system ("cls");
printf("\tSummary Information of Items purchsed\n");
printf("Name(s)of the item(s) purchased: \t%s\n ",item[a]);
printf("Net total:\t$%.2f\n",net_total);
printf("Total tax:\t$%.2f\n",tax);
printf("Total payable:\t$%.2f\n ",grandT);
}`
1/ You should indent your code so it becomes easier to read.
2/ You didn't initialize a, count and net_total. (a is an index from 0 to 15).
3/ char item [15][100]
#include <stdio.h>
int main()
{
int count=0,num,a=0;
char item [15][100];
float price,net_total=0, tax, total_pay,grandT;
do
{
printf("Please enter the name of the item:");
scanf("%s",&item[a]);
printf("\n");
printf("Please enter the price of the item: $");
scanf("%f",&price);
net_total+=price;
tax=0.14*net_total;
grandT=net_total+tax;
printf("\nSelect '1' to continue or '0' to exit:\n");
scanf("%d",&num);
count++;
}
while (num!=0);
system ("cls");
printf("\tSummary Information of Items purchsed\n");
printf("Name(s)of the item(s) purchased: \t%s\n ",item[a]);
printf("Net total:\t$%.2f\n",net_total);
printf("Total tax:\t$%.2f\n",tax);
printf("Total payable:\t$%.2f\n ",grandT);
}

How to display all user input numbers in C?

I am required to create a function that uses arrays and a menu system that displays:
the sum of all numbers entered, the average of numbers entered, and all numbers entered. It will allow the user to enter up to 1000 numbers.
I have the majority of the code working, I just need to figure out how to display all of the numbers a user has entered so far. Would anyone be able to help me with this? Thanks!
I've tried displaying the number entered, but this does not fit the assignment requirements.
Here is the code I have so far:
/*
Title: Array Intro
Author: James Henderson
Desc: a program designed to display the sume, average, and all previous numbers entered of user input numbers
Date: 11/06/19
*/
#include <stdio.h>
#include <math.h>
//Create Variables
//used for math
int counter = 0;
float number, sum = 0.0, average;
//user input number
int userInt;
int userInput[1000];
//Void Function
static void sumFunction(userInput)
{
printf("\n\tWelcome!\n");
printf("Enter 1 to begin:\n");
scanf("%i", &userInput);
//switch statement
while (1)
{
switch (userInput)
{
case 1:
printf("\nEnter a number:\n");
while (1)
{
scanf("%i", &userInput);
//determine sum
number = userInput;
sum += number;
counter++;
average = sum / counter;
printf("\n The average of the numbers is %.2f", average);
printf("\n The sum of the numbers is %.2lf", sum);
printf("\n You may enter up to 1000 numbers");
printf("\n You have entered %d numbers\n", counter);
if (counter == 1000)
{
printf("\nThank you for using my program! Have a lovely day :)");
return;
}
}
}
}
}
Your approach was right, just look how i used userInput. Below code works fine:
#include <stdio.h>
#include <math.h>
//Create Variables
//used for math
int counter = 0;
float number, sum = 0.0, average;
//user input number
int userInt;
int userInput[1000];
//Void Function
static void sumFunction()
{
printf("\n\tWelcome!\n");
printf("Enter 1 to begin:\n");
scanf("%i", &userInt);
//switch statement
while (1)
{
switch (userInt)
{
case 1:
printf("\nEnter a number:\n");
while (1)
{
scanf("%i", &userInput[counter]);
//determine sum
number = userInput[counter];
sum += number;
counter++;
average = sum / counter;
printf("\n The average of the numbers is %.2f", average);
printf("\n The sum of the numbers is %.2lf", sum);
printf("\n You may enter up to 1000 numbers");
printf("\n You have entered %d numbers\n", counter);
// number entered so far
printf("\n The list of numbers entered so far : \n");
for(int i=0;i<counter;i++){
printf(" %d ",userInput[i]);
}
printf("\n");
if (counter == 1000)
{
printf("\nThank you for using my program! Have a lovely day :)");
return;
}
}
}
}
}
int main(){
sumFunction();
}

Having trouble using string

Q) Help out our canteen and develop a program for cash
counter where user selects items out of 5 options with price .
The program keeps on asking until user hit 'q'. After check
out calculate total amount and show user the items purchased.
#include <stdio.h>
int main()
{
char menu[5][15]={"Pizza","Burger","Ice Cream","Pasta","Cold Drink"};
int price[5]={1000,500,300,100,50};
int i,amount=0,total=0,item=0;
printf("\t\t=========ALI FAST FOOD========");
printf("\n\t\tPlease select any option from below\n");
for (i=0;i<5;i++)
{
printf("\nAvailable items are %s press %d",menu[i],item+1);
printf("\tPrice is %d",price[i]);
printf("\n");
item++;
}
int choice;
int start;
printf("Enter number to start buying food");
do {
scanf("\n%d ",&choice);
if (choice==1)
{
static int count=0;
amount = amount+price[0];
printf("\nYou bought %d %s",count+1,menu[0]);
count++;
}
else if (choice==2)
{
static int count=0;
amount = amount+price[1];
printf("\nYou bought %d %s",count+1,menu[1]);
count++;
}
else if (choice==3)
{
static int count=0;
amount = amount+price[2];
printf("\nYou bought %d %s",count+1,menu[2]);
count++;
}
else if (choice==4)
{
static int count=0;
amount = amount+price[3];
printf("\nYou bought %d %s",count+1,menu[3]);
count++;
}
else if (choice==5)
{
int count=0;
amount = amount+price[4];
printf("\nYou bought %d %s",count+1,menu[4]);
count++;
}
else if (choice==0 || choice>=7)
{
printf("\nEnter correct option please\n");
}
}
while (choice!=6);
total = total + amount;
printf("\nTotal amount to be pay is %d",total);
printf("\nThank you for visiting our canteen have a nice day !");
}
In this i need to end the program when user enters "q" instead of "6" . i am unable to do so, please help me fix this issue.
As suggested by "Cid" try handling the input as characters instead of
integers.
Also, as suggested by "JohnBode" use a blank space before "%c" and
not before and after "%c"(i suspect that you tried using a blank
space after the "%c" as well). Let me know if I am mistaken.
Putting it together, you code should look like this....
#include <stdio.h>
int main()
{
char menu[5][15]={"Pizza","Burger","Ice Cream","Pasta","Cold Drink"};
int price[5]={1000,500,300,100,50};
int i,amount=0,total=0,item=0;
printf("\t\t=========ALI FAST FOOD========");
printf("\n\t\tPlease select any option from below\n");
for (i=0;i<5;i++)
{
printf("\nAvailable items are %s press %d",menu[i],item+1);
printf("\tPrice is %d",price[i]);
printf("\n");
item++;
}
char choice;
int start;
printf("Enter number to start buying food\n");
do {
scanf(" %c",&choice);
if (choice=='1')
{
static int count=0;
amount = amount+price[0];
printf("\nYou bought %d %s\n",count+1,menu[0]);
count++;
}
else if (choice=='2')
{
static int count=0;
amount = amount+price[1];
printf("\nYou bought %d %s\n",count+1,menu[1]);
count++;
}
else if (choice=='3')
{
static int count=0;
amount = amount+price[2];
printf("\nYou bought %d %s\n",count+1,menu[2]);
count++;
}
else if (choice=='4')
{
static int count=0;
amount = amount+price[3];
printf("\nYou bought %d %s\n",count+1,menu[3]);
count++;
}
else if (choice=='5')
{
int count=0;
amount = amount+price[4];
printf("\nYou bought %d %s\n",count+1,menu[4]);
count++;
}
//checking for lower and upper case "q"
else if (choice=='0' || choice >= '6' && (choice != 'q' && choice != 'Q'))
{
printf("\nEnter correct option please\n");
}
} while (choice!='q' && choice!='Q');//checking for lower and upper case "q"
total = total + amount;
printf("\nTotal amount to be pay is %d\n",total);
printf("\nThank you for visiting our canteen have a nice day !\n");
}

"Guess The Number" In C But If Statements Don't Work Right

I have a program in C that the user runs to play a "Guess The Number" game. It runs correctly to start but after the user enters 2 numbers (1 for the initial and 1 for the try again) the program repeats when it is supposed to have a limited number of tries.
Here is my code for the program:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
//----Guess a Number Game.-----------------------------------------
// srand and rand needs the #include <stdlib.h> library
srand(time(NULL)); //seeding the guess a number game with the system time, so the guess a # game always starts at a different point.
int guess;
int correctnum;
correctnum = rand();
printf("Enter a number:");
scanf("%i",&guess);
if(guess>correctnum) // If aa is greater than bb AND aa is greater than cc.
{
printf("Please enter another number, lower this time!");
scanf("%i",&guess);
main();
}
else if (guess<correctnum)
{
printf("Please enter another number, higher this time!");
scanf("%i",&guess);
main();
}
else if (guess==correctnum)
{
printf("You are a WINNER!\n");
printf("You guessed the number right and it was %i!\n",correctnum);
}
int repeat;
printf("Would you like to play again? 1=Yes and 2=No.");
scanf("%i",&repeat);
if(repeat==1)
{
main();
}
if(repeat==2)
{
printf("Hope you had a good time playing the game! See you soon!\n");
return 0;
}
}
Don't call main() recursively. What you need is a simple loop. Something like:
int main(void) {
srand(time(NULL));
int correctnum = rand();
int guess;
int done = 0;
while (! done) {
printf("Enter a number:");
scanf("%i",&guess);
if (guess>correctnum) {
printf("Please enter another number, lower this time!");
} else if (guess<correctnum) {
printf("Please enter another number, higher this time!");
} else /* if (guess==correctnum) */ {
printf("You are a WINNER!\n");
printf("You guessed the number right and it was %i!\n",correctnum);
done = 1;
}
}
}
You should probably check for errors from scanf() too, but first things first.
you can implement all functions you want in a loop:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void main(){
srand(time(NULL));
int guess;
int correctnum;
correctnum = rand() % 100; #it's better to get a number between 1~100
int done = 1;
printf("guess the number:\t");
scanf("%i", &guess);
while(done){
if(guess < correctnum){
printf("Please enter another number, higher this time:\t");
scanf("%i", &guess);
}
else if(guess > correctnum){
printf("Please enter another number, lower this time:\t");
scanf("%i", &guess);
}
else if(guess == correctnum){
printf("well done! if you want to play again, input 1, else input 0:\t");
scanf("%i", &done);
if(done == 1){
correctnum = rand() % 100;
printf("guess the number:\t");
scanf("%i", &guess);
}
}
}
printf("Hope you had a good time playing the game! See you soon!\n");
}

Inventory of a grocery store

I write a program to keep track of the inventory of a grocer store. But in my code i would like to print a value. And value is (Number of units) * (unit price). But somehow i get get the garbage value in my program. So Can you Please help me?
#include<stdio.h>
#include<conio.h>
#define MAX 5
int printinventory(int , int unit[] , float price[]);
int main()
{
int item[MAX],unit[MAX],x,i;
float price[MAX];
printf("Please enter how many category items (up to 5) : ");
scanf("%d",&x);
for(i=0;i<x;i++)
{
printf("\nPlease enter Number of Units #%d : ",i+1);
scanf(" %d",&unit[i]);
printf("\nPlease enter the Unit Price #%d : ",i+1);
scanf(" %f",&price[i]);
}
printinventory(x , unit , price);
getch();
}
int printinventory (int y, int unit[] , float price[])
{
int i,j=0;
float value[MAX];
for(i=0;i<y;i++);
{
value[i] = (unit[i] * price[i]);
}
system("cls");
printf("Item Number of Units Unit Price Value ");
printf("\n\n------------------------------------------------");
for(i=1;i<=y;i++)
{
printf("\n%d",i);
printf("\t %d",unit[j]);
printf("\t\t $%.2f",price[j]);
printf("\t$%.2f",value[j]);
j++;
}
printf("\n\n------------------------------------------------");
printf("\n\t\t\t\tTotal $ ");
getch();
}
The problem seems to be that you've mistakenly included a semicolon at the end of one of your for loops:
for(i=0;i<y;i++);

Resources