Like Tree1Likes
  • 1 Post By Salem

Scanf function with circumflex

This is a discussion on Scanf function with circumflex within the C Programming forums, part of the General Programming Boards category; Hello everyone, I am unable to run the following code...any help will be most welcome facing issues with the while ...

  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
    22,274
    What is this program supposed to do?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    32,834
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    32,834
    Regardless, it's still just as unsafe as gets().
    It would be better to use fgets().
    Adak likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 10:19 AM
  2. The scanf function
    By Mcoroklo in forum C Programming
    Replies: 5
    Last Post: 09-20-2009, 05: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, 08:02 PM
  5. scanf function
    By Roaring_Tiger in forum Windows Programming
    Replies: 1
    Last Post: 04-22-2003, 06:57 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21