#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
int main(void){
float lp=0;
float cp=0;
char rc[5];
int yes=0;
int country=0;
char correct[5];
int mod=0;
float FunLength(float *lp);
printf("Please follow the steps:\n")
printf("------------------------------------------------ 1\n");
printf("Enter Lenght:\n");
float FunLength(float *lp);
do{
printf("-------------------------------------------------2\n");
printf("do you know the circumference?: \n");
scanf("%s",rc);
if(!strcmp(rc,"yes")){
yes=1;
break;
}
if(!strcmp(rc,"no")){
yes=2;
break;
}
else{
printf("You have to write 'yes' or 'no'\n");
printf("it's not difficult, commit yourself and\n");
printf("you will make it.\n");
}
}while(yes==0);
if(yes==1){
do{
printf("-----------------------------------------------2.5\n");
printf("Enter circumference: ");
scanf("%f",&cp);
if(cp>13){
printf("tray again");
}
if(cp<8){
printf("try again");
}
}while(cp<8 || cp>13);
}
printf("-----------------------------------------------3\n");
printf("Select country:\n");
printf("1.Austria\n2.Belgium\n3.Bulgaria\n4.Cyprus\n5.Croatia\n");
do{
printf("N° ");
scanf("%d",&country);
if(country>5){
printf("Wrong value\n");
printf("Try Again\n");
}
}while(country==0 || country>5);
printf("Summary:\n");
printf("Length: %fcm\n",lp);
if(yes==1){
printf("Circumference: %fcm\n",cp);
}
else if(country==1){
printf("Country: Austria");
}
else if(country==2){
printf("Country: Belgium");
}
else if(country==3){
printf("Country: Bulgaria");
}
else if(country==4){
printf("Country: Cyprus");
}
else if(country==5){
printf("Country: Croatia");
}
printf("Is it correct?: ");
scanf("%s",correct);
if(!strcmp(correct,"no")){
printf("What do you want to change?:\n");
printf("1.Length 2.Circumference 3.Country\n");
do{
printf("N° ");
scanf("%d",&mod);
if(mod>3){
printf("Wrong Value\n");
printf("Try Again\n");
}
}while(mod==0 || mod>3);
}
return 0;
}
float FunLength(float *lp){
float temp=0;
printf("Enter Length::\n");
do{
scanf("%f",&temp);
*lp=temp;
if(*lp>22){
printf("Try Again\n");
}
if(*lp<8){
printf("Try Again\n");
}
}while(*lp>22 || *lp<8);
return *lp;
}
So that's my code, that compare user sizes with european average sizes. I Would use function and structure. I think I'm gonna use structure with the comparison with every nation. Right now I would use function to make the program more "reusable". The problem is that the following function float FunLength(float *lp) is not working. What am I doing wrong? Also I wanna do the same thing with point 2 and 3, using a function. Please help me, and if you have some advice for the current code or for continuing it, i will appreciate.
conflicting types for ‘FunLength’ code.c:16:10: note: previous declaration of ‘FunLength’ was here
In your code example, you are prototyping FunLength() twice. Remove one or the other:
float FunLength(float *lp);/// here
printf("Please follow the steps:\n")
printf("------------------------------------------------ 1\n");
printf("Enter Lenght:\n");
float FunLength(float *lp);///and here
And then, per the conversation in comments, make all definitions consistent with each other, and such that they support what you are doing:
Change the following:
float FunLength(float *lp);//prototype
To:
void FunLength(float *lp);
And:
float FunLength(float *lp){//function definition
To:
void FunLength(float *lp){
And because the function is now void, remove its return statement:
//return *lp;
}
but the function it is as if it were not performed.
FunLength() is not performed because it is not called anywhere. Once you decide where you will call it, call it like this:
(simplified main() function)
int main(void)
{
float val = 0.0;
FunLength(&val);
printf("%f\n", val);
return 0;
}
Related
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char choice;
struct {
char name[20];
int rn;
}read,write;
void wr(){
FILE *fp;
fp = fopen("record.txt","a+");
printf("Enter Your Name: ");
scanf("%s",write.name);
printf("Enter Your Roll Number: ");
scanf("%d",&write.rn);
fprintf(fp,"%s\t%d",write.name,write.rn);
fclose(fp);
menu();
}
void rd(){
FILE *fp;
fp = fopen("record.txt","r");
while(fscanf(fp,"%s %d",read.name,&read.rn)!= EOF){
printf("%s\t%d\n",read.name,read.rn);
}
fclose(fp);
getche();
menu();
}
void menu(){
system("cls");
printf("Do you want to read/write/exit the data R/W/E: ");
scanf("%c",&choice);
tolower(choice);
if(choice == 'w'){
wr();
}
else if(choice == 'r'){
rd();
}
else if(choice == 'e'){
exit(1);
}
}
int main() {
menu();
getche();
system("cls");
return 0;
}
I want to terminate it when user enters 'e' but it terminates after every information entry as well as I read data.
Means after every time read or write function works and the program should ask again about the choice it terminates.
You need to use a for or a while loop like so:
int main()
{
while(1) // while true
{
menu();
getche();
system("cls");
}
return 0;
}
You are already calling exit(1) in your else if of menu(), so add the while(true) and it will work as you want it to.
I want to create an edit function that receives as a parameter by reference the vector of songs. (using pointers)
The user must choose the song number and re-enter the data of that position of the vector.
I created the struct, I am already receiving the values and I am playing. But I do not know how to edit. Anyone to help me start this part?
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <locale.h>
#include <string.h>
struct registry_of_music {
char name[50];
char artist[60];
char url[80];
};
struct registry_of_music music[9];
int main() {
int i;
printf("\nRegistry of Music\n\n\n");
for(i = 0; i <= 3;i++ ){
printf("Name of Music: ");
fflush(stdin);
fgets(music[i].name, 50, stdin);
printf("Name of Artist: ");
fflush(stdin);
fgets(music[i].artist, 60, stdin);
printf("URL of Internet: ");
fflush(stdin);
fgets(music[i].url, 80, stdin);
}
int op;
do
{
printf("1 - Play\n");
printf("2 - Edit\n");
printf("3 - Exit\n");
printf("Please enter a value:");
scanf("%d", &op);
switch(op) {
case 1: play();
break;
case 2: edit();
break;
case 3: printf("Bye\n");
break;
default: printf("Try Again\n");
}
} while (op!=3);
getch();
return(0);
}
void play(){
int i;
for(i = 0; i <= 3;i++ ){
printf("Name ...........: %s", music[i].name);
printf("Artist .....: %s", music[i].artist);
printf("URL .....: %s", music[i].url);
}
}
void edit(){}
The «fill instance of structure» action is absolutely identical if performing on uninitialized structure or initialized. Even if an instance is not initialized, it has some rubbish values in its fields.
On the other hand there is no way to specify default value which will be shown in fgets's prompt and will be available for keyboard edit, unless you're using much more complicated (and NOT included in ISO C standard) tools.
I'm brand new to C and I'm trying to figure out what in the world is causing this. Another similar question said that I had to download another library but that hasn't fixed the issue. So, hopefully someone can spot my problem.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum Subject {SER=0, EGR=1, CSE=2, EEE=3} subject;
struct Course {
enum Subject subject;
int number;
char teacher[1024];
int hours;
} *course;
//place to store course information
struct Course* CourseCollection = NULL;
//number of courses in the collection. also the index of the next empty element.
int courseCount = 0;
void branching(char option);
void course_insert(struct Course course);
int main() {
char input_buffer;
printf("Welcome to ASU Class Schedule\n");
//menu and input loop
do {
printf("\nMenu Options\n");
printf("------------------------------------------------------\n");
printf("a: Add a class\n");
printf("d: Drop a class\n");
printf("s: Show your classes\n");
printf("q: Quit\n");
printf("\nTotal Credits: %d\n\n", courseCount);
printf("Please enter a choice ---> ");
scanf(" %c", &input_buffer);
branching(input_buffer);
} while (input_buffer != 'q');
return 0;
}
//takes a character representing an inputs menu choice and calls the appropriate
//function to fulfill that choice. display an error message if the character is
//not recognized.
void branching(char option) {
int prefix, courseNum, credits;
char instructor;
struct Course course1;
switch(option) {
case 'a' :
printf("Adding a class");
printf("\nWhat is the subject (SER=0, EGR=1, CSE=2, EEE=3)? ");
scanf(" %d", &prefix);
course1.subject = prefix;
printf("\nWhat is the course number (e.g. 334)? ");
scanf(" %d", &courseNum);
course1.number = courseNum;
printf("\nHow many credits is the class? ");
scanf(" %d", &credits);
course1.hours = credits;
printf("\nWhat is the name of the teacher? ");
scanf(" %s", &instructor);
strlcpy(course1.teacher, instructor, 1024);
printf(" %s %d", course1.subject, course1.number);
courseCount++;
course_insert(course1);
break;
case 'd' :
// TODO
break;
case 's' :
// TODO
break;
case 'q' :
printf("Goodbye ");
break;
default :
printf("Error: Invalid Input. Please Try Again. ");
break;
}
void course_insert(struct Course course) {
CourseCollection = malloc(sizeof(course)*courseCount);
}
}
The problem is a syntactical bug; the function definition for course_insert() is inside the curly braces of the function definition of branching(). You need to fix the curly braces:
void branching (char option)
{
// Code for function
}
void course_insert(struct Course course)
{
CourseCollection = malloc(sizeof(course)*courseCount);
}
I'm starting coding this week so I'm dummy about it. I need help about return to main in my script. For example when I done Course registration part I can't return menu program is crashing
Codes:
#include <stdafx.h>
#include <stdio.h>
void eng();
void menu();
void huh();
int main()
{
menu();
return 0;
}
void menu()
{
int menu1choice;
printf("Menu\n");
printf("\n");
printf("1. Student Registration\n");
printf("2. Show Students.\n");
printf("Please enter number: ");
scanf_s("%d", &menu1choice);
switch (menu1choice)
{
case 1:
{
eng();
break;
}
}
}
void eng()
{
int a = 5;
char name[30];
printf("1.Student Number: ");
scanf_s("%d", &a);
//student number
printf("2.Name: ");
scanf_s("%s", &name);
//student name
getchar();
}
void huh()
{
int a = 5;
char name[30];
printf("Your Student number: %d\n", a);
printf("Your Name: %s\n", name);
//result
getchar();
}
Pls help me write return code lines, Thanks in Advance
Here is some code that may help you understand how to the mechanics of functions returning values, in your case back to the main function.
As for some advice, please read up about magic numbers and specifically why they are bad.
/*
* 35917794_main.c
*/
#include <stdio.h>
#define STU_REG 1
#define STU_SHOW 2
#define EXIT_SUCCESS 0
unsigned int show_menu(void);
unsigned int main
(
unsigned int argc,
unsigned char *arg[]
)
{
unsigned int menu1choice;
/*
* The next statements says run the function show_menu() and put the returned
* result in the variable menu1choice.
*/
menu1choice = show_menu();
switch(menu1choice)
{
case (STU_REG):
{
printf("\nGo do STUDENT REGISTRATION things...\n\n");
break;
}
case STU_SHOW:
{
printf("\nGo do SHOW STUDENT things...\n\n");
break;
}
default:
{
printf("\nGo do something for invalid option...\n\n");
break;
}
}
return(EXIT_SUCCESS);
}
unsigned int show_menu
(
void
)
{
unsigned int ui_W0;
printf("Menu\n\n");
printf("1. Student Registration\n");
printf("2. Show Students.\n");
printf("Please enter number: ");
scanf("%d", &ui_W0);
/*
* The next statements says run the function show_menu() has finished and returned
* returns the result in the variable ui_W0.
*/
return(ui_W0);
}
// I'm having issues compiling my program. Honestly, i'm a new programmer and i'm not really sure how to use certain things within my program. Can someone check my program and give me the corrections please or better thoroghly explain it ti me? It's neede for monday..//
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <time.h>
int main()
{
//File declarations//
FILE*Log;
FILE*Inventory;
FILE*Username;
char fpass_word[10]="invent";
char fusername[10]="";
//Declarations for variables//
int main;
int sub_main;
int s=0;
int s_goods;
int p_goods;
int in_goods;
int c_goods;
double Unit_price;
int item_quantity;
int invoice_num;
char pass_word1[10]=" ";
char pass_word[10]="invent";
char username[10]="inventor";
char username1[10]=" ";
char Supplier[12]=" ";
char Items_name[12]=" ";
char Invoice_date[10]=" ";
//Declarations for variables//
int u=0;
int count=0;
int option;
int choice;
int choice1;
int m=0;
int Save;
int New_inventory;
int Update_inventory;
int Print;
int Close_Program;
int t_sales;
int t_purchases;
double m_sales[4]={30000.00,50000.00,100000.00,120000.00};
}
Log=fopen("Invent.txt","w")
if(Log==NULL)
printf("File does not exist");
}
else
{
fprintf(Log,"%s",pass_word);
fclose(Log);
}
user=fopen("Username.txt","w")
if(user==NULL)
{
printf("File does not exist");
}
else
{
fprintf(user,"%s",username);
fclose(user);
}
printf("__________________________________________________________\n\n");
printf("************Please login to your account!************\n\n");
printf("__________________________________________________________\n\n");
printf("Please enter your username: \n");
scanf("%s",username);
user=fopen("Username.txt","r")
{
if(user==NULL)
{
printf("File does not exist");
}
else
{
fprintf(user,"%s",fusername);
fclose(user);
}
choice1=strncmp(username,fusername,10);
printf("Please enter password: \n");
scanf("%s",pass_word1);
Log=fopen("Invent.txt","r")
if(Log==NULL)
{
printf("File does not exist");
}
else
{
fscanf(Log,"%s",fpass_word);
fclose(Log);
}
choice=strncmp(pass_word1,fpass_word,10);
while (choice!=0 && count<3)
{
printf("*************************************************************************************\n\n");
printf("!!!!!!!!!!!!!!!!!!!!!!!!Please re-enter your login info!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf("*************************************************************************************\n\n");
printf(" Please enter username: \n");
scanf("%s",&username);
choice=strncmp(username,username1,10);
printf("Please enter password!\npassword:");
scanf("%s",pass_word);
choice=strncmp(pass_word,pass_word1,10);
count=count++;
//menu function!!!
getch();
system("cls");
}//login page
printf("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
printf("\n******************** Welcome to the INVENT BIZ main page!********************\n\n");
printf("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n");
printf (">>>>>>>>>>>>>>> Please select an option you desire:<<<<<<<<<<<<<<<\n\n");
printf("1: New inventory\n");
printf("2: Update inventory\n");
printf("3: Print\n");
printf("4: Save \n");
printf("5: Close Program\n");
printf("Please select an option: \n");
scanf("%d",&option);
//menu screen
while(option!=6)
{
switch(main)
{
case 1:
printf("New inventory");
Inventory=fopen("New Inventory.txt","w")
if (Inventory==NULL)
printf("This File is empty!");
}
else
{
fprintf("Please enter invoice date:\t\n");
fprintf("Please enter Supplier:\t\n");
fprintf("Please enter Item name:\t\n");
fprintf("Please enter quantity of items:\t\n");
fprintf(" Please enter invoice number:\t\n");
fprintf("Please enter Unit Price:\t\n");
}
fclose(Inventory);
// Data entered for inventory//
switch(sub-main)
{
case 10:
printf("Please enter sales for each month:%d",t_sales);
printf(" Total Sales\n");
scanf("%d",&t_sales);
break;
case 3:
printf("Print");
Inventory=fopen("New Inventory.txt","r")
fscanf(Inventory,"%d",Invoice_date);
fscanf(Inventory,"%s",Supplier);
fscanf(Inventory,"%s",Items_name);
fscanf(Inventory,"%d",&item_quantity);
fscanf(Inventory,"%d",&invoice_num);
fscanf(Inventory,"%d",&Unit_price);
fclose(Inventory);
break;
case 4:
printf("Save");
Inventory=fopen("Inventory1.txt","w");
if(Inventory==NULL)
{
printf("This file empty!!!");
}
else
{
printf("File saved");
}
fclose(Inventory);
case 5:
printf("Close Program");
exit(main);
break;
} // end switch
system("cls");
printf (">>>>>>>>>>>>>>> Please select an option you desire!!!<<<<<<<<<<<<<<<\n\n");
printf("1: New inventory\n");
printf("2: Update inventory\n");
printf("3: Print\n");
printf("4: Save \n");
printf("Please select an option: \n");
scanf("%d",&option);
}
}
system("cls");
getch();
return ();
}
You should really be more specific and not just throw down an entire program and say you want to someone to clean it up for you.
Thank being said I noticed right away you had a few improperly placed curly braces. Your very first if statement is missing an opening brace and further down you have an unnecessary opening brace after user=fopen("Username.txt", "r")
Beyond that indentation is completely off throughout and your switch statements make it really hard to follow what it is you're trying to accomplish with them.
My advice is to read through the compiler errors and locate each issue one by one, and if you're having a problem you can't solve then be specific and post the errors you're getting with your program.