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.
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.
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.
/* 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");
elseif (age < 40)
puts("prime");
elseif (age < 60)
puts("aches and pains");
elseif (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.
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.
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.
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++.
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);