Yes/No by If/Else Char Response in C on Visual Studio 2013 Compiler

Hello this is my first post. I am taking an introduction to C class in my college and the assignment this week is to write a program to a classmate's yes/no scenario. The problem is when I get to question 3 my compiler errors stating "Unhandled exception at 0x0FB3DBE9 (msvcr120d.dll) in ConsoleApplication3.exe: 0xC0000005: Access violation writing location 0x0000006E."

I am using Microsoft Visual Studio Express 2013 for Windows Desktop Version 12.0.30501.00 Update 2.

Basically, I would like to have the yes/no char response translated to a true/false statement and analyzed later. True is this case means favorable condition and false unfavorable condition to the scenario's resolve (going on a date). This is strictly in C not C++. Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 /* If my wife has cooked at least 4 meals this week, we haven't had a fight, or my favorite sports team won their game.
 Also, she has to be willing to wear a dress and her idea for date has to be less than $30. If these are meet then I'll be happy, and we will can go out. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

int main(void)
{
	int numMeal = 0;
	bool mealGoal = false;
	int numFight = 0;
	bool fightGoal = false;
	char favSportResult= 'n';
	bool sportGoal = false;
	char dressUse = 'n';
	bool dressGoal = false;
	int dateCost = 0;
	bool dateGoal = false;
	bool user;

	printf("How many meals has wife cooked this week?\n");
	scanf_s("%d", &numMeal);
	if (numMeal >= 4) { mealGoal = true; }
	else { mealGoal = false; }

	printf("How many fights this week?\n");
	scanf_s("%d", &numFight);
	if (numFight > 0) { fightGoal = false; }
	else if (numFight == 0) { fightGoal = true; }

	printf("*Edited* Has your favorite sports team won this week?\n");
	scanf_s("%c", favSportResult);
	if (favSportResult == 'y') { sportGoal = true; }
	else if (favSportResult == 'n') { sportGoal = false; }

	printf("Is wife willing to wear a dress? (answer y or n)\n");
	scanf_s("%c", dressUse);
	if (dressUse == 'y') { dressGoal = true; }
	else if (dressUse == 'n') { dressGoal = false; }

	printf("How much will the date cost?\n");
	scanf_s("%d", &dateCost);
	if (dateCost < 30) { dateGoal = true; }
	else { dateGoal = false; }

	if (mealGoal && fightGoal && sportGoal && dressGoal && dateGoal)
	{
		user = true;
	}
	else {
		user = false;
}

if (user = true)
{
	printf("You are a happy man. Enjoy your date.");
}
else if (user = false) {
	printf("No date tonight. Better luck next time.");
}

	system("pause");
}



Last edited on
1
2
if (numMeal >= 4) { mealGoal = true; }
	else { mealGoal = false; }
couldn't you just do mealGoal = numMeal >= 4; same with all the other if/else used for setting true or false.

Also look at lines 56 and 60. It should be == not =. The former is for comparison and the latter is for assignment.

Lines 34 and 39 should be passed by reference as well.

Line 33 shouldn't it ask if your favorite sports team won? Asking how many times then yes or no doesn't make sense.

Also on line 34 and 39 when you do %c it will read in the next character and when you hit enter it will leave a newline in the buffer so instead of being y/n it will be \n to fix this you can do " %c"

*Edit also whats up with lines 56-62 why not just do that where you have 48-54. Makes more sense to just print there instead of assign to variable.
Last edited on
I made all the corrections and it compiles successfully. When run I still get the error mentioned above. It's something to do with the input in question 3. I noticed this time with the space before the format specifier I was able to type a response and enter before the error message breaking the program. Any suggestions to resolve?
It compiled fine for me, would you please post the new code.
First Comment: I am not sure what doing mealGoal = numMeal >= 4 means. Do you mean making that the if expression?

Second comment: Changed and noted.

Third Comment: Not sure what that means.

Fourth Comment: My mistake I was going back and forth between answering the question with a numerical or character response. Thank you.

Fifth Comment: Changed and found the program advanced better.

Sixth Comment: Yes, good point and noted.

Please bare with me I am very inexperienced at this.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

int main(void)
{
int numMeal = 0;
bool mealGoal = false;
int numFight = 0;
bool fightGoal = false;
char favSportResult= 'n';
bool sportGoal = false;
char dressUse = 'n';
bool dressGoal = false;
int dateCost = 0;
bool dateGoal = false;

printf("How many meals has wife cooked this week?\n");
scanf_s("%d", &numMeal);
if (numMeal >= 4) { mealGoal = true; }
else { mealGoal = false; }

printf("How many fights this week?\n");
scanf_s("%d", &numFight);
if (numFight > 0) { fightGoal = false; }
else if (numFight == 0) { fightGoal = true; }

printf("Has your favorite sports team won this week? (answer y or n)\n");
scanf_s(" %c", favSportResult);
if (favSportResult == 'y') { sportGoal = true; }
else if (favSportResult == 'n') { sportGoal = false; }

printf("Is wife willing to wear a dress? (answer y or n)\n");
scanf_s(" %c", dressUse);
if (dressUse == 'y') { dressGoal = true; }
else if (dressUse == 'n') { dressGoal = false; }

printf("How much will the date cost?\n");
scanf_s("%d", &dateCost);
if (dateCost < 30) { dateGoal = true; }
else { dateGoal = false; }

if (mealGoal && fightGoal && sportGoal && dressGoal && dateGoal)
{
printf("You are a happy man. Enjoy your date.");
}
else {
printf("No date tonight. Better luck next time.");
}

system("pause");
}


For the first one. Basically I mean you do if(numMeal >= 4) assign true
else assign false[/code] the numMeal >= 4 returns true/false so you could just assign that.

For the third one you are missing the ampersands (&) the variables should be passed as a reference and not a local copy.

Also, using scanf for a single character I think is not as preferred as using getchar (keep in mind you are going to have to discard the newlines from when you hit enter)


Magnificent! This cleared it up and it works perfectly now. Thank you so much.
Topic archived. No new replies allowed.