March 5th, 2013, 10:00 AM
Problem with a "learning level" program
i tried this program on visual 2010 c++ express.
this is a simple C program.
printf("enter ordered quantity: ");
printf("enter quantity in stock: ");
printf("enter credit status");
printf("supply him:%d and intimate him when the balance:%d will be shipped.\n",stockqty,(ordqty-stockqty));
1.the output should ask user to enter ordqty, stockqty, credit.
2.the output should check if credit(credit status) is Y or N and should output appropriate printf after going through IF
but, this program only asks to input ordqty, stockqty and credit status but even before you can type credit status it exits.
--i can write this program in other ways to make it work but i wanted to know what is the problem with this program? as it will help me learn "what i should not try while writing programs".
please help me, I am learning C and don't have money to join any coaching classes.
March 5th, 2013, 10:29 AM
We have a commonly-asked questions thread where I'm sure this is addressed -- that thread is there for a very good purpose. So, for the ... I forget how many times I've had to explain this here:
tells scanf to read in the next character, which will usually be the newline character from the last call so scanf. Instead, you want scanf to skip all white space (eg, spaces, tabs, newlines) that precede the character entered by the user, so you need to tell that to scanf:
Note the space inserted before the percent sign. That tells scanf to ignore any and all white space that it finds there -- the same basic rule holds true for any literal character you may put in the format string, such as commas or hyphens.
Thoroughly review your textbook's discussion of scanf. Also read the documentation on scanf; if you don't have help files or man pages loaded on your system, then Google on man page scanf. There is a lot you can do with that format string, but you need to learn how.
Use code tags when you post code listings here!
[code] insert your formatted code here [/code]
Last edited by dwise1_aol; March 5th, 2013 at 10:32 AM.
March 5th, 2013, 10:57 AM
many many thanks to you dwise1_aol,
Originally Posted by dwise1_aol
I'm sorry I didn't check other posts first as I wasn't sure what is wrong in my program and what I should be looking for.
I'm learning C from "Let Us C". The book didn't explain anything much on scanf, except that it is used to take values from keyboard and its syntax. So, maybe I think I should refer to some other book. Can you suggest me a book on C that pretty much explains everything?
Thanks to you again. :)
March 5th, 2013, 02:10 PM
I'm not familiar with your book. For that matter, since I learned C decades ago, I am very unfamiliar with the beginning books that are out there. It could be the case that yours is the least bad one, so advising you to jump ship could be premature and could land you in a worse situation. Certainly, if your book had advised you to use scanf in that exact manner to read in a single character after another scanf call had been made, then that would be a mark against it. One mistake could be indicative of other problems, or it could just be one mistake. If you like the tutorial style of the book, it could still be of use, though it would be a good idea to supplement it with other books.
One "beginning" book I am familiar with is the perennial The C Programming Language, 2nd ed for ANSI C by Kernighan and Ritchie, the pioneering developers of C (so they quite literally "wrote the book on C"; the first edition of their book was at first the only book and virtually the only standard for the language). The 2nd edition is only about 260 pages long, but it is also fairly densely packed with information which could be difficult to digest for a beginner who is also not very familiar with programming in other languages. Almost every C programmer has had this book in his bookcase at some time. Still in print and available on amazon.com for $48.99 USD.
Specific to your problem in this thread, they do not address it directly, but rather on page 158 in Table 7-2 ("BASIC SCANF CONVERSIONS"), for c they write:
A different solution to the same problem. And do you see what I mean by the information being densely packed?
Another very good supplement to have is from the Schaum Outlines series of study guides: Programming With C, 2nd ed by Byron Gottfried. I have found it to be a very good reference. It is where I found that solution to your problem, though several years ago -- it is maybe the second most frequent beginner's problem. His explanations of arrays and multi-dimensional arrays are also quite good, as is his treatment of pointers (which includes a list of several different kinds of declarations that use pointers and what exactly they mean). I rarely recommend books, but I do recommend this one. Still in print and available on amazon.com for less than $20 USD.
You should also get into the habit of reading the documentation on the Standard C Library functions. I don't know what compiler/development system (AKA "IDE" for "Integrated Development Environment") you are using. If it came with help files, then you should use those help files to learn more about the functions you are using. Historically, in UNIX and also in Linux the standard form of documentation has been the manual pages called "the man pages", which are accessible from the shell's command line via the man command. If you don't have any such documentation installed on your computer, then you can Google the several thousands of man pages that have been posted on the Web as I described in my previous message.
Make it a habit when you learn a new library function to read the help file's/man pages' entry for that function. There is a lot of information about the function parameters, return value, and function behavior contained in that documentation that most authors will not include in their books. And it is an even better idea to go back later on and re-read a function's documentation.
You should also read through the "commonly asked C/C++ questions" sticky thread at the top of this forum. It might not make much sense to you now, but it will at least expose you to potential problems you will have in the future, at which point you can then return to that thread to get the solution.
Last edited by dwise1_aol; March 5th, 2013 at 02:12 PM.
March 5th, 2013, 03:14 PM
Let Us C is crap; any book that advocates for the use of Turbo C (a nearly 20-year-old compiler) is not worth the paper it's printed on.
I ♥ ManiacDan & requinix
This is a sig, and not necessarily
a comment on the OP:
don't be a help vampire
March 5th, 2013, 04:49 PM
Study this code and ask any questions if this is what you desire.
puts("Enter ordqty and stockqty. Then enter credit status.\n");
if(ordqty <= stockqty)
else if(credit == 'Y' && ordqty > stockqty)
printf("supply him:%d and intimate him when the balance:%d will be shipped.\n",stockqty,ordqty-stockqty);