I created a c program in visual studio 2015 and I made a setup file
the setup file works fine in my computer . So i decided to sent the setup file to my friend , though the setup file was installed successfully when we run the program it shows system error "the program can't star because VCRUNTIME140D.dll is missing from your computer.Try re-installing the program"
here is the program I wrote ( which works perfectly )
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
void beg();
void beg2();
void beg3();
void beg4();
void beg5();
void beg6();
void beg7();
void beg8();
void beg9();
void beg10();
void store_a();
void the_end();
void wrong();
void right();
void help();
int i,m;
char b[101],a[101];
void main()
{
int diff;
i = 0;
printf("\n WELCOME THO THE GAME \n"); //welcome
printf(" ARE YOU READY !!\n");
do
{
printf("\n 1.play\t 2.help\t 3.exit\n"); //menu
printf(" Enter your option : ");
scanf(" %d", &diff);
switch (diff)
{
case 1:
{
beg();
break;
}
case 2:
{
help();
break;
}
case 3:
{
break;
}
}
} while (diff != 3);
}
void beg()
{
int b;
m = 2;
printf("\n what is the capital of india?\n");
printf(" options\n");
printf(" 1.New Delhi\n 2.Mumbai\n 3.Kochi\n 4.Chennai\n");
scanf("%d", &b);
if (b == 1)
{
right();
beg2();
}
else
{
wrong();
beg2();
}
}
void beg2()
{
int b;
m = 3;
printf("\n 12 + 11 = ?\n");
printf(" options\n");
printf(" 1.12\n 2.45\n 3.23\n 4.17\n");
scanf("%d", &b);
if (b != 3)
{
b = 2;
wrong();
beg3();
}
else
{
while (b == 3)
{
if (b == 3)
{
right();
beg3();
break;
}
}
}
}
void beg3()
{
int b;
m = 4;
printf("\n who is the prime minister of india ?\n");
printf(" options\n");
printf(" 1.Pranab Mukherjee\n 2.narendra modi\n 3.manmohan singh\n 4.steven thomas\n");
scanf("%d", &b);
if (b != 2)
{
b = 1;
wrong();
beg4();
}
else
{
while (b == 2)
{
if (b == 2)
{
right();
beg4();
break;
}
}
}
}
void beg4()
{
int b;
m = 5;
printf("\n 5 x 7 = ?\n");
printf(" options\n");
printf(" 1.32\n 2.54\n 3.25\n 4.35\n");
scanf("%d", &b);
if (b != 4)
{
b = 1;
wrong();
beg5();
}
else
{
while (b == 4)
{
if (b == 4)
{
right();
beg5();
break;
}
}
}
}
void beg5()
{
int b;
m = 6;
printf("\n which country is paris the capital of?\n");
printf(" options\n");
printf(" 1.India\n 2.France\n 3.England\n 4.China\n");
scanf("%d", &b);
if (b != 2)
{
b = 1;
wrong();
beg6();
}
else
{
while (b == 2)
{
if (b == 2)
{
right();
beg6();
break;
}
}
}
}
void beg6()
{
int b;
m = 7;
printf("\n what is the capital of america?\n");
printf(" options\n");
printf(" 1.Los Angeles\n 2.New York \n 3.Washington, D.C.\n 4.Las Vegas\n");
scanf("%d", &b);
if (b != 3)
{
b = 1;
wrong();
beg7();
}
else
{
while (b == 3)
{
if (b == 3)
{
right();
beg7();
break;
}
}
}
}
void beg7()
{
int b;
m = 8;
printf("\n what is the highest mountain in the world?\n");
printf(" options\n");
printf(" 1.Mount Everest\n 2.Kangchenjunga \n 3.Annapurna\n 4.K2 \n");
scanf("%d", &b);
if (b != 1)
{
b = 3;
wrong();
beg8();
}
else
{
while (b == 1)
{
if (b == 1)
{
right();
beg8();
break;
}
}
}
}
void beg8()
{
int b;
m = 9;
printf("\n who invented piano?\n");
printf(" options\n");
printf(" 1.serin thomas\n 2.Bartolomeo Cristofori \n 3.annrose pino\n 4.Baldwin pinero \n");
scanf("%d", &b);
if (b != 2)
{
b = 3;
wrong();
beg9();
}
else
{
while (b == 2)
{
if (b == 2)
{
right();
beg9();
break;
}
}
}
}
void beg9()
{
int b;
m = 10;
printf("\n what is the answer of ( 3-3 x 6+2 )?\n");
printf(" options\n");
printf(" 1.-17\n 2. 0 \n 3. -13\n 4. 9 \n");
scanf("%d", &b);
if (b != 3)
{
b = 1;
printf("\n Do you want to know the correct answer for the qustion :\n1.yes\n2.no\t: ");
scanf("%d", &opt);
if (opt == 1)
{
printf("\n The correct answer is as follows:\n Question: 3 – 3 x 6 + 2\n Multiplication first : 3 – 18 + 2\n Left to right : -15 + 2\n Answer : -13\n");
}
wrong();
beg10();
}
else
{
while (b == 3)
{
if (b == 3)
{
right();
beg10();
break;
}
}
}
}
void beg10()
{
int len,i,f;
store_a();
printf("\n A man has a barrel with filled with oil that weighs 100 pounds,\n and then he puts something into it.\n Now the barrel weighs less than 100 pounds.\n What did he put in the barrel ?? \n");
printf(" enter the answer :");
scanf("%s", b);
len = strlen(b);
for (i = 0; i < len; i++)
{
if (a[i] == b[i])
{
f = 1;
}
else
{
f = 0;
break;
}
}
if (f == 1)
{
printf("The answer is correct!!\n");
the_end();
}
else
{
printf(" Wrong answer\nthe correct ans was 'hole'\n");
the_end();
}
}
void wrong()
{
printf("wrong answer !!\n");
printf("your score is :%d\n", i);
printf("\n qustion number: %d", m);
}//when the option is wrong
void the_end()
{
printf("\n***congratulations***\n");
printf(" your final score is :%d\n", i);
}//at the end of the game
void right()
{
printf("the answer is correct !!\n");
i++;
printf("your score is :%d\n", i);
printf("\n qustion number: %d", m);
}//when the ans is correct
void help()
{
int opt;
printf("\n 1.How to play\t2.About\t3.Main menu\n");
printf(" enter your option : ");
scanf("%d", &opt);
if (opt == 1)
{
printf(" answer the qustions..\n you get 1 point for each qustion..\n there is a total of 10 qustions try to get the maximum marks\n good luck \n");
help();
}
else if (opt == 2)
{
printf("\tCREATED BY\n **STEVEN THOMAS** \n **steventhomaspuli#gmail.com**\n ");
help();
}
else if (opt == 3)
{
main();
}
}//help menu
void store_a()
{
a[0] = 'h';
a[1] = 'o';
a[2] = 'l';
a[3] = 'e';
}
and this is the setup file I created
click here to download
pls tell me what is wrong here
You had built and shipped debug version of your program. Debug VC runtime is not a part of VC redistributable package, so your friend get a message about missing DLLs. Build and ship release version.
Related
The code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 500
#define M 100
/*Version 1*/
void replace_word(char *word);
void add_new_word_dictionary(void);
void do_nothing(void);
void return_basic(void);
void check_word(void);
void compute_words(void);
void compute_characters(void);
void compute_ch_sp(void);
void compute_num_dif_words(void);
void create_istogram(void);
void save_file(void);
int get_choice(void);
void return_word(void);
void insert_text(int numwords, char matrix[N][M], int posit);
int main() {
int j;
int choice = 0;
char matrix[N][M];
char word[40] = { "t" };
while (1) {
choice = get_choice();
if (choice == 0) {
insert_text(M, matrix, 1);
}
if (choice == 1) {
add_new_word_dictionary();
}
if (choice == 2) {
do_nothing();
}
if (choice == 3) {
save_file();
}
if (choice == 4) {
compute_words();
}
if (choice == 5) {
break;
}
for (j = 0; j < M; j++) {
printf("%s", matrix[N][M]);
}
}
printf("\n End of Program \n");
return 0;
}
void replace_word(char *word) {
return;
}
void add_new_word_dictionary(void) {
char word[50] = { "s" };
printf("\nPlease enter the word\n");
scanf("\n%s", word);
printf("Your word is %s", word);
return;
}
void do_nothing(void) {
printf("\n do_nothing \n");
return;
}
void return_basic(void) {
printf("\n return basic \n");
return;
}
void check_word(void) {
printf("\n check word \n");
return;
}
void compute_words(void) {
printf("\n compute_words \n");
return;
}
void compute_characters(void) {
printf("\n compute characters \n");
}
void compute_ch_sp(void) {
printf("\n compute_ch_sp \n");
return;
}
void compute_num_dif_words(void) {
printf("\n compute_num_same_words \n");
return;
}
void create_istogram(void) {
printf("\n create istogram \n");
return;
}
void save_file(void) {
printf("\n save_file \n");
return;
}
int get_choice(void) {
int choice = 0;
printf("\n Select a choice from the below \n");
printf("\n Select 0 to add text \n");
printf("\n Select 1 to add new words in the dictionary \n");
printf("\n Select 2 to enter enter correction mode \n");
printf("\n Select 3 to save the text \n");
printf("\n Select 4 to see the statistics about your text \n");
printf("\n Select 5 to exit the program\n");
scanf("\n%d", &choice);
return choice;
}
void insert_text(int numwords, char matrix[N][M], int posit) {
int i;
int j;
char word2[40] = { "" };
while (strcmp(word2, "*T*E*L*O*S*")) {
printf("\n Add the word \n");
scanf("\n%s", word2);
if (posit + 1 > numwords) {
printf("\n Out of Bounds \n ");
}
for (i = numwords - 2; i >= posit; i--) {
strcpy(matrix[i + 1], matrix[i]);
if (!i)
break;
}
strcpy(matrix[posit], word2);
j++;
}
for (i = 0; i < j; i++) {
printf("%s", matrix[i]);
printf("\n j is %d\n", j);
}
return;
}
The problem: I have a function called insert_text. This function adds a string in the 1st position of an array (at least that is what I think it does) and it is called if choice is 0 and executes itself until the string *ΤELOS* is given. When in insert_text I print matrix I get a bunch of *(null)*s... I can count how many words matrix has (by declaring a variable j and incrementing by 1 inside the while loop, but that does not seem to work either. How can I fix this?
The printing code is incorrect: matrix is an array of N arrays of M characters, where you store null terminated C strings. As coded, you pass a single character just beyond the end of the array to printf for %s, which expects a string. The loop should be:
for (j = 0; j < N; j++) {
printf("%s ", matrix[j]);
}
Note that char matrix[N][M]; is uninitialized, so its contents will seem random. Initialize matrix as char matrix[N][M] = { "" };
Also note that in add_new_word_dictionary(), the scanf() conversion should be scanf("%49s", word); to prevent a potential buffer overflow if the user enters a very long word.
Same in insert_text, the code should be scanf("%39s", word2) ans you should test the return value to check for input errors.
Finally, arrays are indexed from 0 in C, so insert_text should be given a position of 0 and the number of words should be N, not M.
Both the test and the insertion loop have problems too.
Here is a modified version:
// call from main as insert_text(N, matrix, 0);
//
void insert_text(int numwords, char matrix[N][M], int posit) {
char word2[40];
int i, j = 0;
for (;;) {
printf("\n Add the word \n");
if (scanf("%39s", word2) != 1) {
break; // end of file or input error
}
if (!strcmp(word2, "TELOS")) {
break; // magic word
}
if (posit >= numwords) {
printf("\n Out of Bounds \n");
break;
}
for (i = numwords - 2; i >= posit; i--) {
strcpy(matrix[i + 1], matrix[i]);
}
strcpy(matrix[posit], word2);
j++;
}
for (i = 0; i < j; i++) {
printf("%s ", matrix[i]);
}
printf("\n j is %d\n", j);
}
Here is a modified version of the whole program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 500
#define M 100
/*Version 1*/
void replace_word(char *word);
void add_new_word_dictionary(void);
void do_nothing(void);
void return_basic(void);
void check_word(void);
void compute_words(void);
void compute_characters(void);
void compute_ch_sp(void);
void compute_num_dif_words(void);
void create_istogram(void);
void save_file(void);
int get_choice(void);
void return_word(void);
void insert_text(int numwords, char matrix[N][M], int posit);
int main() {
int j, done = 0;
char matrix[N][M] = { "" };
while (!done) {
switch (get_choice()) {
case 0:
insert_text(N, matrix, 0);
break;
case 1:
add_new_word_dictionary();
break;
case 2:
do_nothing();
break;
case 3:
save_file();
break;
case 4:
compute_words();
break;
default:
done = 1;
break;
}
for (j = 0; j < N; j++) {
if (matrix[j][0])
printf("%s ", matrix[j]);
}
printf("\n");
}
printf("\n End of Program \n");
return 0;
}
void add_new_word_dictionary(void) {
char word[50];
printf("\nPlease enter the word\n");
if (scanf("%49s", word) == 1)
printf("Your word is %s\n", word);
}
void replace_word(char *word) { printf("\n replace word \n"); }
void do_nothing(void) { printf("\n do_nothing \n"); }
void return_basic(void) { printf("\n return basic \n"); }
void check_word(void) { printf("\n check word \n"); }
void compute_words(void) { printf("\n compute_words \n"); }
void compute_characters(void) { printf("\n compute characters \n"); }
void compute_ch_sp(void) { printf("\n compute_ch_sp \n"); }
void compute_num_dif_words(void) { printf("\n compute_num_same_words \n"); }
void create_istogram(void) { printf("\n create istogram \n"); }
void save_file(void) { printf("\n save_file \n"); }
int get_choice(void) {
int choice = -1;
printf("\nSelect a choice from the below \n");
printf("Select 0 to add text \n");
printf("Select 1 to add new words in the dictionary \n");
printf("Select 2 to enter enter correction mode \n");
printf("Select 3 to save the text \n");
printf("Select 4 to see the statistics about your text \n");
printf("Select 5 to exit the program\n");
scanf("%d", &choice);
return choice;
}
// call from main as insert_text(N, matrix, 0);
//
void insert_text(int numwords, char matrix[N][M], int posit) {
char word2[40];
int i;
for (;;) {
printf("\n Add the word \n");
if (scanf("%39s", word2) != 1) {
break; // end of file or input error
}
if (!strcmp(word2, "TELOS")) {
break; // magic word
}
if (posit >= numwords) {
printf("\n Out of Bounds \n");
break;
}
for (i = numwords - 2; i >= posit; i--) {
strcpy(matrix[i + 1], matrix[i]);
}
strcpy(matrix[posit], word2);
posit++;
}
for (i = 0; i < posit; i++) {
printf("%s ", matrix[i]);
}
printf("\n posit is %d\n", posit);
}
I have new problem about while-loop!
When I input the l_res as y it means that the condition of while loop is still TRUE, instead repeat it from the start, it terminated the program. → enter image description here
int main()
{
//VARIABLES
char f_res[4];
char l_res = 'y';
int a,b,c,x;
while(l_res == 'y')
{
printf("Enter 3 digit capacitor Code: ");
scanf("%d %d %d",&a,&b,&c);
x = (a*10)+b;
printf("What is the unit of the value?(Pico-picofarad, Nano-Nanofarad): ");
scanf("%s", f_res);
if(strcmp(f_res, "Pico") == 0)
{
if(c == 0)
{
x = x;
}
else
{
while(c <= 5 && c > 0)
{
c--;
x = x*10;
}
}
printf("\nCapacitor value is: %d pF\n\n",x);
}
else if(strcmp(f_res, "Nano") == 0)
{
float x = ((a*10)+b)*0.001;
if(c == 0)
{
x = x;
}
else
{
while(c <= 5 && c > 0)
{
c--;
x = x*10;
}
}
printf("\nCapacitor value is: %.3f pF\n\n",x);
}
else
{
printf("\nINVALID Input!\n\n");
}
printf("Do you want to enter other capacitor Code?( y-Yes, n-No): ");
scanf(" %c", l_res);
}
return 0;
}
How can I fix this?
I have followed everything in the book, yet my average score fails me, every single time. I have debugged my program multiple times, in vain.
My minimal executable code:
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX_TESTS 5
#define MAXQUESTION 10000
bool myread(const char * format, void * address) {
char buffer[1024];
fgets(buffer, sizeof buffer, stdin);
return sscanf(buffer, format, address) == 1;
}
struct struc {
int a;
int b;
int c;
int add;
int grade;
};
int sj(int n) {
int t;
t = rand() % n;
return t;
}
void ctm_i(struct struc * t) {
{
t -> a = sj(101);
t -> c = sj(4);
if (t -> c == 1) {
t -> b = sj(101 - (t -> a));
t -> add = (t -> a) + (t -> b);
} else {
t -> b = sj((t -> a) + 1);
t -> add = (t -> a) - (t -> b);
}
t -> grade = 0;
}
}
void tcm_i(struct struc * t, int n) {
int ad;
printf(" ***********************************************************************"
"*********\n");
printf(" ......................................................................."
".........\n");
printf(" Question %d\n\n", n + 1);
printf(" You have 3 attempts for this question\n\n");
if (t -> c == 1)
printf(" %d+%d= ", t -> a, t -> b);
else
printf(" %d-%d= ", t -> a, t -> b);
myread(" %d", & ad);
if (ad == t -> add)
{
t -> grade = 10;
printf(" You earned 10 marks\n\n");
} else {
printf("\n Incorrect, you have 2 attempts remaining\n\n");
printf(" ");
myread(" %d", & ad);
if (ad == t -> add)
{
t -> grade = 7;
printf(" You earned 7 marks\n\n");
} else {
printf("\n Incorrect, you have 1 attempt remaining\n\n");
printf(" ");
myread(" %d", & ad);
if (ad == t -> add)
{
t -> grade = 5;
printf(" You earned 5 marks\n\n");
} else {
t -> grade = 0;
printf("\n Failure, 0 mark\n\n");
printf("\n The correct answer is %d\n\n", t -> add);
}
}
}
printf(" ......................................................................."
".........\n");
printf(" ***********************************************************************"
"*********\n");
}
void quiz(char name[]) {
int rounds = 0;
int highest = 0;
int lowest = INT_MAX;
float allScore = 0;
float avg = 0.0;
int i, j, g = 0;
struct struc test[MAX_TESTS];
srand((unsigned) time(NULL));
for (;;) {
rounds++;
for (i = 0; i < MAX_TESTS; i++) // generate all questions
{
ctm_i( & test[i]);
for (j = 0; j < i; j++)
if (test[i].a == test[j].a && test[i].b == test[j].b && test[i].c == test[j].c)
//if question is already present
ctm_i( & test[i]); //then re-generate
}
printf("\n Are you ready? Press Enter key to continue. ");
myread("", NULL);
for (i = 1; i <= 5; i++) {
printf(" *******************************************************************"
"**"
"***********\n");
printf(" ..................................................................."
".."
"...........\n");
}
// Take quiz
for (i = 0; i < MAX_TESTS; i++)
tcm_i( & test[i], i);
printf(" End\n\n");
bool done = false;
bool unsure = true;
bool showS = true;
while (unsure) {
unsure = false;
puts("\n");
if (showS) {
puts(" Enter 'S' to show results");
}
puts(" Enter 'P' to play another round");
puts(" Enter 'R' to return to main menu");
char choice;
printf(" ");
myread(" %c", & choice);
printf("\n");
if (choice == 'r' || choice == 'R') {
done = true;
} else {
///////////////////////// Changes /////////////
g = 0;
// calculate total score for current round
for (i = 0; i < MAX_TESTS; i++) {
g += test[i].grade; //add score of each question
}
allScore += g; //add current round's score to total
avg = allScore / rounds; //average of all rounds
if (g > highest) {
highest = g;
}
if (g < lowest) {
lowest = g;
}
if (choice == 'S' || choice == 's') {
showS = false;
if (rounds == 1) {
printf(" Final score: %d/100\n", g); //display round score
printf(" ****** Player: %s ******\n", name);
} else {
printf(" Round %d score: %d/100\n", rounds, g); //display round score
printf(" Highest score: %d/100\n", highest);
printf(" Lowest score : %d/100\n", lowest);
printf(" Average score: %f/100\n", avg);
printf(" ****** Player: %s ******\n", name);
}
unsure = true;
} else if (choice == 'P' || choice == 'p') {
/// nothing to be done here
//we will display next test
} else {
puts(" Invalid input!");
unsure = true;
}
////////////////////////////////////
}
}
if (done)
break;
}
}
int main() {
char i1 = '1';
char name[25]; // ig;
printf("\n Welcome");
printf("\n");
while (i1 != 0) {
printf("\n");
//printf(" **********************Welcome %s! *********************\n", name);
printf(" ************************ Main Menu of Maths Quiz ***************************\n");
printf(" * 1.Enter Quiz *\n");
printf(" * 2.Quit *\n");
printf(" ****************************************************************************\n");
printf(" Please choose one from 1-2:\n");
printf(" ");
myread(" %c", & i1);
switch (i1) {
case '1':
printf("\n Enter Quiz:\n");
quiz(name); // calling quiz function defined in file "maincode.c"
break;
case '2':
printf(" Quit.\n\n");
}
}
return 0;
}
Hope that fulfilled the definition of an MRE.
So I have snipped off some bombastic lines, and the ones left are the important ones, I think.
The problem is in your function quiz, and in particular in the while (unsure) loop. In this loop, you add the most recent score to the running total:
allScore += g; //add current round's score to total
This should happen once per round played. But if the user inputs "S" or something invalid, your program sets
unsure = true;
which means the loop will run again, before the next round is played. It then adds the most recent score a second time to the grand total.
The most logical solution would be to move all the calculations of totals, maximum, minimum, average out of the while-loop. The loop serves a different purpose: it is for user interaction and reporting, not for processing results.
possible chance of multiple avg calculation for same round again. Keep flag to track accounting of score, so we will not do the same more than once till the game is not played again.
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX_TESTS 5
#define MAXQUESTION 10000
bool myread(const char * format, void * address) {
char buffer[1024];
fgets(buffer, sizeof buffer, stdin);
return sscanf(buffer, format, address) == 1;
}
struct struc {
int a;
int b;
int c;
int add;
int grade;
};
int sj(int n) {
int t;
t = rand() % n;
return t;
}
void ctm_i(struct struc * t) {
{
t -> a = sj(101);
t -> c = sj(4);
if (t -> c == 1) {
t -> b = sj(101 - (t -> a));
t -> add = (t -> a) + (t -> b);
} else {
t -> b = sj((t -> a) + 1);
t -> add = (t -> a) - (t -> b);
}
t -> grade = 0;
}
}
void tcm_i(struct struc * t, int n) {
int ad;
printf(" ***********************************************************************"
"*********\n");
printf(" ......................................................................."
".........\n");
printf(" Question %d\n\n", n + 1);
printf(" You have 3 attempts for this question\n\n");
if (t -> c == 1)
printf(" %d+%d= ", t -> a, t -> b);
else
printf(" %d-%d= ", t -> a, t -> b);
myread(" %d", & ad);
if (ad == t -> add)
{
t -> grade = 10;
printf(" You earned 10 marks\n\n");
} else {
printf("\n Incorrect, you have 2 attempts remaining\n\n");
printf(" ");
myread(" %d", & ad);
if (ad == t -> add)
{
t -> grade = 7;
printf(" You earned 7 marks\n\n");
} else {
printf("\n Incorrect, you have 1 attempt remaining\n\n");
printf(" ");
myread(" %d", & ad);
if (ad == t -> add)
{
t -> grade = 5;
printf(" You earned 5 marks\n\n");
} else {
t -> grade = 0;
printf("\n Failure, 0 mark\n\n");
printf("\n The correct answer is %d\n\n", t -> add);
}
}
}
printf(" ......................................................................."
".........\n");
printf(" ***********************************************************************"
"*********\n");
}
void quiz(char name[]) {
int rounds = 0;
int highest = 0;
int lowest = INT_MAX;
float allScore = 0;
float avg = 0.0;
int i, j, g = 0;
struct struc test[MAX_TESTS];
srand((unsigned) time(NULL));
for (;;) {
rounds++;
for (i = 0; i < MAX_TESTS; i++) // generate all questions
{
ctm_i( & test[i]);
for (j = 0; j < i; j++)
if (test[i].a == test[j].a && test[i].b == test[j].b && test[i].c == test[j].c)
//if question is already present
ctm_i( & test[i]); //then re-generate
}
printf("\n Are you ready? Press Enter key to continue. ");
myread("", NULL);
for (i = 1; i <= 5; i++) {
printf(" *******************************************************************"
"**"
"***********\n");
printf(" ..................................................................."
".."
"...........\n");
}
// Take quiz
for (i = 0; i < MAX_TESTS; i++)
tcm_i( & test[i], i);
printf(" End\n\n");
bool done = false;
bool unsure = true;
bool showS = true;
bool acct = false;
while (unsure) {
unsure = false;
puts("\n");
if (showS) {
puts(" Enter 'S' to show results");
}
puts(" Enter 'P' to play another round");
puts(" Enter 'R' to return to main menu");
char choice;
printf(" ");
myread(" %c", & choice);
printf("\n");
if (choice == 'r' || choice == 'R') {
done = true;
} else {
///////////////////////// Changes /////////////
if (false == acct) {
g = 0;
// calculate total score for current round
for (i = 0; i < MAX_TESTS; i++) {
g += test[i].grade; //add score of each question
}
allScore += g; //add current round's score to total
avg = allScore / rounds; //average of all rounds
if (g > highest) {
highest = g;
}
if (g < lowest) {
lowest = g;
}
acct = true;
}
if (showS &&(choice == 'S' || choice == 's')) {
showS = false;
if (rounds == 1) {
printf(" Final score: %d/100\n", g); //display round score
printf(" ****** Player: %s ******\n", name);
} else {
printf(" Round %d score: %d/100\n", rounds, g); //display round score
printf(" Highest score: %d/100\n", highest);
printf(" Lowest score : %d/100\n", lowest);
printf(" Average score: %f/100\n", avg);
printf(" ****** Player: %s ******\n", name);
}
unsure = true;
} else if (choice == 'P' || choice == 'p') {
/// nothing to be done here
//we will display next test
} else {
puts(" Invalid input!");
unsure = true;
}
////////////////////////////////////
}
}
if (done)
break;
}
}
int main() {
char i1 = '1';
char name[25]; // ig;
printf("\n Welcome");
printf("\n");
while (i1 != 0) {
printf("\n");
//printf(" **********************Welcome %s! *********************\n", name);
printf(" ************************ Main Menu of Maths Quiz ***************************\n");
printf(" * 1.Enter Quiz *\n");
printf(" * 2.Quit *\n");
printf(" ****************************************************************************\n");
printf(" Please choose one from 1-2:\n");
printf(" ");
myread(" %c", & i1);
switch (i1) {
case '1':
printf("\n Enter Quiz:\n");
quiz(name); // calling quiz function defined in file "maincode.c"
break;
case '2':
printf(" Quit.\n\n");
}
}
return 0;
}
Output:
Enter 'S' to show results
Enter 'P' to play another round
Enter 'R' to return to main menu
s
Round 2 score: 47/100
Highest score: 50/100
Lowest score : 47/100
Average score: 48.500000/100
****** Player: ******
I have written a Guess the Movie game in the C programming language. My logic seems to be correct but whenever I run the program, it doesn't work as expected.
Here is my code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ran = 1;
int l, j = 0, i = 0, total = 0, d = 0;
char b;
char a[20];
char s[1000];
int z;
FILE *my;
printf("Enter your name:\n ");
scanf("%s", s);
ran = rand() % 6;
if (ran == 1)
{
my = fopen("my1.txt", "r");
}
else if (ran == 2)
{
my = fopen("my.txt", "r");
}
else if (ran == 3)
{
my = fopen("my2.txt", "r");
}
else if (ran == 4)
{
my = fopen("my3.txt", "r");
}
else if (ran == 5)
{
my = fopen("my4.txt", "r");
}
for (d = 0; d < 20; d++)
fscanf(my, "%c", &a[d]);
fclose(my);
printf("GUESS THE MOVIE GAME\n");
for (j = 0; j < 7; j++)
{
if (a[j] == 'm')
{
printf("M ");
}
else
{
printf("_ ");
}
}
printf("\n");
printf("Let's begin the game\n");
for (i = 0; i < 7;)
{
if (a[i] != 'm')
{
printf("enter character number %d\n",i+1);
scanf("%c", &b);
if (b == a[i])
{
printf("its a right guess\n");
total = total + 4;
i++;
}
else if (b != a[i])
{
printf("Wrong choice\n");
if (total == 1 || total == 0)
{
total=0;
}
else
{
total = total - 2;
}
}
}
}
printf("You have guessd the movie\n");
printf("The movie name is: ");
for (i = 0; i < 7; i++)
{
printf("%c",a[i]);
}
printf("Your score is %d\n",total);
}
This is the program output that I get each time I run the above code:
Enter your name:
raj
GUESS THE MOVIE GAME
_ _ _ _ M _ _
Let's begin the game
Enter character number 1
Wrong choice
Enter character number 1
I
Wrong choice
Enter character number 1
Wrong choice
Enter character number 1
Besides the deficiencies pointed out in comments, there's this major logic error:
for (i = 0; i < 7;)
{
if (a[i] != 'm')
{
…
}
}
If the loop encounters an m, it repeats endlessly. Eliminate the if (a[i] != 'm') or add an else ++i.
The problem is in this function. It's supposed to validate input for two variables as integers. What did I do wrong? O__O Thanks :)
I used an if else statement to check for change in the variable valid so that it will exit the loop once the right input is given. Even if I input the right values, the code still crashes.
void input(int *n1, int *n2, char *opt)
{
int valid = 0;
int v2 = 0;
char choice;
int a, b;
while (v2 == 0)
{
printf("Enter first number: \n");
if(scanf("%d", &a) == 1)
{
while(v2 == 0)
{
printf("Enter second number: \n");
if(scanf("%d", &b) == 1)
{
v2 =1;
getchar();
}
else
{
getchar();
printf("Invalid input!\n");
}
}
getchar();
}
else
{
getchar();
printf("Invalid input!\n");
}
}
while( valid == 0)
{
printf("Addition -> 1\nSubtraction -> 2\nMultiplication -> 3\nDivision -> 4\nReset -> R\nExit -> E\n");
scanf("%c", &choice);
if (choice == 'r' || choice == 'e')
{
choice = toupper(choice);
}
if ((choice == '1') || (choice == '2') || (choice == '3') || (choice == '4') || (choice == 'R') || (choice == 'E'))
{
valid = 1;
}
else
{
printf("Invalid input!\n\n");
}
}
*opt = choice;
*n1 = a;
*n2 = b;
}
Here's the whole code for reference. The answers earlier were able to fix the crash. Now, either the loop doesn't exit or it doesn't work right.
#include <stdio.h>
#include <ctype.h>
int add(int n1, int n2);
int subtract(int n1, int n2);
int multiply(int n1, int n2);
int divide(int n1, int n2);
void input(int *n1, int *n2, char *opt);
int main(void)
{
int n1, n2, ret;
char opt;
start:
input(&n1, &n2, &opt);
switch(opt)
{
case '1':
ret = add(n1, n2);
printf("The sum is %d\n", ret);
break;
case '2':
ret = subtract(n1, n2);
printf("The difference is %d\n", ret);
break;
case '3':
ret = multiply(n1, n2);
printf("The product is %d\n", ret);
break;
case '4':
ret = divide(n1, n2);
printf("The quotient is %d\n", ret);
break;
case 'R':
goto start;
break;
case 'E':
printf("Goodbye!\n");
return 0;
break;
}
goto start;
}
void input(int *n1, int *n2, char *opt)
{
int valid = 0;
int v2 = 0;
char choice;
int a, b;
while (v2 == 0)
{
printf("Enter first number: \n");
if(scanf("%d", &a) == 1)
{
while(v2 == 0)
{
printf("Enter second number: \n");
if(scanf("%d", &b) == 1)
{
v2 =1;
getchar();
}
else
{
getchar();
printf("Invalid input!\n");
}
}
getchar();
}
else
{
getchar();
printf("Invalid input!\n");
}
}
while( valid == 0)
{
printf("Addition -> 1\nSubtraction -> 2\nMultiplication -> 3\nDivision -> 4\nReset -> R\nExit -> E\n");
scanf("%c", &choice);
if (choice == 'r' || choice == 'e')
{
choice = toupper(choice);
}
if ((choice == '1') || (choice == '2') || (choice == '3') || (choice == '4') || (choice == 'R') || (choice == 'E'))
{
valid = 1;
}
else
{
printf("Invalid input!\n\n");
}
}
*opt = choice;
*n1 = a;
*n2 = b;
}
int add(n1, n2)
{
int result;
result = (n1+n2);
return result;
}
int subtract(n1, n2)
{
int result;
result = (n1-n2);
return result;
}
int divide(n1, n2)
{
int result;
result = (n1/n2);
return result;
}
multiply(n1, n2)
{
int result;
result = (n1*n2);
return result;
}
Change
if(scanf("%d", a) != 0)
to
if(scanf("%d", &a) == 1)
// ^^^^ This is the right check
// ^^^ Missing &
scanf return EOF if it fails to assign to the first receiving argument. In this case, it will return 1 if data was successfully read into &a.
Similarly, change
if(scanf("%d", b) != 0)
to
if(scanf("%d", &b) == 1)
// ^^^ ^^^^
Instead of
if(scanf("%d", a) != 0)
You should use
if(scanf("%d", &a))
Scanf could return 0,1 or EOF out of which only 1 indicates no error in input!
However if your a was a pointer to some integer address location you could have used the former code.Change it for inputting b as well