I've attempted to find if the user has inputted a product id value that's a duplicate and if so, it just tells them that it's a duplicate value and then returns to the menu in my switch statement.
The actual result i get, is that after "productsfilled == 0", it won't utilise the For Loops to check for the duplicates and productsfilled will remain at 1. I've looked online and this way of finding duplicates tends to work and i have used it previously in my code, so I don't think that could be the issue.
Here's my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <conio.h>
int productsfilled;
struct ProductData{
int product_id;
char product_name[120];
int price;
};
int quiz_5(){
char ch;
int size, input = 0;
struct ProductData products[20];
while(1){
printf("\nWelcome To The Super Mall's Product List. Please Select An Option:\n1. Add Product\n2. Display Product\n3. Delete Product\n");
fflush(stdin);
switch(getchar()){
case '1':
printf("\nPlease Enter Product ID:");
scanf("%d",&products[productsfilled].product_id);
printf("\nPlease Enter Product Name:");
scanf("%s",&products[productsfilled].product_name);
printf("\nPlease Enter Product Price:");
scanf("%d",&products[productsfilled].price);
printf("Productsfilled: %d",productsfilled);
if(productsfilled == 0){
productsfilled = 1;
break;
}
for(int i = 0; i < productsfilled;i++){
for (int j = i + 1; j < productsfilled;j++){
if(products[i].product_id == products[j].product_id){
printf("\nPlease Use Different Product ID");
break;
}else{
printf("test");
productsfilled += 1;
break;
}
}
}
break;
case '2':
while(1){
for(int i = 0;i < productsfilled;i++){
printf("Product ID: %d Product Name: %s Product Price: %d\n",products[i].product_id,products[i].product_name,products[i].price);
}
printf("Please Press Enter To Continue");
fflush(stdin);
if(getchar() == '\n'){
break;
}
}
case '3':
break;
case '\n':
break;
default:
printf("Please Select An Option:\n1. Add Product\n2. Display Product\n3. Delete Product: ");
}
}
}
int main() {
int input = 1;
printf("Welcome to my assignment. Which quiz do you want to run (please input the number of the quiz e.g. for quiz 1, type 1): \n-Quiz 1\n-Quiz 2\n-Quiz 3\n-Quiz 4\n-Quiz 5\n-Quiz 6\n-Quiz 7\n");
while(input == 1){
fflush(stdin);
switch(getchar()){
case '5':
quiz_5();
break;
case '\n':
printf("Welcome to my assignment. Which quiz do you want to run (please input the number of the quiz e.g. for quiz 1, type 1): \n-Quiz 1\n-Quiz 2\n-Quiz 3\n-Quiz 4\n-Quiz 5\n-Quiz 6\n-Quiz 7\n");
getchar();
default:
printf("Invalid Input\n");
} }
return 0;
}
The problem is that you don't increment productsfilled before you enter the loop...therefore, productsfilled is always 1 less than the actual length of your array which means that you don't compare all elements in the array.
Try your program on 2 inputs, both with the same ID. You'll see that you don't compare anything.
You are wrong when using scanf for string input:
scanf("%s",&products[productsfilled].product_name);
You should not use &, you should use as below:
scanf("%119s",products[productsfilled].product_name);
OT, in main function:
switch(getchar()){
case '5':
...
Because getchar() will return int value, so if you want to access to quiz_5, you have to type 35 (ANSCI code) instead of type 5 when you run your program.
char a = '5';
similar to:
int a = 35;
Related
this program lets the user enter a string and then asks what to do ( have deleted the parts that actually do stuff to make everything lighter and the problem easier to spot). The problem is that whenever a string longer than 256 characters is entered, it kinda "spills" into the following fgets(), which in turn prints "insert a valid input".
here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <string.h>
int main(void) {
printf("your string:\n");
char stringa[257];
fgets(stringa, 257, stdin);
for (int i = 0; stringa[i]; i++) {
stringa[i] = tolower(stringa[i]);
}
char stringakek[257];
int j= 0;
for (int i = 0; stringa[i]; i++) {
if (stringa[i] >= 'a' && stringa[i] <= 'z' && stringa[i] != '\0') {
stringakek[j] = stringa[i];
j++;
}
}
printf("this is your string all in lowercase and containing only letters: %s\n", stringakek);
printf("what do you want to do?\n");
printf("press 1 for case 1\n");
printf("press 2 for case 2\n");
printf("press 3 for case 3\n");
int controllo = 0;
char a[300000];
while (controllo == 0) {
fgets(a, 300000, stdin);
switch(a[0]) {
case '1':
printf("case 1\n");
controllo = 1;
break;
case '2':
printf("case 2\n");
controllo = 1;
break;
case '3':
printf("case 3\n");
controllo = 1;
break;
default:
printf("insert a valid input\n");
break;
}
}
}
For example if something like
*R3HQ53BAQ5I4GAERÈ+ÒÈGA43IOHAUNVKANE124RÈ+fioureahjkgaeu+-reR3HQ53BAQ5I4GAERÈ+ÒÈGA43IOHAUNVKANE124RÈ+fioureahjkgaeu+-reR3HQ53BAQ5I4GAERÈ+ÒÈGA43IOHAUNVKANE124RÈ+fioureahjkgaeu+-reR3HQ53BAQ5I4GAERÈ+ÒÈGA43IOHAUNVKANE124RÈ+fioureahjkgaeu+-reR3HQ53BAQ5I4GAERÈ+ÒÈGA43IOHAUNVKANE124RÈ+fioureahjkgaeu+-re*
was input the expected output would be:
*your string:
R3HQ53BAQ5I4GAERÈ+ÒÈGA43IOHAUNVKANE124RÈ+fioureahjkgaeu+-reR3HQ53BAQ5I4GAERÈ+ÒÈGA43IOHAUNVKANE124RÈ+fioureahjkgaeu+-reR3HQ53BAQ5I4GAERÈ+ÒÈGA43IOHAUNVKANE124RÈ+fioureahjkgaeu+-reR3HQ53BAQ5I4GAERÈ+ÒÈGA43IOHAUNVKANE124RÈ+fioureahjkgaeu+-reR3HQ53BAQ5I4GAERÈ+ÒÈGA43IOHAUNVKANE124RÈ+fioureahjkgaeu+-re
this is your string all in lowercase and containing only letters: rhqbaqigaergaiohaunvkanerfioureahjkgaeurerhqbaqigaergaiohaunvkanerfioureahjkgaeurerhqbaqigaergaiohaunvkanerfioureahjkgaeurerhqbaqigaergaiohaunvkanerfioureahjkgaeurerhq
what do you want to do?
press 1 for case 1
press 2 for case 2
press 3 for case 3*
but it actually outputs this:
*your string:
R3HQ53BAQ5I4GAERÈ+ÒÈGA43IOHAUNVKANE124RÈ+fioureahjkgaeu+-reR3HQ53BAQ5I4GAERÈ+ÒÈGA43IOHAUNVKANE124RÈ+fioureahjkgaeu+-reR3HQ53BAQ5I4GAERÈ+ÒÈGA43IOHAUNVKANE124RÈ+fioureahjkgaeu+-reR3HQ53BAQ5I4GAERÈ+ÒÈGA43IOHAUNVKANE124RÈ+fioureahjkgaeu+-reR3HQ53BAQ5I4GAERÈ+ÒÈGA43IOHAUNVKANE124RÈ+fioureahjkgaeu+-re
this is your string all in lowercase and containing only letters: rhqbaqigaergaiohaunvkanerfioureahjkgaeurerhqbaqigaergaiohaunvkanerfioureahjkgaeurerhqbaqigaergaiohaunvkanerfioureahjkgaeurerhqbaqigaergaiohaunvkanerfioureahjkgaeurerhq
what do you want to do?
press 1 for case 1
press 2 for case 2
press 3 for case 3
**insert a valid input***
Why is this? How can I fix this?
So, I'm totally new to C and I'm having some struggle with some basic concepts, mainly when it is about buffer, memory, and string creation.
So, I have this assignment from College where I need to make an Array of Structs called Vehicles, and during the execution of the program I must take inputs from the user about the Vehicle mark, year, and licensePlate.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Vehicles {
char mark[20];
int modelYear;
char licensePlate[8];
};
int main() {
const int DEFAULT_SIZE = 10;
struct Vehicles vehiclesArr[DEFAULT_SIZE];
for(int i = 0; i < DEFAULT_SIZE ; i++) {
memcpy(vehiclesArr[i].mark, "", 1);
vehiclesArr[i].modelYear = 0;
memcpy(vehiclesArr[i].licensePlate, "", 1);
}
int choice = 0;
int counter = 0;
printf("Hello Professor, my name is Henrique and in this program I will be your guide.\n");
while (choice != 4) {
printf("So, what it will be for today?\n Choose a valid option.\n");
printf("0. Create a new Vehicle.\n");
printf("1. List all saved vehicles.\n");
printf("2. Update a saved vehicle.\n");
printf("3. Delete a saved vehicle.\n");
printf("4. Quit Program.\n");
scanf("%d", &choice);
switch (choice) {
case 0:
if (counter < 10) {
printf("Choose a vehicle mark \n");
scanf("%[^\n]s", vehiclesArr[counter].mark);
printf("\n Choose the model year \n");
scanf("%d", &vehiclesArr[counter].modelYear);
printf("\n Choose license plate \n");
scanf("%[^\n]s", vehiclesArr[counter].licensePlate);
counter++;
break;
} else {
printf("\n Storage limit reached.");
}
case 1:
for(int i = 0; i < DEFAULT_SIZE; i++) {
printf("\nModel: %s", vehiclesArr[i].mark);
printf("\nYear: %d", vehiclesArr[i].modelYear);
printf("\nLicense Plate: %s", vehiclesArr[i].licensePlate);
}
break;
case 2:
break;
case 3:
break;
case 4:
printf("Gently finishing program... Bye Bye!");
break;
default:
printf("\nWhat? The number must be between 0 and 4\n");
}
}
}
The problem is that, when I execute the program and press 0, the program just skip the user input on those two lines bellow:
...
printf("Choose a vehicle mark \n");
scanf("%[^\n]s", vehiclesArr[counter].mark);
...
printf("\n Choose license plate \n");
scanf("%[^\n]s", vehiclesArr[counter].licensePlate);
...
I'm trying to figure out what is happening been a while now, the only thing that works is if I use "%s", but I need the program to handle when user inputs an space character.
Can someone help me, and please explain why is this happening? This is my first question so I would appreciate if you guys also tell me how I can make my questions better.
This is a part of a program that ask the user to put students data with interactive choices.
int main(){
unsigned int choice = 0;
char new_name[7] = {0};
unsigned int num_students = 0;
Student students_list[3] = {0}; // Student is a struct type
do {
printf("Press 1 to add a student, 2 to edit an existing student, 0 to exit and print the results.\n");
printf("Choose option: ");
scanf("%d", &choice);
switch (choice){
case 1:
printf("Enter the serial number (7 digits): ");
scanf("%s", new_name);
students_list[num_students] = create_student(new_name);
printf("Student added\n");
num_students += 1;
break;
case 2:
printf("student list:\n");
for (size_t i=0; i<num_students; ++i){
print_results(students_list, &num_students);
printf("Choose student id to edit: ");
}
break;
case 0:
printf("I'm printing the results... I'm printing the results...");
print_results(students_list, &num_students);
break;
default:
printf("Error, try again.\n");
break;
}
} while (choice != 0);
}
But there must be something wrong.. It's stuck.
Output:
$ ./"strucfirst"
Press 1 to add a student, 2 to edit an existing student, 0 to exit and print the results.
Choose option: 1
Enter the serial number (7 digits): 1234567
Student added
What am I doing wrong? Is it do_while or switch that makes problems?
Full code (in italian :\ ) if maybe the error could be in other parts of the program: https://hastebin.com/eqayepaquh.cpp
#include <stdio.h>
#include <stdlib.h>
#define NEWGEAR 15.0
#define USEDGEAR 5.0
int main() {
int type;
int num_items;
float total;
printf("Welcome to the market\n");
printf("What would you like to do\n");
printf("\t1-Buy New Gear\n");
printf("\t2-Buy used gear\n");
printf("\t3-Quit\n");
scanf("%d", &type);
while (type != 3) {
switch (type) {
case 1:
printf("How many new items would you like?\n");
scanf("%d", &num_items);
total = num_items * 15.0;
break;
This is where the code keeps on asking how many new items would you like?
case 2:
printf("How many old items would you like?\n");
scanf("%d", &num_items);
total = num_items * USEDGEAR;
break;
This is where the code keeps on asking how many old items would you like?
case 3:
break;
default:
printf("Not a valid option\n");
break;
}
}
printf("Your total cost is %f\n",total);
return 0;
}
Both my choices are looping
You never update the type variable to 3, so the while loop never terminates. Although you do have a break statement, it is affecting the switch and not the while loop that surrounds it.
I think this will handle what you want to achieve.
#include <stdio.h>
#include <stdlib.h>
#define NEWGEAR 15.0
#define USEDGEAR 5.0
int main() {
int type = 0;
int num_items;
float total;
printf("Welcome to the market\n");
printf("What would you like to do\n");
printf("\t1-Buy New Gear\n");
printf("\t2-Buy used gear\n");
printf("\t3-Quit\n");
while (type != 3) {
printf("Enter an option: ");
scanf("%d", &type);
if(type == 3)
break;
switch (type) {
case 1:
printf("How many new items would you like?\n");
scanf("%d", &num_items);
total = num_items * 15.0;
break;
case 2:
printf("How many old items would you like?\n");
scanf("%d", &num_items);
total = num_items * USEDGEAR;
break;
default:
printf("Not a valid option\n");
break;
}
}
printf("Your total cost is %f\n",total);
return 0;
}
Basically, after your user finished doing cases 1-2 or default, your program would prompt your user for an option again if he wants to quit or perform another case 1 or 2 operation.
Your loop logic is flawed:
you should move the prompt code inside the loop.
you should update total for each answer.
you should test if scanf() is successful at converting user input.
Here is a modified version:
#include <stdio.h>
#define NEWGEAR 15.0
#define USEDGEAR 5.0
int main() {
int type;
int num_items;
double total = 0;
printf("Welcome to the market\n");
for (;;) {
printf("What would you like to do\n");
printf("\t1-Buy New Gear\n");
printf("\t2-Buy used gear\n");
printf("\t3-Quit\n");
if (scanf("%d", &type) != 1 || type == 3)
break;
switch (type) {
case 1:
printf("How many new items would you like?\n");
if (scanf("%d", &num_items) == 1)
total += num_items * 15.0;
break;
case 2:
printf("How many old items would you like?\n");
if (scanf("%d", &num_items) == 1)
total += num_items * USEDGEAR;
break;
default:
printf("Not a valid option\n");
break;
}
}
printf("Your total cost is %f\n", total);
return 0;
}
So I'm trying to make a grade book that does everything displayMenu() says. But i cant even get the student ID to save when i go to view the grades. Please Help.
Everything is initialized here
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define PAUSE system("pause")
#define CLS system("cls")
#define EXAMS 100
#define STUDENT 4
void displayArray(float grades[STUDENT][EXAMS]);
void newStudent(float grades[STUDENT][EXAMS]);
void displayStudentAverage(float grades[STUDENT][EXAMS]);
int main() {
float grades[STUDENT][EXAMS];
This is everything the program should do
displayMenu(grades, 0 );
} // end of main;
int displayMenu(float grades[STUDENT][EXAMS]) {
printf("\t \t MENU \t \t");
printf("Enter Corresponding Number\n");
printf("1.Enter New Student\n");
printf("2.Change Existing Grades\n");
printf("3.View All Grades\n");
printf("4.View Average Score Per Student\n");
printf("5.View Average Score Per Exam\n");
printf("6.View Average Score For The class\n");
printf("7.CLEAR GRADEBOOK\n");
printf("8. Save Gradebook\n");
printf("8.Exit\n");
int choice = 0;
scanf("%d", &choice);
switch (choice) {
case 1:
newStudent(grades, 0);
CLS;
displayMenu(grades,0);
break;
case 2:
break;
case 3: displayArray(grades, 0);
CLS;
displayMenu(grades,0);
break;
case 4:
displayStudentAverage(grades, 0);
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
exit(0);
break;
case 9:
exit(0);
break;
default: printf("You Have entered an incorrect number");
PAUSE;
}
}
This is what displays the grades
void displayArray(float grades[STUDENT][EXAMS]) {
printf("%.1f\t", grades[STUDENT][EXAMS]);
}
I'm trying to add the values to the array here
void newStudent(float grades[STUDENT][EXAMS]) {
float addgrade;
printf("Please Enter Student ID: ");
scanf("%f", &grades[STUDENT][EXAMS]);
printf("Enter four exam grades, use comma to split grades");
scanf("%f", addgrade);
grades[STUDENT][EXAMS] += addgrade;
PAUSE;
CLS;
}
void displayStudentAverage(float grades[STUDENT][EXAMS]) {
int sum, loop;
float avg;
sum = avg = 0;
for (loop = 0; loop < 10; loop++) {
sum = sum + grades[loop];
}
avg = (float)sum / loop;
printf("Average of array values is %.2f", avg);
}
First of all, it's always good check the compiler warnings to get some hints to possible bugs...
Here's a list of problems in the code:
no header files included
displayMenu prototype is missing
the grades array is used with inconsistent types (float/int)
with grades[STUDENT][EXAMS] the grades array is accessed out of bounds (for example if you define an array of size 5 you can only access position 0 to 4)
the return type of main needs to be int
the function newStudent has return type void but the code tries to return something with return &grades[STUDENT][EXAMS];
Apart from that, the code should work...