I seem to have an issue with the output of my program, when i choose option 1 it works fine in terms of asking me what data I would like to add with the packets, I enter numerical figures as I am supposed to but except for the data variable output it outputs strange ASCII characters instead of the numbers I originally inputted so any help would be appreciated thank you.
#include <stdio.h>
#include <stdlib.h>
struct packet{
int source[4];
int destination[4];
int type[4];
int port[4];
char data[50];
};
void main ()
{
struct packet s[50]; //Array for structure input
int choice;
int customerCount = 0, ii = 0;
while (customerCount <= 50){
printf("What would you like to do?\n");
printf("\t1) Add a packet.\n");
printf("\t2) s all packets.\n");
printf("\t3) Save packets.\n");
printf("\t4) Clear all packets.\n");
printf("\t5) Quit the programme.\n");
scanf("%i", &choice);
switch (choice)
{
case 1: printf("\n****Adding a packet*****\n");
printf("Where is the packet from?\n");
scanf("%i", &s[customerCount].source);
printf("Where is the packet going?\n");
scanf("%i", &s[customerCount].destination);
printf("What type is the packet?\n");
scanf("%i", &s[customerCount].type);
printf("What is the packet's port?\n");
scanf("%i", &s[customerCount].port);
printf("Enter up to 50 characters of data.\n");
scanf("%s", s[customerCount].data);
customerCount++;
break;
case 2: printf("\nDisplaying Infomation\n");
for(ii = 0; ii < customerCount; ii++) {
printf("\nSource: %s", s[ii].source);
printf("\nDestination: %s", s[ii].source);
printf("\nType : %s", s[ii].type);
printf("\nPort : %s", s[ii].port);
printf("\nData: %s\n---\n", s[ii].data);
}
break;
case 3: break;
case 4: break;
case 5: break;
default: printf("\nThis is not a valid choice, please choose again\n\n");
break;
}
}
}
You're adding input to the structure's int arrays as though they're a single int and printing them as though they're strings. Make the int arrays single ints and print them with %i as the printf format specifier.
struct packet
{
int source;
int destination;
int type;
int port;
char data[50];
};
case 2:
printf("\nDisplaying Infomation\n");
for(ii = 0; ii < customerCount; ii++)
{
printf("\nSource: %i", s[ii].source);
printf("\nDestination: %i", s[ii].source);
printf("\nType : %i", s[ii].type);
printf("\nPort : %i", s[ii].port);
printf("\nData: %s\n---\n", s[ii].data);
}
break;
Related
I'm trying to write a program that allows to add data about different types of stars and print them back on the screen. Why does this not work:
typedef union{
struct{
char name_main[30];
int age;
char color[12];
};
struct{
char name_binary[30];
double radius_1, radius_2;
};
struct{
char name_light[30];
double luminosity_low, luminosity_high, period;
};
}STARS;
int main(int argc, char **argv)
{
bool running = true;
int choice;
STARS *star = (STARS *)malloc(sizeof(star));
printf("Star Menu\n");
putchar('\n');
while(running)
{
putchar('\n');
printf("Please choose a type of star:\n");
printf("1. Main Sequence Star\n2. Binary Star\n3. Variable Light Star\n4. Exit\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("You've chosen: Main Sequence Star\n");
printf("Please enter the name of the star:\n");
scanf("%s", star->name_main);
//printf("%s", star->name_main);
printf("Please enter the age of: %s\n", star->name_main);
scanf("%d", &star->age);
printf("Age: %d\n", star->age);
printf("Please enter the color of: %s\n", star->name_main);
scanf(" %s", star->color);
printf("Color: %s\n", star->color);
// printf("Type : Main sequence | Name : %s | Age : %d | Color : %s\n", star->name_main, star->age, star->color);
break;
case 2:
printf("You've chosen: Binary Star\n");
printf("Please enter the name of the star:\n");
scanf("%s", star->name_binary);
printf("Name: %s\n", star->name_binary);
printf("The radius of the first star:\n");
scanf("%lf", &star->radius_1);
printf("The radius of the second star:\n");
scanf("%lf", &star->radius_2);
printf("Radius 1: %.2lf | Radius 2: %.2lf\n", star->radius_1, star->radius_2);
break;
case 3:
printf("You've chosen: Variable Light Star\n");
break;
case 4:
printf("Exit\n");
running = false;
break;
}
}
return 0;
}
I'm using a union of structs because each star has specific characteristics and we don't care about the others' when we write for a certain type. The issue appears at the color, if I write red the output will be: Color: r:. I've tried adding a space before the format specifier, consuming a character before using scanf but the issue persists and I'm not sure why. Any thoughts?
You didn't allocate enough size of buffer. sizeof(star) is the size of the pointer. A pointer is typically 4-byte or 8-byte long, but the structure is at least 30-byte long for char name_main[30];.
Also note that casting results of malloc() family is considered as a bad practice.
Wrong line:
STARS *star = (STARS *)malloc(sizeof(star));
It should be:
STARS *star = malloc(sizeof(*star));
STARS *star = (STARS *)malloc(sizeof(star));
It's a mistake. sizeof(star) returns size of the pointer, not the structure.
Change it to: STARS *star = (STARS *)malloc(sizeof(*star)); to fix your problem.
I'm having trouble passing my struct to a new function. My current code opens a text file, saves the relevant information to the structure and prints the saved information. Now I'm trying to write a function that will ask the user to input a name and for the code to check all name fields in the struct and return inforamtion once found a result. The code works fine until I try pass the struct to the "searchDroneName" function. The main shows that I have saved the relevant information properly and I did the exact same for the "searchDroneName" function. But when I print out the saved information of the struct in the "searchDroneName" function it prints out a bunch of random numbers and weird characters.
I'm sure it's just my lack of understanding of functions and how to pass information but and help is appreciated, thanks.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DRONE_COUNT 10
typedef struct{
int drone_number;
char drone_name[20];
int year_manufactured;
double mass;
double top_speed;
double max_distance;
double load_capacity;
} drone_info;
int searchDroneName(int no_of_drones){
drone_info droneinfo[10];
int i, found, numdrones;
char namechoice[20];
numdrones = no_of_drones;
// Test Data
printf("Data:\n\n");
for (i=0; i < numdrones; i++){
printf("ID: %d Name: %s Year: %d Mass: %.2f Top Speed: %.2f Max Distance: %.2f Load Capacity: %.2f\n",
droneinfo[i].drone_number, droneinfo[i].drone_name, droneinfo[i].year_manufactured, droneinfo[i].mass, droneinfo[i].top_speed, droneinfo[i].max_distance, droneinfo[i].load_capacity);
}
printf("Input Drone Name: ");
scanf("%19s", namechoice);
found = 0;
for (i=0; i < numdrones; ++i){
printf("Drone Name: %s\n", droneinfo[i].drone_name);
if (!strcmp(namechoice, droneinfo[i].drone_name)){
printf("FOUND A MATCH");
found = 1;
}
}
if(found == 0){
printf("No Matches Were Found!\n");
}
return 0;
}
int main(void) {
drone_info droneinfo[10];
int choice, droneID, yrman, i, no_of_drones;
float dronemass, dronemaxdist, dronetopspd, droneload;
char dronename[20];
i = 0;
FILE* inputfile = fopen("drone.txt", "r");
if(inputfile == NULL)
{
perror("ERROR! ");
exit(-1);
}
//GAY CODE BELLOW
while(fscanf(inputfile, "%d %19s %d %f %f %f %f", &droneID, dronename, &yrman, &dronemass, &dronetopspd, &dronemaxdist, &droneload)==7){
if(ferror(inputfile)){
perror("An error occurred: ");
}
droneinfo[i].drone_number = droneID;
strcpy(droneinfo[i].drone_name, dronename);
droneinfo[i].year_manufactured = yrman;
droneinfo[i].mass = dronemass;
droneinfo[i].top_speed = dronetopspd;
droneinfo[i].max_distance = dronemaxdist;
droneinfo[i].load_capacity = droneload;
i++;
}
no_of_drones = i;
fclose(inputfile);
printf("Data:\n\n");
for (i=0; i < no_of_drones; i++){
printf("ID: %d Name: %s Year: %d Mass: %.2f Top Speed: %.2f Max Distance: %.2f Load Capacity: %.2f\n",
droneinfo[i].drone_number, droneinfo[i].drone_name, droneinfo[i].year_manufactured, droneinfo[i].mass, droneinfo[i].top_speed, droneinfo[i].max_distance, droneinfo[i].load_capacity);
}
//GAY CODE ABOVE
do{
printf("Please select an option:\n\n");
printf("1. Input/update drone information\n");
printf("2. Search a drone\n");
printf("3. Simulate a drone delivery scenario\n");
printf("4. Display simulation results\n");
printf("5. Save drone information\n");
printf("6. Save all results\n");
printf("7. Exit\n\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
//Input Drone Function
break;
case 2:
//Search Drone function
searchDroneName(no_of_drones);
break;
case 3:
//Simulate Drone function
break;
case 4:
//Display simulation results function
break;
case 5:
//Save drone information function
break;
case 6:
//Save all results function
break;
case 7:
// Exit, Breaks loop
break;
default:
printf("\nInvalid choice! Please enter a number inbetween 1 and 7!\n\n" );
break;
}
} while (choice != 7);
return 0;
}
Change
int searchDroneName(int no_of_drones){
drone_info droneinfo[10];
to
int searchDroneName(drone_info *droneinfo, int no_of_drones){
and
case 2:
//Search Drone function
searchDroneName(no_of_drones);
to
case 2:
//Search Drone function
searchDroneName(droneinfo, no_of_drones);
I'm having problems editing the fields of a structure from a seperate function, I'm trying to edit fields of my drone structure from the update droneinfofunction .basically i get the same error for all the arrows (invalid type argument of '->')
i'm sure this problem stems from my lack of understanding of pointers
any help would be greatly appreciated :)
here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DRONE_COUNT 10
typedef struct{
//define struct info and variables
int drone_number;
char drone_name[20];
int year_manufactured;
double mass;
double top_speed;
double max_distance;
double load_capacity;
} drone_info;
int updateDroneInfo(drone_info *droneinfo, int no_of_drones) {
int searchID, numdrones, i, drYrMan;
double drMass, drTopSpeed, drMaxDist, drLoadCap;
char drname[20];
numdrones = no_of_drones;
printf("what drone ID would you like to update?: ");
scanf("%d", &searchID);
printf("name: ");
scanf("%s", drname);
printf("year manufactured: ");
scanf("%d", &drYrMan);
printf("mass: ");
scanf("%lf", &drMass);
printf("top speed: ");
scanf("%lf", &drTopSpeed);
printf("max distance: ");
scanf("%lf", &drMaxDist);
printf("load capacity: ");
scanf("%lf", &drLoadCap);
droneinfo[searchID]->drone_number = searchID;
droneinfo[searchID]->drone_name = drname;
droneinfo[searchID]->year_manufactured = drYrMan;
droneinfo[searchID]->mass = drMass;
droneinfo[searchID]->top_speed = drTopSpeed;
droneinfo[searchID]->max_distance = drMaxDist;
droneinfo[searchID]->load_capacity = drLoadCap;
for(i=0; i < numdrones; i++){
}
return 0;
}
//drone search function
int searchDroneName(drone_info *droneinfo, int no_of_drones){
int i, found;
char namechoice[20];
printf("input drone name: ");
scanf("%s", namechoice);
found=0;
scanf("what drone would you like to search %s", namechoice);
for (i=0; i < no_of_drones; i++){
if (!strcmp(namechoice, droneinfo[i].drone_name)) {
printf("found a match\n\nID: %d Name: %s Year: %d Mass: %.2f Top Speed: %.2f Max Distance: %.2f Load Capacity: %.2f\n",
droneinfo[i].drone_number, droneinfo[i].drone_name, droneinfo[i].year_manufactured, droneinfo[i].mass, droneinfo[i].top_speed, droneinfo[i].max_distance, droneinfo[i].load_capacity);
found = 1;
}
}
if(found == 0){
printf("\nNo matches were found!\n");
}
return 0;
//make condition for all
}
int main(void) {
drone_info droneinfo[10];
int choice, droneID, yrman, i, no_of_drones;
double dronemass, dronemaxdist, dronetopspd, droneload;
char dronename[20];
i=0;
//open the drone.txt file where the drone info is stored
FILE* inputfile = fopen("drone.txt", "r");
if(inputfile == NULL)
{
perror("ERROR! ");
exit(-1);
}
//initialise the function that puts the struct in an array
while(fscanf(inputfile, "%d %19s %d %lf %lf %lf %lf", &droneID, dronename, &yrman, &dronemass, &dronetopspd, &dronemaxdist, &droneload)==7){
if(ferror(inputfile)){
perror("An error occurred: ");
}
droneinfo[i].drone_number = droneID;
strcpy(droneinfo[i].drone_name, dronename);
droneinfo[i].year_manufactured = yrman;
droneinfo[i].mass = dronemass;
droneinfo[i].top_speed = dronetopspd;
droneinfo[i].max_distance = dronemaxdist;
droneinfo[i].load_capacity = droneload;
i++;
}
no_of_drones = i;
fclose(inputfile);
//print the dtone info in an array
printf("Data:\n\n");
for (i=0; i < no_of_drones; i++){
printf("ID: %d Name: %s Year: %d Mass: %.2f Top Speed: %.2f Max Distance: %.2f Load Capacity: %.2f\n",
droneinfo[i].drone_number, droneinfo[i].drone_name, droneinfo[i].year_manufactured, droneinfo[i].mass, droneinfo[i].top_speed, droneinfo[i].max_distance, droneinfo[i].load_capacity);
}
do{
//program menu with appropriate menu items
printf("Please select an option:\n\n");
printf("1. Input/update drone information\n");
printf("2. Search a drone\n");
printf("3. Simulate a drone delivery scenario\n");
printf("4. Display simulation results\n");
printf("5. Save drone information\n");
printf("6. Save all results\n");
printf("7. Exit\n\n");
scanf("%d", &choice);
//switch for the 7 available menu cases
switch(choice)
{
case 1:
//input drone function
updateDroneInfo(droneinfo, no_of_drones);
break;
case 2:
//search drone function
searchDroneName(droneinfo, no_of_drones);
break;
case 3:
//simulate drone function
break;
case 4:
//display simulation results
break;
case 5:
//save drone information
break;
case 6:
//save all results function
break;
case 7:
//exit/breaks the loop
break;
default:
printf("Invalid Data Entered! please enter a number between 1 and 7\n\n");
break;
}
} while(choice != 7);
return 0;
}
re
droneinfo[searchID]
has the type drone_info and not drone_info*, so you use . instead of ->.
In statements like this:
droneinfo[searchID]->drone_number = searchID;
the expression droneinfo[searchID] is not a pointer. It has the type drone_info because the pointer droneinfo was already dereferenced by the subscript operator.
You have to write:
droneinfo[searchID].drone_number = searchID;
Also arrays do not have the assignment operator. You need to copy element elements from one array to another.
Instead of this statement:
droneinfo[searchID]->drone_name = drname;
you have to write using the standard string function strcpy:
$include <string.h>
//...
strcpy( droneinfo[searchID].drone_name, drname );
#include <stdio.h>
#include <string.h>
#define mL 5
#define NL 20
#define UL 6
struct LIST
{
char n[NL];
float am;
char u[UL];
};
struct array
{
struct LIST array;
};
void addCityInformation(struct array *add, int *items);
void printCities(struct array *all, int items);
int main(void)
{
struct array shopping[mL];
int choice, nrOfItemsAdded = 0;
do
{
printf("\nWhat du you want to do?");
printf("\n1 - add grocery");
printf("\n2 - print shopping list");
printf("\n3 - exit");
printf("\nYour choice: ");
scanf("%d", &choice);
while(getchar() != '\n');
switch (choice)
{
case 1:
addCityInformation(&shopping[nrOfItemsAdded], &nrOfItemsAdded);
break;
case 2:
printCities(shopping, nrOfItemsAdded);
break;
case 3:
printf("Exiting program\n\n");
break;
default:
printf("Invalid input\n\n");
break;
}
}
while(choice != 3);
return 0;
}
int clean_stdin()
{
while (getchar()!='\n');
}
void addCityInformation(struct array *add, int *items)
{
if(*items == mL)
printf("No more space in the list\n");
else
{
printf("Enter name: ");
fgets(add->array.n, NL, stdin);
add->array.n[strlen(add->array.n)-1] = '\0';
do {
printf("Enter amount: ");
}while (scanf("%f", &add->array.am )); //loop untill other than float
getchar();
printf("Enter unit: ");
fgets((add->array.u), UL, stdin);
add->array.u[strlen(add->array.u)-1] = '\0';
(*items)++;
}
}
void printCities(struct array *all, int items)
{
printf("\n\n%-20s %-15s %-9s | %-6s\n", "Name", "amount", "unit");
printf("--------------------------------------------------------\n");
for(int i = 0; i < items; i++)
printf("%-20s %-15.1f %-9.4s \n", all[i].array.n, all[i].array.am, all[i].array.u);
}
This is my loop beside that i am only showing a part of the code. It now just continues to give enter amount and letting me register it in the struct. I want to restrict the user to only entering positive numbers and no character at all. And if he types a character it should rerun the loop even if it is 123Av123 it should run the loop and only register the correct number
Edit: now showing the whole code//loop untill other than float is what i want help with
int check=scanf("%f", &add->array.am )
if(check!=1||add->array.am<0){
printf("Incorrect input");
return 1;
}
I think that will do it.
Edit: you wanted it to rerun after so use continue; instead of return;
I'm having serious trouble with my program it is supposed to provide a menu and do all the functions the code is pretty explanatory my problem is I only have visual studios which doesnt allow scanf and scanf_s and messes with things so I use online compilers but those are still iffy. Can any one help and give me some tips. I'm having trouble with the math and function to list accounts, some whiles are empty as well I wasn't sure if that was best to use. I'm relatively new to this forum. It also can't have struct and can only be in C :-(
#include <stdio.h>
char name[20];
float avail_bal;
void options();
void open();
void list();
void deposit();
void withdraw();
void exit();
int main(void)
{
char option;
while(1){
printf("****Banking System WELCOME****\n");
printf("Enter 1-5 of the following options: \n");
option = getchar();
scanf("%c\n", &option);
switch(option)
{
case '1': open();
break;
case '2': list();
break;
case '3': deposit();
break;
case '4': withdraw();
break;
case '5': return 0;
default: exit();
break;
}
}
return 0;
}
void options()
{
printf("1. Open Account\n");
printf("2. List Accounts\n");
printf("3. Deposit\n");
printf("4. Withdraw\n");
printf("5. Exit");
}
void open()
{
float avail_bal = 0;
char name[20];
int acc_num;
printf("Open new account(enter number 1-5)\n\n");
scanf("%d", &acc_num);
printf("Account number: %d\n");
printf("Available balance: %f\n");
}
void list()
{
}
void deposit()
{
float add;
int acc_num;
printf("Which count do you want to deposit money in?");
scanf(" %d", &acc_num);
printf("Amount to deposit: ");
scanf("%f", &add);
while()
{
}
}
void withdraw()
{
int acc_num;
float withdraw;
printf("Account to withdraw from: ");
scanf("%d", &acc_num);
printf("Amount to withdraw from account: ")
scanf("%f", &withdraw);
while()
{
printf("Current balance for account %d: %f ");
break;
} acc_num++
}
The problem with scanf was interesting. Here is an example for how to do without (although you shouldn't) such that you can play with your code easier.
#include <stdio.h>
#include <stdlib.h>
// only 5 accounts possible
int accounts[5];
// each its balance
float avail_bal[5];
void options();
// open(P) is a standard Posix function
void bopen();
void list();
void deposit();
void withdraw();
// exit(3) is a standard C function
void pexit();
int main(void)
{
char option;
while (1) {
printf("****Banking System WELCOME****\n");
printf("Enter 1-5 of the following options: \n");
options();
option = getc(stdin);
// swallow the '\n'
getc(stdin);
switch (option) {
case '1':
bopen();
break;
case '2':
list();
break;
case '3':
deposit();
break;
case '4':
withdraw();
break;
case '5':
pexit();
default:
pexit();
}
}
return 0;
}
void options()
{
puts("1. Open Account");
puts("2. List Accounts");
puts("3. Deposit");
puts("4. Withdraw");
puts("5. Exit");
}
void bopen()
{
int acc_num;
char c;
puts("Open new account(enter number 1-5)");
c = getc(stdin);
getc(stdin);
// assuming ASCII here where the digits 0-9 start at place 48 in the table
acc_num = (int) c - 48;
if (acc_num < 1 || acc_num > 5) {
puts("Account number must be between one and five inclusive");
return;
}
if (accounts[acc_num] != 0) {
printf("Account number %d is already taken\n", acc_num);
return;
}
// mark account as taken
accounts[acc_num] = 1;
// spend a fiver for the new client for being a new client
avail_bal[acc_num] = 5.0;
printf("Account number: %d\n", acc_num);
printf("Available balance: %f\n", avail_bal[acc_num]);
}
void list()
{
int i;
for (i = 0; i < 5; i++) {
if (accounts[i] != 0) {
printf("Account 000%d: %f\n", i, avail_bal[i]);
}
}
}
void deposit()
{
float add;
int acc_num;
char c;
char s[100];
puts("Which account do you want to deposit money in?");
c = getc(stdin);
getc(stdin);
acc_num = (int) c - 48;
printf("Amount to deposit: ");
// to get a number without scanf() we have to read the input as a string
// (fgets() adds a '\0' at the end, so keep a seat free for it)
fgets(s, 99, stdin);
// and convert it to a double (atof() only for brevity, use strtod() instead)
add = atof(s);
avail_bal[acc_num] += add;
printf("Amount deposited %f\n", add);
}
void withdraw()
{
int acc_num;
float withdraw;
char c;
char s[100];
// all checks ommitted!
puts("Account to withdraw from: ");
c = getc(stdin);
getc(stdin);
acc_num = (int) c - 48;
puts("Amount to withdraw from account: ");
fgets(s, 99, stdin);
withdraw = atof(s);
avail_bal[acc_num] -= withdraw;
printf("Current balance for account %d: %f\n", acc_num, avail_bal[acc_num]);
}
void pexit()
{
// place logic to save data here or use a function triggered by atexit() for that task
puts("Imagine all of your data would have been put in a safe place!");
exit(EXIT_SUCCESS);
}
Just replace the constructs with scanf before you pass your work for grading.
If you're using integer values as options why you are using character's
#include <stdio.h>
char name[20];
float avail_bal;
void options();
void open();
void list();
void deposit();
void withdraw();
void exit();
int main(void)
{
int option;
printf("****Banking System WELCOME****\n");
void options();
printf("Enter 1-5 of the following options: \n");
scanf("%d",&option);
switch(option)
{
case 1: open();
break;
case 2: list();
break;
case 3: deposit();
break;
case 4: withdraw();
break;
case 5: return 0;
default: exit();
break;
}
return 0;
}
void options()
{
printf("1. Open Account\n");
printf("2. List Accounts\n");
printf("3. Deposit\n");
printf("4. Withdraw\n");
printf("5. Exit");
}
void open()
{
float avail_bal = 0;
char name[20];
int acc_num;
printf("Open new account(enter number 1-5)\n\n");
scanf("%d", &acc_num);
printf("Account number: %d\n");
printf("Available balance: %f\n");
}
void list()
{
}
void deposit()
{
float add;
int acc_num;
printf("Which count do you want to deposit money in?");
scanf(" %d", &acc_num);
printf("Amount to deposit: ");
scanf("%f", &add);
while()
{
}
}
void withdraw()
{
int acc_num;
float withdraw;
printf("Account to withdraw from: ");
scanf("%d", &acc_num);
printf("Amount to withdraw from account: ")
scanf("%f", &withdraw);
while()
{
printf("Current balance for account %d: %f ");
break;
} acc_num++
}