View Single Post
Old 06-05-2005, 11:49 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Code:
scanf("userresponse");
This isn't a valid statement, if you'd read up on the scanf()From the C standard library
Quote:
int scanf(const char* format, ...);
Performs formatted input conversion, reading from stdin according to format format. The function returns when format is fully processed. Returns number of items converted and assigned, or EOF if end-of-file or error occurs before any conversion. Each of the arguments following format must be a pointer. Format string may contain:
  • blanks and tabs, which are ignored
  • ordinary characters, which are expected to match next non-white-space of input
  • conversion specifications, consisting of:
    • %
    • (optional) assignment suppression character "*"
    • (optional) maximum field width
    • (optional) target width indicator:
      • h: argument is pointer to short rather than int
      • l: argument is pointer to long rather than int, or double rather than float
      • L: argument is pointer to long double rather than float
    • conversion character:
      • d: decimal integer; int* parameter required
      • i: integer; int* parameter required; decimal, octal or hex
      • o: octal integer; int* parameter required
      • u: unsigned decimal integer; unsigned int* parameter required
      • x: hexadecimal integer; int* parameter required
      • c:characters; char* parameter required; white-space is not skipped, and NUL-termination is not performed
      • s: string of non-white-space; char* parameter required; string is NUL-terminated
      • e,f,g: floating-point number; float* parameter required
      • p: pointer value; void* parameter required
      • n: chars read so far; int* parameter required
      • [...]: longest non-empty string from specified set; char* parameter required; string is NUL-terminated
      • [^...]: longest non-empty string not from specified set; char* parameter required; string is NUL-terminated
      • %: literal %; no assignment
As you can see, the scanf() function needs to have your format which you'd like the user to input, something like:
Code:
int base, height;
printf("Please enter base in form of B:H: ");
fflush(stdout);
if(2 > scanf("%d:%d", &base, &height))
{
    printf("Error, you didn't provide BxH\n");
    return -1;
}
/* then do whatever calculation son base and height */
At the very least this would work, but for any other type of input, like strings or chars, you might want to use some other readin handling, since *scanf() functions are prone to introducing errors, because they don't provide any size checking of what they read.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote