In function ‘Calculate_Duty’: error: nested functions are disabled, use -fnested-functions to re-enable - switch-statement

include
/* Declare function prototypes */
float Calculate_Duty (int, int);
void Print_Duty (float);
int main (void)
{
/* Declare all variables to be used in the program */
char more_to_process;
int origin, category, quantity, num_ship=0;
float unit_price;
float cost, duty, total_ship=0;
float total_duty=0, tax_rate=0;
/* Begin to execute the program */
printf("Do you have more customs forms to process? Type: Y for yes or N for no \n");
scanf(" %c", &more_to_process);
while ((more_to_process =='Y') && (more_to_process!='N'))
{
printf ("What is the origin of the goods? Type: 1 for US, 2 for China, 3 for Brazil \n");
scanf ("%d", &origin);
printf ("What category of goods? Type: 1 for food, 2 for clothing, 3 for wood \n");
scanf ("%d", &category);
printf ("What is the quantity? \n");
scanf ("%d", &quantity);
printf ("What is the unit price? \n");
scanf ("%f", &unit_price);
total_ship= quantity * unit_price;
/* Calculate the duty of the shipment */
tax_rate= Calculate_Duty (origin, category);
duty= tax_rate * total_ship;
total_duty+= duty;
/* Print the duty of the shipment */
printf ("Origin \t Category \t Quantity \t Unit Price \t Shipment \t Tax Rate \t Duty \t \n");
printf ("%d \t %d \t \t %d \t \t %02.f \t \t %0.2f \t %0.2f \t \t %0.2f \t \n", origin,
category, quantity, unit_price, total_ship, tax_rate, duty);
Print_Duty (duty);
printf ("Do you have more customs forms to process? Type: Y for yes, N for no \n");
scanf (" %c", &more_to_process);
++num_ship;
}
printf ("Transaction Summary: \n");
printf ("Number of Shipments Processed = %d \n", num_ship);
printf ("Total Duties Collected = $ %0.2f \n", total_duty);
return 0;
}
/* Perform the function Calculate_Duty */
float Calculate_Duty (int origin, int category)
{
float duty;
switch (origin)
{
/* Case 1 is for US */
case 1:
switch (category)
{
case 1:
duty=0;
break;
case 2:
duty= 0;
break;
case 3:
duty= .05;
break;
}
break;
/* Case 2 is for China */
case 2:
switch (category)
{
case 1:
duty= .02;
break;
case 2:
duty= .03;
break;
case 3:
duty= .04;
break;
}
break;
/* Case 3 is for Brazil */
case 3:
switch (category)
{
case 1:
duty= .01;
break;
case 2:
duty= .02;
break;
case 3:
duty= .08;
break;
}
return duty;
}
/* Perform the Print_Duty function */
void Print_Duty (float duty)
{
printf ("The amount due is $ %0.2f \n", duty);
}

As I can see one } is missing from switch(origin), before return duty.

Related

How do i create an option in switch case?

I've recently tried to make some switch case function. Now, I'm currently trying to make an option but it seems it always skips the if part in case 1. The input just doesn't go in the if - else part.
#include <stdio.h>
int main(){
int option;
char choice;
int celcius;
menu:
printf("Welcome to program.\n");
printf("1. Input celcius\n");
printf("2. Convert To Kelvin\n");
printf("3. Convert To Fahrenheit\n");
printf("4. Convert To Reamur\n");
scanf("%d", &option);
switch(option){
case 1:
scanf("%d", &celcius);
printf("The value is : %d\n", celcius);
printf("Continue? : (y/n)\n"); scanf("%c", &choice);getchar();
if(choice == 'y' || choice == 'Y'){
goto menu;
}else if(choice == 'n' || choice == 'N');
break;
}
case 2:
printf("Please input the value: "); scanf("%d", &celcius);
int kelvin = celcius + 273;
printf("Converted Value is : %d\n", kelvin);
break;
case 3:
printf("Please input the value: "); scanf("%d", &celcius);
int fahrenheit = celcius + 32;
printf("Converted Value is : %d\n", fahrenheit);
break;
case 4:
printf("Please input the value: "); scanf("%d", &celcius);
int reamur = 5/4 * celcius;
printf("Converted Value is : %d\n", reamur);
break;
default:
printf("Wrong input.\n");
}
return 0;
}
(incomplete code because currently trying to make the case 1 works.)
Any solutions will help : )
You have a getchar() after the scanf() for choice. This is reading the newline character that is left in the input buffer after the scanf() for option.
You need to remove the getchar() and change the scanf() for choice to:
scanf(" %c", &choice);
The space before the %c will skip any whitespace characters in the input buffer.

Using Switch and JOptionPane

Problem: Ask the user to enter 0-9999 and convert it into words using switch case and JOptionPane...
My question: I figure out how to convert the other numbers but if I entered 11-19, the output for example I enter 11 - "Ten One" how can I get the right output for this?
public class NumbertoWords {​​
/**
* #param args the command line arguments
*/
public static void main(String[] args) {​​​​​​​​​
// TODO code application logic here
int num=Integer.parseInt(JOptionPane.showInputDialog("Enter a Number from [0-9999]"));
int thou=num/1000;
int temp=num%1000; // 9999
int hun=temp/100; //999 -> 9
int temp1=temp%100;
int tens=temp1/10; //99
int ones=temp%10;
String display="";
switch(thou){​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
case 9: display=" Nine Thousand"; break;
case 8: display=" Eight Thousand"; break;
}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
switch (hun){​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
case 9: display=display+ " Nine Hundred"; break;
case 8: display=display+ " Eight Hundred"; break;
}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
switch (tens){​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
case 9: display=display+ " Ninety"; break;
}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
switch (ones){​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
case 9: display=display+ " Nine"; break;
}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
JOptionPane.showMessageDialog(null, display,"Number to Words",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

How to read parts [duplicate]

This question already has answers here:
Why is “while( !feof(file) )” always wrong?
(5 answers)
Closed 2 years ago.
I have a problem with this program: I was able to make another draft program but it only reads the first three lines and stops. Unfortunately, the input file is not symmetrical so I should use while loops for some parts only to avoid repeating parts of the program. With strcpy I could copy the work done up to a certain point and then continue from that point on. I was trying but visual studio tells me to move the memory to the heap but I don't know how to do it ... Practically I should only extrapolate certain data from the input file and order them in an output file according to a precise line.
I also know there's a problemi with char buf[200], rec[200] because I have to allocate much more memory...
the first time in
while ( !feof(fd) ) {
fscanf(fd, "session %d (COPY MODE):\n\n", &session);
you call feof before any read so feof cannot work proprely, an easy change is to do
while (fscanf(fd, "session %d (COPY MODE):\n\n", &session) == 1) {
Out of that the mode can be something else that COPY MODE and the two \n after are not mandatory, you also do not check your next fscanf success and globaly the algorithm you use is wrong
A possible way to do the job also checking the validity of the input file is :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE * fp = fopen("summary.txt", "r");
if (fp == NULL) {
perror("cannot open summary.txt");
exit(1);
}
int session;
char mode[64];
const char * separator = "_____________________________________________";
while (fscanf(fp, " session %d (%[^)]):", &session, mode) == 2) {
if (strcmp(mode, "COPY MODE") && strcmp(mode,"ONLINE FREE MODE"))
{
fprintf(stderr, "invalid file, invalid mode '%s'\n", mode);
exit(1);
}
char line[64];
char given[64];
char recognized[64];
int registration, registration_set = 0;
int sequences, sequences_set = 0;
for(;;) {
/* read next line, bypass possible newline(s) before */
if (fscanf(fp, " %63[^\n]", line) != 1) {
if (!feof(fp)) {
perror("reading file");
exit(1);
}
*line = 0;
}
else if (sscanf(line, "number of sequences: %d", &sequences) == 1) {
sequences_set = 1;
continue;
}
else if ((*given == 0) &&
(sscanf(line, "characters given: %63s", given) == 1)) {
if (!registration_set) {
fprintf(stderr,
"registration is missing before 'characters given: %s'\n",
given);
exit(1);
}
if (!sequences_set) {
fprintf(stderr,
"sequences is missing before 'characters given: %s'\n",
given);
exit(1);
}
continue;
}
else if ((*recognized == 0) &&
(sscanf(line, "characters recognized: %63s", recognized) == 1)) {
if (!*given) {
fprintf(stderr,
"given is missing before 'characters recognized: %s'\n",
recognized);
exit(1);
}
continue;
}
if (registration_set) {
if (!*given) {
fputs("invalid file, given is missing\n", stderr);
exit(1);
}
printf("%d %d %d %s %s\n", session, registration, sequences, given, recognized);
if (!*line || !strcmp(line, separator))
break;
}
if (sscanf(line, "registration %d:", &registration) != 1) {
fprintf(stderr,
"invalid file, 'registration <n>:' expected rather than '%s'\n",
line);
exit(1);
}
else {
registration_set = 1;
*given = *recognized = 0;
}
}
}
fclose(fp);
return 0;
}
Note the space at the beginning of the format of the fscanf, that allows to bypass character considered to be space including the newline
Compilation and execution :
pi#raspberrypi:/tmp $ gcc -Wall c.c
pi#raspberrypi:/tmp $ cat summary.txt
session 1 (COPY MODE):
number of sequences: 15
registration 1:
characters given: CALOR
registration 2:
characters given: CARINO
registration 3:
characters given: SUSHI
_____________________________________________
session 2 (COPY MODE):
registration 1:
number of sequences: 15
characters given: SUSHI
characters recognized: SUSHI
_____________________________________________
session 3 (ONLINE FREE MODE):
number of sequences: 15
registration 1:
characters given: PERA
characters recognized: PFRA
registration 2:
characters given: SALON
characters recognized: SALON
registration 3:
characters given: PERRO
characters recognized: PERRO
_____________________________________________
session 4 (ONLINE FREE MODE):
registration 1:
number of sequences: 7
characters given: TORTUGA
characters recognized: TORTUGA
registration 2:
number of sequences: 4
characters given: ANAEROBIO
characters recognized: ANAERPBIO
registration 3:
number of sequences: 4
characters given: PAPELES
characters recognized: PAPELEX
pi#raspberrypi:/tmp $ ./a.out
1 1 15 CALOR
1 2 15 CARINO
1 3 15 SUSHI
2 1 15 SUSHI SUSHI
3 1 15 PERA PFRA
3 2 15 SALON SALON
3 3 15 PERRO PERRO
4 1 7 TORTUGA TORTUGA
4 2 4 ANAEROBIO ANAERPBIO
4 3 4 PAPELES PAPELEX
pi#raspberrypi:/tmp $

scanning a line has strings and integer C

I have a project and im facing a problem in this code
void Show() {
system("cls");
FILE *AddedSc;
int sc1i,sc2i;
int sc1ii,sc2ii;
char TNi[100],TN2i[100];
char TNii[100],TN2ii[100];
AddedSc = fopen("addedscores.txt", "r");
printf(" - Choose teams from this list: \n\n");
printf(" 1. Brazil\n 2. Germany\n 3. Italy\n 4. KSA\n 5. Portogual\n 6. Australia\n 7. USA\n 8. Spin\n 9. England\n 10. Korea\n\n\n");
printf(" * Enter the two teams that you want to show their results: ");
printf("\n\n\nTeam A: ");
scanf("%s", TNi);
printf("Team B: ");
scanf("%s", TN2i);
while (fscanf(AddedSc,"%[^\n] %d %d %[^\n]", TNii, sc1ii, sc2ii,TN2ii) !=EOF) {
if (strcmp(TNi, TNii) == 0 && strcmp(TN2i, TN2ii) == 0)
printf("\n\n%s %d - %d %s", TNi, sc1ii, sc2ii, TN2i);
else if(strcmp(TN2i, TNii) == 0 && strcmp(TNi, TN2ii) == 0)
printf("\n\n%s %d - %d %s", TN2i, sc2ii, sc1ii, TNi);
else printf("Not found!");
}
fclose(AddedSc);
there is a file contains data like this
usa 2 0 italy
I want the user to enter the name of two teams and let the program to search in the file and compare what the user entered and print the result of the match on the screen ..
please help me in this....
The problem is the first format, %[^\n], which will eat up all the lines in the input. You can change it just %s as follows:
while (fscanf(AddedSc,"%s %d %d %s", TNii, sc1ii, sc2ii,TN2ii) != EOF)
And it will be safer to compare the result as:
while (fscanf(AddedSc,"%s %d %d %s", TNii, sc1ii, sc2ii,TN2ii) == 4)

How can I safely and simply read a line of text from a file or stdin?

Given that fgets only sometimes includes a linebreak, and fscanf is inherently unsafe, I would like a simple alternative to read text line-by-line from a file. Is this page a good place to find such a function?
Yes. The following function should satisfy this requirement without creating any damaging security flaws.
/* reads from [stream] into [buffer] until terminated by
* \r, \n or EOF, or [lastnullindex] is reached. Returns
* the number of characters read excluding the terminating
* character. [lastnullindex] refers to the uppermost index
* of the [buffer] array. If an error occurs or non-text
* characters (below space ' ' or above tilde '~') are
* detected, the buffer will be emptied and 0 returned.
*/
int readline(FILE *stream, char *buffer, int lastnullindex) {
if (!stream) return 0;
if (!buffer) return 0;
if (lastnullindex < 0) return 0;
int inch = EOF;
int chi = 0;
while (chi < lastnullindex) {
inch = fgetc(stream);
if (inch == EOF || inch == '\n' || inch == '\r') {
buffer[chi] = '\0';
break;
} else if (inch >= ' ' && inch <= '~') {
buffer[chi] = (char)inch;
chi++;
} else {
buffer[0] = '\0';
return 0;
}
}
if (chi < 0 || chi > lastnullindex) {
buffer[0] = '\0';
return 0;
} else {
buffer[chi] = '\0';
return chi;
}
}

Resources