Thread: Scanf function with circumflex

  1. #1
    Registered User
    Join Date
    Feb 2011
    Location
    New Delhi
    Posts
    5

    Scanf function with circumflex

    Hello everyone,
    I am unable to run the following code...any help will be most welcome

    facing issues with the while statement.
    unable to detect EOL character

    I can tackle this problem through the other route as well but I want see whats wrong about this method.

    Code:
    # include <stdio.h>
    
    main()
    {
    
    int i=0;
    char myarray[80];
    
    scanf(" [^\n]",myarray);
    
    while ((myarray[i]!='\n') || (myarray[i]==""))
    {
    	printf("\n text%d : %c",i,&myarray[i]);
    	++i;
    
    }
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is this program supposed to do?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Using a compiler that can tell you about printf/scanf mess-ups would be a big bonus.
    Code:
    $ gcc -Wall foo.c
    foo.c:4: warning: return type defaults to ‘int’
    foo.c: In function ‘main’:
    foo.c:9: warning: too many arguments for format
    foo.c:11: warning: comparison between pointer and integer
    foo.c:11: warning: comparison with string literal results in unspecified behavior
    foo.c:13: warning: format ‘%c’ expects type ‘int’, but argument 3 has type ‘char *’
    foo.c:18: warning: control reaches end of non-void function
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Location
    New Delhi
    Posts
    5
    Here, I am just trying to enter a line of text in an array terminated by a new line character. then I go on to print that line element by element of the array (piece meal method) till the time I encounter new line character.

    Actually I am writing a bigger more complex program and its a part of that.
    As such this program is not supposed to do anything...

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Try this one:

    Code:
    #include <stdio.h>
    
    int main(void) {
       int i=0;
       char myarray[80];
    
       scanf("%[^\n]",myarray);
    
       while (myarray[i]!='\0')
       {
          printf("\n text %d: %c",i,myarray[i]);
          ++i;
       }
       putchar('\n');
       return 0;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Regardless, it's still just as unsafe as gets().
    It would be better to use fgets().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    ^^^ Definitely!

    scanf() is for formatted input, not user input. It's just used for user input while a student is getting started.

    I'm sure fgets() should be used more, even in such classes though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating an scanf function inside an function
    By anserudd in forum C Programming
    Replies: 4
    Last Post: 03-25-2011, 09:19 AM
  2. The scanf function
    By Mcoroklo in forum C Programming
    Replies: 5
    Last Post: 09-20-2009, 04:36 AM
  3. the scanf function
    By timhxf in forum C Programming
    Replies: 5
    Last Post: 01-08-2007, 01:19 PM
  4. scanf function
    By Roaring_Tiger in forum C Programming
    Replies: 6
    Last Post: 04-22-2003, 07:02 PM
  5. scanf function
    By Roaring_Tiger in forum Windows Programming
    Replies: 1
    Last Post: 04-22-2003, 05:57 PM

Tags for this Thread