Quote:
After typing in the number 1000, the information in "stdin" would look like this, for example. 1000\n I used "\n" to represent the new-line character. Your program is reading the number like this. Code:
scanf( "%d", &FAH ); \n Later, your program wants to read a Y or N answer. When your program calls "fgets" to read a line, or "scanf" to read a character, it will read the new-line character (without waiting). What I was getting at is that the whole not waiting problem is caused by how the program reads the number, since it does not read the entire input line. So it is not enough to just use "fgets" in one place (reading Y/N) and not the other (reading the number). If you want the program to wait for input, then you need to make sure that all previous lines that were entered have been completely read in, including the new-line at the end of each line. The "scanf" function is not very good for reading input because it does not know nor care about new-line characters or input lines. The new-line characters are the same as spaces as far as "scanf" is concerned. These are all the same as far as "scanf" is concerned. 1\n N\n 10\n Y\n 1 N\n 10 Y\n 1 N 10 Y\n 1\n N\n 10 Y\n That wouldn't really matter much, except that the program is also displaying messages to prompt for the input. If the data that "scanf" wants to read has already been typed in before the prompt, then the program won't wait for any input after displaying the prompt message. You could do something like this. Code:
scanf( "%d ", &FAH ); 1000N\n When "scanf" tries to read white-space after the number, it will fail, since "N" is not white-space. That leaves the following in the input buffer. N\n And the next call to "scanf" or "fgets" will read the remainder of the line, just as if it had been typed in as a separate line. The solution is to always use "fgets" to read entire input lines and then use "sscanf" to determine if the information that was entered is valid. That way you know that the program is not reading left-over information from some other input line. |
so it would require an entire rework of the script to get what i want. ok.
just to make sure im following what you are saying. for my request for the FAH value at the start of the script i would have to do something like: Code:
fgets( FAH, sizeof(FAH), stdin ); |
Quote:
Code:
fgets( BUFFER, sizeof(BUFFER), stdin ); |
progress:
Code:
#include<stdio.h> Code:
ssma-imac:ENG-3211 ssma$ gcc -o hw_1_5.v2 hw_1_5.v2.c |
im reading up on the following links. they do explain a bit more about fgets and what not. still not sure what is wrong with the while statement preventing the script from restarting.
http://faq.cprogramming.com/cgi-bin/...&id=1043284385 http://faq.cprogramming.com/cgi-bin/...&id=1043284392 http://faq.cprogramming.com/cgi-bin/...&id=1043284392 i now see why you like fgets so much v scanf. |
Take a look at the last 10 lines. The "while" statement that you had at the end was checking for invalid input. To check for a yes answer you would need something similar to the "while" statement at the end.
Code:
int main(void) |
Quote:
(I see Erik_FL beat me to this answer. Well done.) |
thanks guys. question about the {}'s. i notice they are not nested. it is like math were you only need to have the same number of { as you need } or is it not like in BASH were you need a { to open a section (function) and a } to close that same section.
just so that i can understand what the code is saying, let me see what i get. sscanf ( BUFFER, "%ls%*ls", ANS ) that reads the BUFFER from above and puts in long string(?) to the variable ANS is that right? !=1, not 100% on this, but reading beryllos this is anything other then YyNn? so kind of like the bash expression d != were the directory does not exist? strchr( "YyNn", *ANS ) this takes the standard characters of YyNn and puts them into the string ANS? not sure what the == NULL does, but im guessing its like 1>&2 in bash? In the 2nd while statement (still not sure how the {} work to get that there with the do statement at the top of the script) this allows for any version of Yy to fill the string ANS. Kind of like BASH [Yy]? please tell me what i have right and wrong. Thanks. |
I can answer some questions, not all:
Quote:
Code:
sscanf( BUFFER, "%1s%*1s", ANS ) != 1 Code:
|| Code:
strchr( "YyNn", *ANS ) == NULL Here are a few resources that may be helpful (not necessarily the best; just the first ones I found today that look good): strchr function http://www.codecogs.com/reference/co...g.h/strchr.php relational operators (table including many programming languages) http://en.wikipedia.org/wiki/Relatio...onal_operators braces Sorry, I can't find a clear introduction to the usage of braces in C. Perhaps an introductory textbook would help. |
Quote:
Code:
if ( i == 5 ) Code:
i = 5; Code:
while ( i-- > 0 ) Quote:
If you get rid of the "%*1s" then it just looks for the first non-blank character and does not care if there are more characters after that. Code:
sscanf ( BUFFER, "%1s", ANS ) On a different subject, something that might not be obvious is these two expressions mean the same thing in C when "ANS" is a character array (string). Both expressions produce an 8-bit character value. Code:
*ANS Code:
ANS Quote:
C does not have I/O redirection statements like a scripting language. The default output always goes to a file called "stdout", for example, and "stdout" is usually set by the operating system or shell that runs the C program. Quote:
The pairing of the braces is important. The entire statement is really like this. Code:
do { /* statements... */ } while ( *ANS == 'Y' || *ANS == 'y' ); Code:
do { do { /* statements... */ } while ( /* not valid */ ); } while ( *ANS == 'Y' || *ANS == 'y' ); Code:
*ANS = 'y'; |
All times are GMT -5. The time now is 06:31 PM. |