Results 1 to 8 of 8
I have just begun with C programming. The book I have teaches the scanf at the dead end and that's pretty stupid. I have written a small, stupid program in ...
- 12-30-2007 #1Linux User
- Join Date
- Jun 2007
- Posts
- 458
A total stupid n00b in C
I have just begun with C programming. The book I have teaches the scanf at the dead end and that's pretty stupid. I have written a small, stupid program in c for calculating the percentage from a ratio. Before you burst out laughing at any stupid mistake, please tell me where I am bugging around:
Code:#include <stdio.h> int main() { unsigned int a, b; float percent; printf("Enter the ratio you want to convert to percentage. Enter the first value and press ENTER\n"); scanf("%u ", &a); printf(": "); scanf("%u\n", &b); printf("You provided with %u : %u.\n", a, b); percent = (a * 100) / b; printf("The percentage is &f%%.\n", percent); return 0; }"When you have nothing to say, say nothing."
- 12-30-2007 #2you have &f rather than %f and you must escape the second %Code:
printf("The percentage is &f%%.\n", percent);
Code:printf("The percentage is %f\%.\n", percent);
- 12-30-2007 #3
coopstahl is correct about his first point, but not quite correct about the second one. Here's the offending character:
The purpose for putting a backslash ("\") before a particular character in a C string is so the C compiler will take the character after the backslash as being literally what is desired; and that is what coopstahl has done here. But that's not what's needed, because the C compiler will already take a percent sign "as is" within a string, so it's never necessary to use a backslash there. What's needed, though, is for the standard I/O package, at runtime, to print a literal percent sign. To do that, you place two percent signs in a row in the C string. Both of them are actually in the string when the standard I/O package gets them; the package, seeing them, then knows to output a single percent sign.Code:printf("The percentage is %f\%.\n", percent);
Let's try an experiment. Let's try this with:
- nothing extra before the percent sign;
- a backslash before the percent sign; and
- an extra percent sign before the percent sign.
My prediction is that placing a backslash before the percent sign will have no effect, and in either case we'll have the wrong output. Also, adding another percent sign will fix this.
Consider this program:
At least on my machine, the output is this:Code:#include <stdio.h> #include <string.h> #include <unistd.h> int main(void) { char comment1[]="We're about to display the format strings themselves:\n"; char comment2[]="We're about to use the format strings:\n"; char format1 []="The percentage is %f%.\n"; char format2 []="The percentage is %f\%.\n"; char format3 []="The percentage is %f%%.\n"; float alpha; alpha=3.5; write(1,comment1,strlen(comment1)); write(1,format1,strlen(format1)); write(1,format2,strlen(format2)); write(1,format3,strlen(format3)); printf("The length of each string is as follows:\n"); printf("%d %d %d\n", strlen(format1), strlen(format2), strlen(format3) ); if(strcmp(format1,format2)==0) { printf("The first two format strings are identical.\n"); } else { printf("The first two format strings differ.\n"); } write(1,comment2,strlen(comment2)); printf(format1,alpha); printf(format2,alpha); printf(format3,alpha); return 0; } /* main() */
Hope this helps.Code:We're about to display the format strings themselves: The percentage is %f%. The percentage is %f%. The percentage is %f%%. The length of each string is as follows: 23 23 24 The first two format strings are identical. We're about to use the format strings: The percentage is 3.500000%.0 The percentage is 3.500000%.0 The percentage is 3.500000%.
--
Bill
Old age and treachery will overcome youth and skill.
- 12-31-2007 #4Linux User
- Join Date
- Jun 2007
- Posts
- 458
Thanks for correcting me in that but the trouble lies somewhere else. The expected output is as follows with user input in bold:
----------------XX------------------
Enter the ratio you want to convert to percentage. Enter the first value and press ENTER
5 : 10
You provided with 5 : 10
The percentage is 50%
----------------XX------------------
But when the first statement appears, I enter 5 and then the terminal gives no output and no prompt. I kill the program there. I mentioned that I have not gone through scanf but I am the impatient reader. Please help. Maybe there's something wrong with variable choice."When you have nothing to say, say nothing."
- 12-31-2007 #5
- First, don't use scanf(). It's difficult to use it robustly; that is, it's difficult to write code that behaves correctly no matter what (perhaps incorrect) input the user may enter. Instead, use fgets() to get each line, strip the final line feed off the line (making sure that the final character is a line feed, obviously), and use sscanf() to parse the input on that line.
- Second, read the documentation (about scanf(), for example) more slowly and patiently. Raise questions about it if you don't understand it. This may sound harsh, but we are unpaid volunteers, and most of us just don't have time to spoon feed material that's already there. I hope this does not sound offensive.
--
Bill
Old age and treachery will overcome youth and skill.
- 01-01-2008 #6Linux User
- Join Date
- Jun 2007
- Posts
- 458
I don't take it offensive but still, thanks for introducing fgets and sscanf to me. And thanks for making me a bit more patient. Impatience is making me so confused and nothing else.
"When you have nothing to say, say nothing."
- 01-04-2008 #7Just Joined!
- Join Date
- Jan 2008
- Posts
- 2
I don't know if you still have problems, but I can tell you this about scanf. It looks for a user input of exactly the format of its parameter string. so:
scanf("%u ",&a);
would wait for the user to type an unsigned integer followed by a space character. And:
scanf("%u\n",&b);
would wait for the user to type an unsigned integer followed by an enter (linefeed).
To solve your problem above, you should put either one of those two:
scanf("%u : %u\n",&a,&b);
or
printf("Enter numerator:\n");
scanf("%u\n",&a);
printf("Enter denominator:\n");
scanf("%u\n",&b);
hope this helps you understand scanf() (although it is quite an obsolete function, especially considering the C++ standard IO libraries).
Mike.
- 01-04-2008 #8
I'm not sure that obsoletion is precisely the problem with scanf(). It certainly isn't made obsolete by anything new in C++, because usamamuneeb's objective was to learn C, not C++. LordMikado is quite correct to show this example of how C++ is far more nifty than C in many ways, but it cannot be said that C++ has made C "obsolete". There is much new code being written in C. The Linux kernel is written in C. C++ is simply a newer, niftier language, as are Perl, python, and ruby.
And as far as I can tell, scanf() isn't made obsolete by fgets() and sscanf() either, because they were all pretty much introduced at the same time. They're all described, for example, in the first edition of Kernighan and Ritchie's The C Programming Language. That came out in 1978. That's so old that when ANSI C came out, that was the occasion for the second edition of The C Programming Language in 1988.
No, the real reason to avoid scanf() is that it's just plain clumsy to use. It's clumsy mainly because it gets out of phase in interactive situations. Cosider this program:
When you run it, at least under GNU/Linux, you get something like this. The user input is shown in color.Code:#include <stdio.h> int main(void) { unsigned int a; unsigned int b; for(;;) { printf("Enter first value:\n"); scanf("%u\n",&a); printf("Enter second value:\n"); scanf("%u\n",&b); printf("got %u %u\n",a,b); } return 0; } /* main() */
and so on.Code:Enter first value: 111 222 Enter second value: 333 got 111 222 Enter first value: 444 Enter second value: 555 got 333 444 Enter first value: 666 Enter second value: 777 got 555 666 Enter first value: 888 <control D> Enter second value: got 777 888 Enter first value:
Note that the program received the correct data, but delayed processing it until things got confusing.
Further, because of the delay in processing the input, catching invalid input (for example, if you expect an input value to be between 500 and 600, and complain and get a new value if it isn't) can be really confusing for the user of the program.
And that's only for getting one value per line. Getting more than one value per line, in my experimentation today, gave more disconcerting results than I'd want to bore you with.--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote