When I tried to compile the below code I get the above error mentioned, I don't know the cause of it and how to rectify it.Please explain me what is my mistake and suggest me a way to rectify it.
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *f1,*f2;
int i,j,k;
int n;
char a,b,c;
printf("Enter the number of students whose details you wish to enter:\n");
scanf("%d",&n);
typedef struct //structure definition
{
char *che;
che=(char*)malloc(5*sizeof(char)); //1st error.
char *c;
c=(char*)malloc(30*sizeof(char));
int rollno;
int branch;
float gpa;
switch (branch)
{
case 1:
{
che={'E','C','E'};
break();
}
case 2:
{
che={'E','E','E'};
break();
}
case 3:
{
che={'C','S','E'};
break();
}
case 4:
{
che={'C','E'};
break();
}
default :
{
printf("WRONG INPUT\n***********");
}
}
}stud;
stud *details;
details=(stud *)malloc(n*sizeof(stud));
printf("Enter the details of the students as prompted:\n1:ECE \n2:EEE \n3:CSE \n4:CE(Chemical ENginering)");
f1=fopen("details.txt","w");
if (f1 == NULL)
{
printf("I couldn't open the file\n");
goto cha;
}
for(i=0;i<n;i++)
{
printf("\nEnter the name of the number %d student\n",n);
fscanf(f1,"%[^\n]s",&details[i].c);
fflush(stdin);
printf("\nEnter the rollno. of the number %d student\n",n);
fscanf(f1,"%d",&details[i].rollno);
fflush(stdin);
printf("\nEnter the branch code of the number %d student\n",n);
fscanf(f1,"%d",&details[i].branch);
fflush(stdin);
printf("\nEnter the gpa of the number %d student\n",n);
fscanf(f1,"%f",&details[i].gpa);
fflush(stdin);
}
fclose(f1);
f2=fopen("details.txt","r");
printf("\nThe details you entered are:\n");
for(i=0;i<n;i++)
{
printf("\nThe name of the number %d student \n",n);
fprintf(f1,"%s",details[i].c);
fflush(stdout);
printf("The rollno. of the number %d student\n",n);
fprintf(f1,"%d",details[i].rollno);
fflush(stdout);
printf("The branch code of the number %d student\n",n);
fprintf(f1,"%d",details[i].branch);
fflush(stdout);
printf("The gpa of the number %d student\n",n);
fprintf(f1,"%f",details[i].gpa);
fflush(stdout);
}
fclose(f2);
cha :;
}
Errors:
16 3 C:\Users\rkbsh_000\Desktop\Untitled10.c [Error] expected specifier-qualifier-list before 'che'.
I also get the error "does not name a type ".Please explain me about this error too.
The compiler doesn't know that che is a type.Include the appropriate header file.
OR
As you know,The compiler parses the file from top to bottom.Make sure a type is defined BEFORE using it into another.
Related
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 );
case 3 is the troubled zone, an integer array is working, it's fine, but "first" and "last" string arrays aren't working at all. I don't know how to solve it.
I have to use only stdio.h and stdlib.h libraries."first" and "last" are 2d char pointers, I'd like to 20 elements and maximum 30 characters.Adding ,and editing are fine it's working, but "first[temp]=first[temp+1];" this code cause compiler error when I try "strcpy(first[temp],first[temp+1]);" there is no compiler error.
Eventually using strcpy. when I want to delete the strings remain the same, and the integers take the value of 0
int main(){
printf("Welcome to address book ! \n ");
printf("--------------------------------------------------------\n");
int a=0;
int temp=0;
char* first[20][30];
char* last[20][30];
int n[20];
int g=0;
while(a<=1000){
int b=0;
printf("What do you want? \n");
printf("1-New ID\n2-Edit ID\n3-Delete ID\n4-List All ID's\n5-Close the Program\n");
printf("--------------------------------------------------------\n");
scanf("%d",&b);
switch(b){
case 1:
printf("Enter Fist Name \n");
scanf("%s",&first[g]);
printf("Enter Last Name\n");
scanf("%s",&last[g]);
printf("Enter Student Number\n");
scanf("%d",&n[g]);
g++;
a++;
continue;
case 2:
printf("Which user you want to change ?\n");
printf("user sequence :");
scanf("%d",&temp);
temp=temp-1;
printf("Listed First Name : %s\n",first[temp]);
printf("New First Name : ");
scanf("%s",&first[temp]);
printf("Listed Last Name : %s\n",last[temp]);
printf("New Last Name : ");
scanf("%s",&last[temp]);
printf("Listed Number : %d\n",n[temp]);
printf("New Listed Number : ");
scanf("%d",&n[temp]);
temp=0;
a++;
continue;
case 3:
printf("which user do you want to delete :");
scanf("%d",&temp);
if(temp+1<=g){
// first[temp]=first[temp+1];
// last[temp]=last[temp+1];
n[temp]=n[temp+1];
}
else
printf("unavailable");
a++;
continue;
case 4:
printf("--------------------------------------------------------\n");
int u;
for(u=0;u<g;u++){
printf(">%d.user. ",u+1);
printf("%d\t",n[u]);
printf("%s\t",first[u]);
printf("%s\n",last[u]);
}
printf("\n--------------------------------------------------------\n");
a++;
continue;
case 5:
printf("\nThank you for the use this program !\n ");
temp= 1000-a;
a= a+temp;
a++;
continue;
}
}
return 0;}
Here's my code, sorry if it is beginners type of program. I was not able to fully enter classes this finals so i don't really know that much about structures. Please help :(
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void add();
void search();
void update();
void bmi();
int age();
void display();
struct rec{
char fn[30], mn[30], ln[30], gen[30];
int month, date, year;
float height, weight;
};
void main() {
int i;
printf("1 - Add Records\n");
printf("2 - Search Patient\n");
printf("3 - Update Patient Record\n");
printf("4 - Display Saved Records\n");
printf("5 - Calculate Body Mass Index\n");
printf("6 - Calculate Age\n");
printf("Enter a number to select an entry:");
scanf("%d",&i);
if(i==1) add();
else printf("Invalid number!");
}
void add(rec){
int a,n;
struct rec patient[50];
FILE *fp;
fp=fopen("recordsfinals.txt","a");
if(fp==NULL){
printf("File does not exist!");
}
printf("Enter number of entries to append:");
scanf("%d",&a);
for(n=1; n<=a; n++){
printf("Enter Firstname:");
scanf("%c",&patient.fn);
printf("\nEnter middle name:");
scanf("%c",&patient.mn);
printf("\nEnter last name:");
scanf("%c",&patient.ln);
printf("\nEnter gender:");
scanf("%c",&patient.gen);
printf("\nEnter Date of Birth\n Month:");
scanf("%",&patient.month);
printf("\nEnter Date:");
scanf("%d",&patient.date);
printf("\nEnter Year of birth:");
scanf("%d",&patient.year);
printf("Enter height:");
scanf("%f",&patient.height);
printf("Enter weight:");
scanf("%f",&patient.weight);
fprintf(fp,"%c, %c, %c, %c, %d, %d, %d, %f, %f", patient.fn, patient.mn, patient.ln, patient.gen, patient.month, patient.date, patient.year, patient.height, patient.weight);
}
fclose(fp);
}
Not only height but other variables have the same problems as well.
The compiler is telling you that this type of expression isn't valid:
patient.height
when patient is an array. And patient is clearly an array:
struct rec patient[50];
You cannot treat an array as if it were a structure or union. You need to access one if its elements. For instance
patient[0].height
I would like to ask why am i getting Run-time Check Failure #2 When i am doing my program?
I'm very new to C programming.
I'm trying to make a Console application that have some option after they key in Y/N,
But whenever i reach the end of all the option i get that error.
Could anyone tell me how i could solve it & what is the proper way of doing this kind of programming?
#define _CRT_SECURE_NO_WARNINGS // To allow Visual studio to use "scanf" function
#include <stdio.h> // Standard Input output . header
#include <Windows.h>
void codername() {
printf("Coder: Rong Yuan\n");
}
void projectname() {
printf("Project name: NPoly Learning\n");
}
void loadcurrentdate() {
SYSTEMTIME str_t;
GetSystemTime(&str_t);
printf("Date: %d . %d . %d \n"
, str_t.wDay, str_t.wMonth, str_t.wYear);
}
int main() {
char option;
int input;
int mincome, fmember, total;
printf("Do you like to see our option? Y/N \n");
scanf("%s", &option);
if (option == 'y' || option == 'Y') {
printf("1. Display Coder Detail\n");
printf("2. Display Project Name\n");
printf("3. Load Current Date\n");
printf("4. Calculator PCI\n");
printf("5. Exit\n");
scanf("%d", &input);
}
else
exit(1);
switch (input) {
case 1:
codername();
printf("Do you like to return to main?");
break;
case 2:
projectname();
break;
case 3:
loadcurrentdate();
break;
case 4:
printf("Enter your house monthly income: ");
scanf("%d", &mincome);
printf("Enter total family member: (INCLUDING YOURSELF) ");
scanf("%d", &fmember);
total = mincome / fmember;
printf("Total PCI: %d / %d = %d \n", mincome, fmember, total);
system("pause");
break;
case 5:
exit(0);
}
}
scanf("%s", &option);
is wrong as option is a char . So replace %s with %c there.%s should be used for strings (array of characters) and %c is the format specifier used for a character.