Stuck in a C project(Quiz) - c

I am a beginner in C and I am working on a quiz. Everything is going well but I want to count the user's correct answers and display them at the end of the quiz using while or for. I have searched for hours but couldn't find anything. If anyone could help me it will be much appreciated!
Here is my code (the non-english are the questions):
#include <stdio.h>
int main() {
int x, a, answer, B, i;
printf("Καλώς ήρθατε στο quiz/εργασία:)\n");
printf("Αυτό το κουίζ περιέχει ερωτήσεις για χώρες επειδή δεν ήθελα να βάλω μαθηματικά\n");
press:
printf("Πάτα *1* για έναρξη\n");
scanf("%d", &x);
if (x <= 1) {
printf("Πρεπει να απαντησεις συνολικα 8 ερωτησεις\n");
goto start;
} else
if (x > 1) {
goto press;
}
start:
switch (x) {
case 1:
{
printf("ποια ειναι η πρωτεύουσα της Ρουμανίας;:\n");
printf("1=Κραϊοβα, 2=Βουκουρέστη, 3=Βουδαπέστη, 4=Οραντέα\n");
scanf("%d", &answer);
if (answer == 2) {
while (i = 1) {
i += 1;
}
printf("Σωστό!Ειναι η 'Βουδαπέστη.'\n");
} else
if (answer == 1, 3, 4) {
printf("Λαθος.\n");
}
}
case 2:
{
printf("ποια χώρα κατανάλλωσε το περισσότερο τσάι το 2022;:\n");
printf("1=Ιρλανδία, 2=Ηνωμένο Βασίλειο, 3=Τουρκία, 4=Ινδονησία\n");
scanf("%d", &answer);
if (answer == 3) {
while (i <= 1) {
i += 1;
}
printf("Σωστό!Με τον μέσο Τούρκο να καταναλλωνει 3.16kg ετησίως.\n");
} else
if (answer == 1, 2, 4) {
printf("Λαθος.\n");
}
}
case 3:
{
printf("Ποια από τις παρακάτω χώρες έχουν 3 πρωτεύουσες;:\n");
printf("1=Ελβετία, 2=Νότια Αφρική, 3=Κίνα, 4=Αυστραλία\n");
scanf("%d", &answer);
if (answer == 2) {
while (i <= 2) {
i += 1;
}
printf("Σωστό!Ειναι η Νότια Αφρική(1:Κέιπ τάουν, 2:Πρετόρια, 3:Μπλουμφοντέιν.\n");
} else
if (answer == 1, 3, 4) {
printf("Λαθος.\n");
}
}
case 4:
{
printf("Ποιανής χώρας ο εθνικός ύμνος δεν έχει στίχους;:\n");
printf("1=Μάλτα, 2=Νορβηγία, 3=Ισλανδία, 4=Ισπανία\n");
scanf("%d", &answer);
if (answer == 4) {
while (i <= 3) {
i += 1;
}
printf("Σωστό!Ειναι η Ισπανία.\n");
} else
if (answer == 1, 3, 2) {
printf("Λαθος.\n");
}
}
case 5:
{
printf("ποια ειναι η πρωτεύουσα της Εσθονίας;:\n");
printf("1=Ταλίν, 2=Τάρτου, 3=Νάρβα, 4=Ρακβερέ\n");
scanf("%d", &answer);
if (answer == 1) {
while (i <= 4) {
i+=1;
}
printf("Σωστό!Ειναι το Ταλίν.\n");
} else
if (answer == 2, 3, 4) {
printf("Λαθος.\n");
}
}
case 6:
{
printf("Ποια είναι η μικρότερη χώρα της κεντρικής Αμερικής;:\n");
printf("1=Ελ Σαλβαδόρ, 2=Ονδούρα, 3=Νικαράγουα, 4=Γουατεμάλα\n");
scanf("%d", &answer);
if (answer == 1) {
while (i <= 5) {
i += 1;
}
printf("Σωστό!Είναι το Ελ Σαλβαδόρ με έδαφος μόλις 21,041km²\n");
} else
if (answer == 2, 3, 4) {
printf("Λαθος.\n");
}
}
case 7:
{
printf("Ποια χώρα έχει τις περισσότερες ζώνες ώρας;:\n");
printf("1=Ρωσία, 2=ΗΠΑ, 3=Γαλλία, 4=Ηνωμένο Βασίλειο\n");
scanf("%d", &answer);
if (answer == 3) {
while (i <= 6) {
i += 1;
}
printf("Σωστό!Ειναι η Γαλλία με 12 ζώνες ώρας(+παραθαλάσσια εδάφη).\n");
} else
if (answer == 1, 2, 4) {
printf("Λαθος.\n");
}
}
case 8:
{
printf("Ποια ήπειρος φιλοξενεί τις περισσότερες χώρες;:\n");
printf("1=Ευρώπη, 2=Νότια Αμερική, 3=Αφρική, 4=Ασία\n");
scanf("%d", &answer);
if (answer == 3) {
while (i <= 7) {
i += 1;
}
printf("Σωστό!Ειναι η Αφρική με 54 χώρες.\n");
} else
if (answer == 1, 2, 4) {
printf("Λαθος.\n");
}
}
printf("απαντησες σωστα %d\n",i);
}
return 0;
}
I have tried to put the while element in different locations in various forms but nothing worked. The closest I got to "Work" is the answer you answered right- 32767.

Allow me to suggest some other code that simplifies things
First of all, you will find soon that goto is not suggested as best practice.
Below find and explanation of the program:
I start my main program by creating two arrays, one for the questions and one for the right answers.
Then I make a do-while where I loop the question until someone presses 1.
When someone presses 1, i loop as many times as the questions. I find the questions number with sizeof(questions)/sizeof(questions[0]) which divides the whole length of questions by the size of a single question element.
I this for loop i run a function that takes as parameters the question and the right answer and if my input equals the right answer it returns 1, else 0
Returning from the function, I can add the returned value to a variable i declared (iscorrect) which will not be affected if the answer is not correct. But if it is it will be +1.
Ending the loop i have the right answers number in my variable iscorrect
If you wish to have explanations for the right answers you can always add an exrta array an pass it as a parameter to the function, and make it print insted of a simple 'correct' i added for testing.
Functions help you keep your code clean and organized, they also minimise space & time, as you can write it once and call it as many times as you want in your code
Hope I helped
#include <stdio.h>
int AskQuestion (char Question[], int rightanswer){
int answer = "";
printf(Question);
scanf("%d", &answer);
if(answer==rightanswer){
printf("Right\n\n");
return 1;
}
printf("Wrong\n\n");
return 0;
}
int main() {
int x;
char questions[3][250] = {
"Question 1: ..........",
"Question 2: ..........",
"Question 3: .........."
};
int answers[3] = {
1,
2,
1
};
printf("String array Elements are:\n");
printf("Welcome to the quiz\n");
do{
printf("Press *1* to start\n");
scanf("%d", &x);
}
while(x!=1);
int iscorrect =0;
int questionslength = sizeof(questions)/sizeof(questions[0]);
for (int i=0; i<questionslength; i++){
char question[] = "";
int rightanswer = 1;
iscorrect += AskQuestion(questions[i], answers[i]);
}
printf("Right answers %d\n",iscorrect);
return 0;
}

I would recommend first making it as simple as possible. Something like:
#include <stdio.h>
int main()
{
int answer = 0;
int count = 0;
printf("Καλώς ήρθατε στο quiz/εργασία:)\n");
printf(
"Αυτό το κουίζ περιέχει ερωτήσεις για χώρες επειδή δεν ήθελα να βάλω μαθηματικά\n");
printf("ποια ειναι η πρωτεύουσα της Ρουμανίας;:\n");
printf("1=Κραϊοβα, 2=Βουκουρέστη, 3=Βουδαπέστη, 4=Οραντέα\n");
scanf("%d", &answer);
if (answer == 2) {
printf("Σωστό!Ειναι η 'Βουδαπέστη.'\n");
count++;
} else {
printf("Λαθος.\n");
}
printf("ποια χώρα κατανάλλωσε το περισσότερο τσάι το 2022;:\n");
printf("1=Ιρλανδία, 2=Ηνωμένο Βασίλειο, 3=Τουρκία, 4=Ινδονησία\n");
scanf("%d", &answer);
if (answer == 3) {
printf("Σωστό!Με τον μέσο Τούρκο να καταναλλωνει 3.16kg ετησίως.\n");
count++;
} else {
printf("Λαθος.\n");
}
printf("Ποια από τις παρακάτω χώρες έχουν 3 πρωτεύουσες;:\n");
printf("1=Ελβετία, 2=Νότια Αφρική, 3=Κίνα, 4=Αυστραλία\n");
scanf("%d", &answer);
if (answer == 2) {
printf(
"Σωστό!Ειναι η Νότια Αφρική(1:Κέιπ τάουν, 2:Πρετόρια, 3:Μπλουμφοντέιν.\n");
count++;
} else {
printf("Λαθος.\n");
}
printf("Ποιανής χώρας ο εθνικός ύμνος δεν έχει στίχους;:\n");
printf("1=Μάλτα, 2=Νορβηγία, 3=Ισλανδία, 4=Ισπανία\n");
scanf("%d", &answer);
if (answer == 4) {
printf("Σωστό!Ειναι η Ισπανία.\n");
count++;
} else {
printf("Λαθος.\n");
}
printf("ποια ειναι η πρωτεύουσα της Εσθονίας;:\n");
printf("1=Ταλίν, 2=Τάρτου, 3=Νάρβα, 4=Ρακβερέ\n");
scanf("%d", &answer);
if (answer == 1) {
printf("Σωστό!Ειναι το Ταλίν.\n");
count++;
} else {
printf("Λαθος.\n");
}
printf("Ποια είναι η μικρότερη χώρα της κεντρικής Αμερικής;:\n");
printf("1=Ελ Σαλβαδόρ, 2=Ονδούρα, 3=Νικαράγουα, 4=Γουατεμάλα\n");
scanf("%d", &answer);
if (answer == 1) {
printf("Σωστό!Είναι το Ελ Σαλβαδόρ με έδαφος μόλις 21,041km²\n");
count++;
} else {
printf("Λαθος.\n");
}
printf("Ποια χώρα έχει τις περισσότερες ζώνες ώρας;:\n");
printf("1=Ρωσία, 2=ΗΠΑ, 3=Γαλλία, 4=Ηνωμένο Βασίλειο\n");
scanf("%d", &answer);
if (answer == 3) {
printf("Σωστό!Ειναι η Γαλλία με 12 ζώνες ώρας(+παραθαλάσσια εδάφη).\n");
count++;
} else {
printf("Λαθος.\n");
}
printf("Ποια ήπειρος φιλοξενεί τις περισσότερες χώρες;:\n");
printf("1=Ευρώπη, 2=Νότια Αμερική, 3=Αφρική, 4=Ασία\n");
scanf("%d", &answer);
if (answer == 3) {
printf("Σωστό!Ειναι η Αφρική με 54 χώρες.\n");
count++;
} else {
printf("Λαθος.\n");
}
printf("απαντησες σωστα %d\n", count);
return 0;
}

There are multiple problems in the code:
i is not initialized. If you mean to count the number of correct answers, name the variable count and initialize it to 0.
while (i = 1) { i += 1; } is an endless loop as i = 1**sets**ito1instead of comparingito1, which is written i == 1`.
all loops while (i <= 2) { i += 1; } are equivalent to if (i <= 2) { i = 3; }. You should probably just count the number of correct answers.
if (answer == 1, 3, 4) is a constant test that is always true, because the , in the test expression is an operator that evaluates to its second operand. Note that this test is redundant anyway. You should just use else {
the initial iteration using labels and goto statements should be changed.
Here is a modified version:
#include <stdio.h>
int main() {
int x, answer, count = 0;
printf("Καλώς ήρθατε στο quiz/εργασία:)\n");
printf("Αυτό το κουίζ περιέχει ερωτήσεις για χώρες επειδή δεν ήθελα να βάλω μαθηματικά\n");
for (;;) {
printf("Πάτα *1* για έναρξη\n");
if (scanf("%d", &x) != 1)
return 1;
if (x < 1) {
printf("Πρεπει να απαντησεις συνολικα 8 ερωτησεις\n");
} else {
break;
}
}
switch (x) {
case 1:
{
printf("ποια ειναι η πρωτεύουσα της Ρουμανίας;:\n");
printf("1=Κραϊοβα, 2=Βουκουρέστη, 3=Βουδαπέστη, 4=Οραντέα\n");
if (scanf("%d", &answer) != 1)
break;
if (answer == 2) {
count += 1;
printf("Σωστό!Ειναι η 'Βουδαπέστη.'\n");
} else {
printf("Λαθος.\n");
}
}
case 2:
{
printf("ποια χώρα κατανάλλωσε το περισσότερο τσάι το 2022;:\n");
printf("1=Ιρλανδία, 2=Ηνωμένο Βασίλειο, 3=Τουρκία, 4=Ινδονησία\n");
if (scanf("%d", &answer) != 1)
break;
if (answer == 3) {
count += 1;
printf("Σωστό!Με τον μέσο Τούρκο να καταναλλωνει 3.16kg ετησίως.\n");
} else {
printf("Λαθος.\n");
}
}
case 3:
{
printf("Ποια από τις παρακάτω χώρες έχουν 3 πρωτεύουσες;:\n");
printf("1=Ελβετία, 2=Νότια Αφρική, 3=Κίνα, 4=Αυστραλία\n");
if (scanf("%d", &answer) != 1)
break;
if (answer == 2) {
count += 1;
printf("Σωστό!Ειναι η Νότια Αφρική(1:Κέιπ τάουν, 2:Πρετόρια, 3:Μπλουμφοντέιν.\n");
} else {
printf("Λαθος.\n");
}
}
case 4:
{
printf("Ποιανής χώρας ο εθνικός ύμνος δεν έχει στίχους;:\n");
printf("1=Μάλτα, 2=Νορβηγία, 3=Ισλανδία, 4=Ισπανία\n");
if (scanf("%d", &answer) != 1)
break;
if (answer == 4) {
count += 1;
printf("Σωστό!Ειναι η Ισπανία.\n");
} else {
printf("Λαθος.\n");
}
}
case 5:
{
printf("ποια ειναι η πρωτεύουσα της Εσθονίας;:\n");
printf("1=Ταλίν, 2=Τάρτου, 3=Νάρβα, 4=Ρακβερέ\n");
if (scanf("%d", &answer) != 1)
break;
if (answer == 1) {
count += 1;
printf("Σωστό!Ειναι το Ταλίν.\n");
} else {
printf("Λαθος.\n");
}
}
case 6:
{
printf("Ποια είναι η μικρότερη χώρα της κεντρικής Αμερικής;:\n");
printf("1=Ελ Σαλβαδόρ, 2=Ονδούρα, 3=Νικαράγουα, 4=Γουατεμάλα\n");
if (scanf("%d", &answer) != 1)
break;
if (answer == 1) {
count += 1;
printf("Σωστό!Είναι το Ελ Σαλβαδόρ με έδαφος μόλις 21,041km²\n");
} else {
printf("Λαθος.\n");
}
}
case 7:
{
printf("Ποια χώρα έχει τις περισσότερες ζώνες ώρας;:\n");
printf("1=Ρωσία, 2=ΗΠΑ, 3=Γαλλία, 4=Ηνωμένο Βασίλειο\n");
if (scanf("%d", &answer) != 1)
break;
if (answer == 3) {
count += 1;
printf("Σωστό!Ειναι η Γαλλία με 12 ζώνες ώρας(+παραθαλάσσια εδάφη).\n");
} else {
printf("Λαθος.\n");
}
}
case 8:
{
printf("Ποια ήπειρος φιλοξενεί τις περισσότερες χώρες;:\n");
printf("1=Ευρώπη, 2=Νότια Αμερική, 3=Αφρική, 4=Ασία\n");
if (scanf("%d", &answer) != 1)
break;
if (answer == 3) {
count += 1;
printf("Σωστό!Ειναι η Αφρική με 54 χώρες.\n");
} else {
printf("Λαθος.\n");
}
}
}
printf("απαντησες σωστα %d\n", count);
return 0;
}

Related

C program query

So this is my program and the number, name, address doesn't print again after single execution and also If the seat is already taken there should be "seat is taken already, please try again" which I'm confused about
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct passenger
{
char name[20];
char address[30];
int age;
};
struct passenger data;
struct rwcl
{
int row;
char col;
};
int clmn, i, j;
int arr[5][5];
struct rwcl number;
for(i=0;i<5;i++)
{
printf("Enter Your Name: ");
scanf("\n");
gets(data.name);
printf("Enter Your Address: ");
scanf("\n");
gets(data.address);
printf("Enter Your Age: ");
scanf("%d", &data.age);
printf("\nAll aboard! You may now choose your desired seat/s.");
while(i<5){
for(j=0;j<5;j++){
if (j == 0) {
arr[i][j] = i+1;
}
if(j == 1){
arr[i][j] = 'A';
}
if(j== 2){
arr[i][j] = 'B';
}
if(j == 3){
arr[i][j] = 'C';
}
if(j== 4){
arr[i][j] = 'D';
}
}
i++;
}
printrwcl:
printf("\n\n");
for(i=0;i<5;i++){
for(j=0;j<5;++j){
if(j == 0 ){
printf("%-5d", arr[i][j]);
}
else {
printf("%-5c", arr[i][j]);
}
}
if(j==5) {
printf("\n");
}
}
printf("\n");
rowselect:
printf("Choose a row between 1,2,3,4,5 or 6 for cancellation: ");
scanf("%d", &number.row);
if(number.row < 0 || number.row > 6) {
printf("\nPlease, re-enter. Thank you.\n");
goto rowselect;
}
if(number.row == 6) {
printf("Recorded, thank you.");
exit(0);
}
columnselect:
printf("Choose a letter between A,B,C,D: ");
scanf("\n");
scanf("%c", &number.col);
switch(number.col)
{
case 'A':
clmn = 1;
break;
case 'B':
clmn = 2;
break;
case 'C':
clmn = 3;
break;
case 'D':
clmn = 4;
break;
}
if(arr[number.row-1][clmn] == 'X')
{
printf("Seat is taken. Please choose a different one.");
}
else
{
printf("Seat %d%c has been reserved.", number.row, number.col);
arr[number.row-1][clmn] = 'X';
}
goto printrwcl;
}
}

C Case in loop problems

I have multiple errors with this project. The first and most important one would have to be one I answer a question in a case. Rather than it going back to choosing a case, it instead goes towards the difficulty. This won't let me exit the program and to get the average questions correct.
Another error is the end average questions correct. My program crashes whenever I try to just skip questions and go to the end. All it would do is divide 0/0 unless that's not possible. I'm supposed to use a switch function which is something I've seen has a problem with loops. Sadly, I can't find a way to fix it on my end, or every answer has just gone right over my head.
#include <stdio.h>
int main()
{
int option = 0;
int answer = 0;
int remainder = 0;
int ttlProblems = 0;
int correctAnswers = 0;
double averageCorrect = 0;
char difficulty;
printf("Math Practice Program Menu");
printf("\n\n1. Addition\n");
printf("2. Subtraction \n");
printf("3. Multiplication \n");
printf("4. Division \n");
printf("5. Exit\n\n");
printf("Select an option: ");
scanf_s("%d", &option);
while (option != 5)
{
switch (option)
{
case 1:
printf("Select difficulty level...\ne for easy\nm for medium\nh for hard\n");
scanf_s(" %c", &difficulty, 1);
if (difficulty == 'e')
{
printf("3 + 1 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
if (answer == 4)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else if (difficulty == 'm')
{
printf("34 + 19 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
if (answer == 53)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else if (difficulty == 'h')
{
printf("134 + 119 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
if (answer == 253)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else
{
printf("Invalid difficulty set\n");
}
break;
case 2:
printf("Select difficulty level...\ne for easy\nm for medium\nh for hard\n");
scanf_s(" %c", &difficulty, 1);
if (difficulty == 'e')
{
printf("3 - 1 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
if (answer == 2)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else if (difficulty == 'm')
{
printf("34 - 19 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
if (answer == 15)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else if (difficulty == 'h')
{
printf("134 - 119 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
if (answer == 15)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else
{
printf("Invalid difficulty s\n");
}
break;
case 3:
printf("Select difficulty level...\ne for easy\nm for medium\nh for hard\n");
scanf_s(" %c", &difficulty, 1);
if (difficulty == 'e')
{
printf("3 * 1 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
if (answer == 3)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else if (difficulty == 'm')
{
printf("34 * 19 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
if (answer == 646)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else if (difficulty == 'h')
{
printf("134 * 119 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
if (answer == 15946)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else
{
printf("Invalid difficulty set\n");
}
break;
case 4:
printf("Select difficulty level...\ne for easy\nm for medium\nh for hard\n");
scanf_s(" %c", &difficulty, 1);
if (difficulty == 'e')
{
printf("9 / 2 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
printf("Enter any remainder: ");
scanf_s("%d", &remainder, 1);
if (answer == 4 && remainder == 1)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else if (difficulty == 'm')
{
printf("34 / 19 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
printf("Enter any remainder: ");
scanf_s("%d", &remainder, 1);
if (answer == 1 && remainder == 15)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else if (difficulty == 'h')
{
printf("134 / 19 = ? Enter answer: ");
scanf_s("%d", &answer, 1);
printf("Enter any remainder: ");
scanf_s("%d", &remainder, 1);
if (answer == 7 && remainder == 1)
{
++ttlProblems;
++correctAnswers;
printf("Correct!\n");
}
else
{
++ttlProblems;
printf("Sorry, Incorrect\n");
}
}
else
{
printf("Invalid difficulty set\n");
break;
}
break;
case 5:
break;
default:
printf("Not a valid option.\n");
}
}
averageCorrect = ttlProblems / correctAnswers;
printf("You attempted %d problems and got %d correct\nFor a percentage of %.2f correct.", ttlProblems, correctAnswers, averageCorrect);
}
Currently, your code asks the user to enter the option type before the while loop, so it will only ask once and use that value for the option every loop iteration. Moving your option input scanf_s("%d", &option); into the beginning of the while loop should solve your first problem.
In regards to dividing by zero, this will cause the program to crash because it is not possble. You might want to check whether correctAnswers is zero or not before performing the division.

yes or no to exit a switch statement

Is it possible to create yes or no function to be called to exit a switch statement. if (y)es is hit it would exit. If (n)o is hit it would loop back to the switch's options. if so how would this be done. This is what I have so far.
I hope this helps to clarify what I am trying to do
int yes_no(void) {
scanf("%d", &option);
while (option == 'y' || option == 'Y' || option == 'n' || option == 'N') {
if (option == 'y' || option == 'Y') {
return 1;
} else
if (option == 'n' || option == 'N') {
return 0;
} else {
printf("Only (Y)es or (N)o are acceptable");
}
}
}
int main(void) {
int select;
do {
printf("0 exit");
printf("1 hello");
scanf("%d", &select);
switch (select) {
case 0:
printf("Exit the program? (Y)es or (N)o :");
yes_no(); // if (n)o is hit it will loop back to the menu
break;
case 1:
printf("hello how r u doing");
break;
default:
printf("not accepted number");
}
} while (select != 0);
}
Consider the following yes_no function.
int yes_no() {
char option;
while (1) {
printf("(y/n): ");
if (scanf(" %c", &option) != 1) {
printf("Error occurred while reading option.\n");
continue;
}
if (option == 'y' || option == 'Y') {
return 1;
} else if (option == 'n' || option == 'N') {
return 0;
} else {
printf("Only (y)es or (n)o are acceptable.\n");
}
}
}
You have a return value of 1 if given a yes input, and 0 in case of no. With this in mind, in the case 0 code block of the switch statement, the return value of this function can be captured, which can then be used to either break the loop or do nothing. For example:
int main(void) {
int select;
int continue_loop = 1;
printf("Press 0 to exit\n");
printf("Press 1 for hello\n\n");
while (continue_loop) {
printf("(0/1): ");
if (scanf(" %d", &select) != 1) {
printf("Error occurred while reading number.\n");
continue;
}
switch (select) {
case 0:
printf("Exit the program? (y)es or (n)o;\n");
int make_sure = yes_no(); // yes_no() returns 1 if 'yes', and 0 if 'no'
// If 'yes', break while loop by setting continue_loop to 0.
// Do nothing in case of 'no'
if (make_sure == 1) {
continue_loop = 0;
}
break;
case 1:
printf("Hello, how are you doing?\n");
break;
default:
printf("Number not accepted.\n");
}
}
return 0;
}
This should do the job, if I get your question correctly:
int yes_or_no()
{
do
{
char option = 0;
scanf(" %c", &option);
if (option == 'y' || option == 'Y')
return 1;
if (option == 'n' || option == 'N')
return 0;
printf("Only (Y)es or (N)o are acceptable");
}
while( 1 );
}
with switch you can perform your code better:
int yes_no(void) {
scanf("%d", &option);
while (1){
switch(option){
case 'y':
case 'Y':
return 1;
case 'n':
case 'N':
return 0;
default:
printf("invalid input.try again");
scanf("%d", &option);
}
}
}

Returning to start of loop C

I want to return to the start of the loop and make the user enter another input.
This is what I have but I keep getting the error message repeating over and over again.
How would I return so the user can enter a argument
printf("Enter a option\n");
scanf("%d", &option);
while (option != 1 || option != 2 || option != 3 || option != 4)
{
if (option == 1)
{
option1(...);
break;
}
else if (option == 2)
{
option2(...);
break;
}
else if (option == 3)
{
option3(...);
break;
}
else if (option == 4)
{
option4(...);
break;
}
else
{
printf("\nPlease enter a correct option\n");
continue;
}
}
Just rearrange the logic, for example like:
do {
printf("Enter a option\n");
scanf("%d",&option);
if(option == 1){
option1(...);
break;
}
else if(option == 2){
option2(...);
break;
}
else if(option == 3){
option3(...);
break;
}
else if(option == 4){
option4(...);
break;
}
else{
printf("\nPlease enter a correct option\n");
continue;
}
}
while(true);
Now your code does the scanf only once and then iterates over the same result, instead you must read the value each time you begin the loop.
int option;
while(1)
{
printf("Enter a option\n");
scanf("%d",&option);
if(option == 1){
printf("option1\n");
break;
}
else if(option == 2){
printf("option2\n");
break;
}
else if(option == 3){
printf("option3\n");
break;
}
else if(option == 4){
printf("option4\n");
break;
}
else{
printf("\nPlease enter a correct option\n");
continue;
}
}
use a do..while with printf/scanf inside.
do {
printf("Enter a option\n");
scanf("%d",&option);
your if/else statements here...
} while(true);
you should Change your loop to do while.
do
{
printf("Enter a option\n");
scanf("%d",&option);
if(option == 1){
option1(...);
}
else if(option == 2){
option2(...);
}
else if(option == 3){
option3(...);
}
else if(option == 4){
option4(...);
}
else{
printf("\nPlease enter a correct option\n");
continue;
}
}while(option != 1 || option != 2 || option != 3 || option != 4);

How can I make a function that counts the total orders I have made and adds the price up?

I prompt the user if they want to order fish, once they are done ordering fish, they are asked to order chips, and then drinks. Once drinks is done, I need a total of all prices and then I need to list them.
Problem is, I don't know how to "tally" up all the totals. How would I go about writing a function to tally up subtotals?
The way my program gets the subTotal is:
subTotal(typeOfFood, foodChoice, foodSize, foodOrders);
In my subtotal function it just has subtotal+= $x.xx based on what type of food they chose.
I tried making a function like this:
float accumTotal(int total) {
float finalTotal;
finalTotal += total;
return finalTotal;
}
but that didn't work.
OUTPUT of my proglram:
Do you order FISH (Y/N)? y
Fish choice (K- Haddock, T- Halibut): t
What size (L-Large, M-Medium, S-Small): l
How many orders do you want? (>=0): 2
You ordered: [fish]: [Halibut], Size: [Large], ordered: [2], subtotal: [8.00]
Do you order FISH (Y/N)? y
Fish choice (K- Haddock, T- Halibut): t
What size (L-Large, M-Medium, S-Small): s
How many orders do you want? (>=0): 2
You ordered: [fish]: [Halibut], Size: [Small], ordered: [2], subtotal: [4.80]
Do you order FISH (Y/N)? n
Do you order CHIPS (Y/N)? y
Chips choice (C- Cut, R- Rings): r
What size (L-Large, M-Medium, S-Small): m
How many orders do you want? (>=0): 5
You ordered: [chips]: [Ring], Size: [Medium], ordered: [5], subtotal: [12.00]
Do you order CHIPS (Y/N)? n
Do you order DRINKS (Y/N)? n
============================
ACCUMULATIVE TOTAL = $x.xx
SOURCE: though not really necessary
#include <stdio.h>
#include <string.h>
int processing(char *typeOfFood, char foodChoice, char foodSize, int foodOrders);
float subTotal(char *typeOfFood, char foodChoice, char foodSize, int foodOrders);
float accumTotal(int total);
float subTotal(char *typeOfFood, char foodChoice, char foodSize, int foodOrders) {
float subTotal = 0;
if ((strcmp(typeOfFood, "fish")) == 0) {
if (foodChoice == 'k' || foodChoice == 'K') {
if (foodSize == 's' || foodSize == 'S') {
subTotal += 3.00;
}
else if (foodSize == 'm' || foodSize == 'M') {
subTotal += 4.00;
}
else if (foodSize == 'l' || foodSize == 'L'){
subTotal += 5.00;
}
}
else if (foodChoice == 't' || foodChoice == 'T') {
if (foodSize == 's' || foodSize == 'S') {
subTotal += 2.40;
}
else if (foodSize == 'm' || foodSize == 'M') {
subTotal += 3.20;
}
else if (foodSize == 'l' || foodSize == 'L'){
subTotal += 4.00;
}
}
}
if ((strcmp(typeOfFood, "chips")) == 0) {
if (foodChoice == 'c' || foodChoice == 'C') {
if (foodSize == 's' || foodSize == 'S') {
subTotal += 1.20;
}
else if (foodSize == 'm' || foodSize == 'M') {
subTotal += 1.60;
}
else if (foodSize == 'l' || foodSize == 'L'){
subTotal += 2.00;
}
}
else if (foodChoice == 'r' || foodChoice == 'R') {
if (foodSize == 's' || foodSize == 'S') {
subTotal += 1.80;
}
else if (foodSize == 'm' || foodSize == 'M') {
subTotal += 2.40;
}
else if (foodSize == 'l' || foodSize == 'L'){
subTotal += 3.00;
}
}
}
if ((strcmp(typeOfFood, "drink")) == 0) {
if (foodChoice == 's' || foodChoice == 'S') {
if (foodSize == 's' || foodSize == 'S') {
subTotal += 1.20;
}
else if (foodSize == 'm' || foodSize == 'M') {
subTotal += 1.60;
}
else if (foodSize == 'l' || foodSize == 'L'){
subTotal += 2.00;
}
}
else if (foodChoice == 'c' || foodChoice == 'C') {
if (foodSize == 's' || foodSize == 'S') {
subTotal += 1.05;
}
else if (foodSize == 'm' || foodSize == 'M') {
subTotal += 1.40;
}
else if (foodSize == 'l' || foodSize == 'L'){
subTotal += 1.75;
}
}
else if (foodChoice == 't' || foodChoice == 'T') {
if (foodSize == 's' || foodSize == 'S') {
subTotal += 0.90;
}
else if (foodSize == 'm' || foodSize == 'M') {
subTotal += 1.20;
}
else if (foodSize == 'l' || foodSize == 'L'){
subTotal += 1.50;
}
}
} // ifs
subTotal = subTotal * foodOrders;
return subTotal;
}
float accumTotal(int total) {
// float finalTotal;
// finalTotal += total;
// printf("[%lf]", finalTotal);
// return finalTotal;
}
int processing(char *typeOfFood, char foodChoice, char foodSize, int foodOrders){
char *foodSizeSelect, *foodChoiceSelect;
float total = subTotal(typeOfFood, foodChoice, foodSize, foodOrders);
if ((strcmp(typeOfFood, "fish")) == 0){
switch (foodChoice) {
case 'k':
foodChoiceSelect = "Haddock";
break;
case 'K':
foodChoiceSelect = "Haddock";
break;
case 't':
foodChoiceSelect = "Halibut";
break;
case 'T':
foodChoiceSelect = "Halibut";
break;
}
}
else if ((strcmp(typeOfFood, "chips")) == 0){
switch (foodChoice) {
case 'c':
foodChoiceSelect = "Cut";
break;
case 'C':
foodChoiceSelect = "Cut";
break;
case 'r':
foodChoiceSelect = "Ring";
break;
case 'R':
foodChoiceSelect = "Ring";
break;
}
}
else if ((strcmp(typeOfFood, "drinks")) == 0){
switch (foodChoice) {
case 's':
foodChoiceSelect = "Softdrink";
break;
case 'S':
foodChoiceSelect = "Softdrink";
break;
case 'c':
foodChoiceSelect = "Coffee";
break;
case 'C':
foodChoiceSelect = "Coffee";
break;
case 't':
foodChoiceSelect = "Tea";
break;
case 'T':
foodChoiceSelect = "Tea";
break;
}
}
switch (foodSize) {
case 's':
foodSizeSelect = "Small";
break;
case 'S':
foodSizeSelect = "Small";
break;
case 'm':
foodSizeSelect = "Medium";
break;
case 'M':
foodSizeSelect = "Medium";
break;
case 'l':
foodSizeSelect = "Large";
break;
case 'L':
foodSizeSelect = "Large";
break;
}
printf("You ordered: [%s]: [%s], Size: [%s], ordered: [%d], subtotal: [%.2lf]\n\n", typeOfFood, foodChoiceSelect, foodSizeSelect, foodOrders, total);
}
int main() {
char fishYesNo, chipsYesNo, drinksYesNo;
char fishChoice, fishSize; int fishOrders;
char chipsChoice, chipsSize; int chipsOrders;
char drinksChoice, drinksSize; int drinksOrders;
char *typeOfFood;
do {
typeOfFood = "fish";
printf("Do you order FISH (Y/N)? ");
scanf(" %c", &fishYesNo);
if (fishYesNo != 'n' || fishYesNo != 'n') {
printf("Fish choice (K- Haddock, T- Halibut): ");
scanf(" %c", &fishChoice);
printf("What size (L-Large, M-Medium, S-Small): ");
scanf(" %c", &fishSize);
printf("How many orders do you want? (>=0): ");
scanf("%d", &fishOrders);
processing(typeOfFood, fishChoice, fishSize, fishOrders);
}
else if (fishYesNo == 'n' || fishYesNo == 'N') {
typeOfFood = "chips";
}
} while ((strcmp(typeOfFood, "fish")) == 0);
do {
typeOfFood = "chips";
printf("Do you order CHIPS (Y/N)? ");
scanf(" %c", &chipsYesNo);
if (chipsYesNo != 'n') {
printf("Chips choice (C- Cut, R- Rings): ");
scanf(" %c", &chipsChoice);
printf("What size (L-Large, M-Medium, S-Small): ");
scanf(" %c", &chipsSize);
printf("How many orders do you want? (>=0): ");
scanf("%d", &chipsOrders);
processing(typeOfFood, chipsChoice, chipsSize, chipsOrders);
}
else if (chipsYesNo == 'n' || chipsYesNo == 'N') {
typeOfFood = "drinks";
}
} while ((strcmp(typeOfFood, "chips")) == 0);
do {
typeOfFood = "drinks";
printf("Do you order DRINKS (Y/N)? ");
scanf(" %c", &drinksYesNo);
if (drinksYesNo != 'n') {
printf("Drinks choice (S- Softdrink, C- Coffee, T- Tea): ");
scanf(" %c", &drinksChoice);
printf("What size (L-Large, M-Medium, S-Small): ");
scanf(" %c", &drinksSize);
printf("How many orders do you want? (>=0): ");
scanf("%d", &drinksOrders);
processing(typeOfFood, drinksChoice, drinksSize, drinksOrders);
}
else if (drinksYesNo == 'n' || drinksYesNo == 'N') {
}
} while ((strcmp(typeOfFood, "drinks")) == 0);
}
What you need is a tracking variable to keep count of the running total:
void processing(char *typeOfFood, char foodChoice, char foodSize, int foodOrders, float *subTotal)
{
...
printf("You ordered: [%s]: [%s], Size: [%s], ordered: [%d], subtotal: [%.2lf]\n\n", typeOfFood, foodChoiceSelect, foodSizeSelect, foodOrders, total);
*subTotal += total; // add to the total.
}
int main () {
...
char *typeOfFood;
float subTotal = 0.0f;
...
...
// for each call to 'processing'
processing(typeOfFood, fishChoice, fishSize, fishOrders, &subTotal);
...
...
// when the drinks are done:
else if (drinksYesNo == 'n' || drinksYesNo == 'N') {
printf("your bill totals to $%.2f", subTotal);
break; // note the added 'break' so that the program exits.
}
...
}

Resources