Else block problem in C

I'm working through Chuck Allison's "Thinking in C" course. I'm using Visual C++ Express and making my source files .c, with almost everything working fine so far.

I'm at this Chapter 4 example that I can't get to work correctly. If you enter your age as over 80, you're asked if that's really true, and if it is, you're supposed to get "Congratulations!" This program gives me "I didn't think so" regardless of a 'Y' or 'y', and I can't figure out the reason.

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
/* 01-age.c
   This program makes comments about your age. */

#include <stdio.h>

int main()
{
    int stop;

    int age;
    puts("Enter your age: ");
    scanf_s("%d", &age);
    if (age < 20)
        puts("youth");
    else if (age < 40)
        puts("prime");
    else if (age < 60)
	puts("aches and pains");
    else if (age < 80)
        puts("golden");
    else
    {
	char really;
	printf("Are you really %d? ", age);
	fflush(stdout);
	scanf_s(" %c", &really);
	if (really == 'Y' || really == 'y')
	    puts("Congratulations!");
	else
	    puts("I didn't think so!");
    }

    scanf_s("%d", &stop);

    return 0;
}
Try doing this

if ((really == 'Y') || (really == 'y'))

atleast thats how its done in c++ :P
it works fine here.

The only thing I can think of that might be messing it up is line 26:

scanf_s(" %c", &really); <-

The space before the %c might be messing you up. Try changing to scanf_s("%c",&really);
Removing the space skips the user input and displays the choice for no then goes to the end of the program
Patryk: First I tried the added parentheses. That didn't work, so then I duplicated and made a .cpp file with. That didn't work either.

Disch: Allison's reason for the space being there before the %c is just in case the user enters white space before giving an answer. But according to that reasoning it should work even without the space, and it didn't. It would actually not allow any input (as just noted) and would directly go to writing "I didn't think so." I had previously tried that, although I tried it again upon the suggestion.

As with other guessing games I've had to play so far since picking this up, I tried taking away the _s in the scanf_s. It wasn't there in the example, but since the beginning I would get warnings from Visual C++ Express about it being scanf instead of scanf_s, and I haven't had any problems until now. For me, the program only works without it, with everything else as I had it.

Thanks for your replies.
I had no problems with the "Are you really?" condition, but I did have a few problems compiling right off the bat... the compiler complained about printf_s and some other things... I used both the gcc compiler and the one included in Visual C++ 5.0... Both were basically the same.

Below is the modified code with comments on what was changed and why. It compiles with no complaints on my system, try it on yours.

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
/* 01-age.c
   This program makes comments about your age. */

#include <stdio.h>
#include <conio.h>    /* Needed for getch() */

int main()
{
    int age;

    fputs("Enter your age: ", stdout);    /* Use fputs("prompt", stdout) if you don't want a newline after the prompt */
    scanf("%d", &age);    /* I don't even know what scanf_s() is, but scanf() will work, and it's the standard */
    if (age < 20)
        puts("youth");
    else if (age < 40)
        puts("prime");
    else if (age < 60)
	puts("aches and pains");
    else if (age < 80)
        puts("golden");
    else
    {
	char really;
	printf("Are you really %d? ", age);
	fflush(stdout);
	scanf(" %c", &really);
	if (really == 'Y' || really == 'y')
	    puts("Congratulations!");
	else
	    puts("I didn't think so!");
    }

    fputs("\nPress any key to exit...", stdout);    /* Notify that the program is done */
    getch();    /* Use getch() to pause, instead of using scanf() to assign an int that will never be used*/
    return 0;
}
Disch, since you said it worked fine for you, perhaps you have a different compiler and perhaps I should change mine. If I wouldn't have to change operating systems right now, what one should I use? PM me if you prefer.
Eh. got it working in C++.
Dont know why xD
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
#include <iostream>

int main()
{
	int age;
	std::cout << "Enter your age.\n";
	std::cin >> age;
	if (age < 20)
		std::cout << "Youth\n";
	else if (age < 40) {
		std::cout << "Prime\n";
	}
	else if (age < 60) {
		std::cout << "Aches and Pains\n";
	}
	else if (age < 80) {
		std::cout << "Golden\n";
	}
	else 
		std::cout << "are you really 80?\n";
	char really;
		std::cin >> really;
		if ((really == 'Y') || (really == 'y'))
			std::cout << "Congratulations!\n";
		else
			std::cout << "I didn't think so\n";

	return (0);
}

Lol
Last edited on
Thanks, Ryan, that works great. And I appreciate the new syntax that I haven't been properly introduced to yet.
You can use the GNU gcc compiler (Free, open source) instead of the Microsoft one if you want...

You'll need to download MinGW from here: http://sourceforge.net/projects/mingw/files/Automated%20MinGW%20Installer/MinGW%205.1.6/MinGW-5.1.6.exe/download

Once you have it all installed, you can just open a CMD window, then cd to the folder with your source in it and type 'gcc -o age.exe 01-age.c' (-o switch specifies the output name of the compiled executable, otherwise it defaults to a.exe)

You can also type 'gcc --help' for more switches to the gcc command, but you probably won't need to. Also, use the '-O2' switch for compiling "release" version exe's (rather than debug versions), it does some optimizations while compiling to speed up the program and reduce it's size. Using the 'strip yourprogram.exe' command will also greatly reduce the size of the compiled executable.
Last edited on
Faerie wrote:
And I appreciate the new syntax that I haven't been properly introduced to yet.

Do notice that you posted a C program, and from your original post, it looks like you're learning C, not C++. The program patryk posted is C++.
I clearly stated at the top of my post i got it working C++, no?
Yes, but that doesn't help the OP that much if you found a fixer for it in C++ and he's coding in C.

-Albatross
I should be getting to C++ in a week or two, so the exposure wouldn't hurt. As I started learning C++, it was suggested that I learn C first if I was also interested in learning Java. The "new syntax" I was referring to were statements Ryan used to replace my guesswork novice statements for keeping the console open to see the results.
I wasn't directing it to him. I just felt like posting it. Maybe I was off-topic in the wrong place and wrong time xD
it was suggested that I learn C first


Sigh. Don't learn C first, learn C++ first.
http://www.parashift.com/c++-faq-lite/how-to-learn-cpp.html#faq-28.2
Well, like the article says, if your goal is ultimately to learn C++, then you may as well start with C++, not only because it's easier that way, but also cause C++ can, and often does, use C statements and syntax, so you'll learn C as well while you learn C++.

But ya know, up to you.
firedraco +1. It takes less effort to code in C when you know C++ than to code in C++ when you know C, owing to the fact that C++ is a superset of C.

-Albatross
So many of you are amazingly helpful.

I think the reasons given for learning C first was to appreciate how C++ avoids "the dark corners" of C. The "Thinking in C" course is probably more like an overview anyway that isn't comprehensive enough to get me into any bad habits. But I see that Stroustrup also had this to say at http://www2.research.att.com/~bs/learn.html:

I don't know C or C++, should I learn C first? No. Learn C++ first. The C subset of C++ is easier to learn for C/C++ novices and easier to use than C itself. The reason is that C++ provides better guarantees than C (stronger type checking). In addition, C++ provides many minor features, such as the `new' operator, that are notationally more convenient and less error-prone than their C alternatives. Thus, if you plan to learn C and C++ (or just C++) you shouldn't take the detour through C. To use C well, you need to know tricks and techniques that aren't anywhere near as important or common in C++ as they are in C. Good C textbooks tends (reasonably enough) to emphasize the techniques that you will need for completing major projects in C. Good C++ textbooks, on the other hand, emphasizes techniques and features that lead to the use of C++ for data abstraction and object-oriented programming. Knowing the C++ constructs, their (lower-level) C alternatives are trivially learned (if necessary).
AFAIK, you need to specify the buffer size of input value when it is character or string while using scanf_s().
Use scanf instead, in which you do not need to specify the buffer size.

In your code, try using scanf_s("%c", &really, 1);
Topic archived. No new replies allowed.