Thread: Reading in a String of Chars

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    82

    Reading in a String of Chars

    I'm tring to read in a string of Characters and store them into an array, but I get this error.

    times_ten.c: In function `main':
    times_ten.c:17: parse error before '=' token


    Code:
    #include <stdio.h>
    #include "readIn.h"
    #include <string.h>
    #define BUFFER_SIZE =9
    
    int main()
    {
    char *n;
    
    printf("Enter a Hexadecimal >> ");
    scanf("&s", &*n);
    
    readIn(*n, BUFFER_SIZE); // LINE 17 
    
    return 0;
    }
    ~

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) Get rid of the = sign in your #define.

    [edit=further explanation]
    When you use #define, you are saying "replace THIS text with THIS".
    Code:
    #define FOO bar
    So everywhere you have FOO in your code, it replaces it with the text bar.

    In your case, it is replacing BUFFER_SIZE with =9.
    [/edit]

    2) You don't need the address of, or the dereference when using scanf on a pointer.
    3) Your pointer doesn't actually point to anything.

    Quzah.
    Last edited by quzah; 09-19-2004 at 10:45 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    I'm trying to pass the string into readIn, I'm having a little problem still. =/

    int readIn(char *theString, int limit)



    Code:
    #include <stdio.h>
    #include "readIn.h"
    #include <string.h>
    #define BUFFER_SIZE 9
    
    int main()
    {
    char *n;
    
    
    printf("Enter a Hexadecimal >> ");
    scanf("&c", &n);
    
    
    readIn(n, BUFFER_SIZE);
    
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Instead of using this...
    Quote Originally Posted by gqchynaboy
    Code:
    #define BUFFER_SIZE =9
    I think you need this:
    Code:
    	const int BUFFER_SIZE = 9;
    here I've just created a constant called BUFFER_SIZE wich has the value 9, so you can't modify it value anywhere else, like in a function call...

    It is 2 am here and English isn't my home languege so if the explanation is kinda confuse let me know I will try to explain better.

  5. #5
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    You're not using scanf right.

    Code:
    char n[BUFFER_SIZE];
    scanf("%s", n);
    readIn(n, BUFFER_SIZE];

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Knowing the code for readIn() would be helpful...my guess is that it accepts input from the user up to limit-1 bytes in length and stores it in theString. If that's the case I have no idea what you're even using scanf() for.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Nessarose
    You're not using scanf right.

    Code:
    char n[BUFFER_SIZE];
    scanf("%s", n);
    readIn(n, BUFFER_SIZE];

    Yeah but he is trying to assign data to a "pointer"?
    Code:
    #include<stdio.h>
    #include "readIn.h"		// <---- I don't have this so I will assume it is right
    #include<string.h>
    //#define BUFFER_SIZE 9 <---- What you need is a const variable
    
    int main ()
    {
    //char *n; Why the hell n is a pointer? And if it is a pointer it is a pointer for what?
    	char n;			//A char is a pointer by definition it points to the 0 adress of        //the string like char name[20]; 
    	//"name" is a pointer to the first position of the string   
    	const int BUFFER_SIZE = 9;	//It is what I think you want with that define
    
    	printf ("Enter a Hexadecimal >> ");
    	//scanf ("%c", &n); wrong
            scanf(%s,&n); //Dunno what n is supposed to do but...
    
    	readIn (n, BUFFER_SIZE);
    
    	return 0;
    }

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Quote Originally Posted by itsme86
    Knowing the code for readIn() would be helpful...my guess is that it accepts input from the user up to limit-1 bytes in length and stores it in theString. If that's the case I have no idea what you're even using scanf() for.

    Code:
    #include <stdio.h>
    #include "readIn.h"
    
    int readIn(char *theString, int limit)
    {
    int length =0;
    limit--;
    
    read(STDIN_FILENO, the String, 1);
    
    while((*theString != '\n') && (length < limit))
      {
      theString++;
      length++;
    
      read(STDIN_FILENO, theString, 1);
      }
    while (*theString !='\n')
      read(STDIN_FILENO, theSTring, 1);
      *theString= '\0';
    
    return length;
    
    }

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Right...you don't need the scanf() in your calling function then. Instead you want to declare an array of type char to pass to readIn() for storage.

    I'm not sure who wrote that readIn() function, but I'd suggest modifying it to use FILE streams.
    If you understand what you're doing, you're not learning anything.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Maragato
    Yeah but he is trying to assign data to a "pointer"?
    Code:
    #include<stdio.h>
    #include "readIn.h"		// <---- I don't have this so I will assume it is right
    #include<string.h>
    //#define BUFFER_SIZE 9 <---- What you need is a const variable
    No, you do not need a const variable. The #define is prefectly fine. Reread my explanation of #define above.
    No, he shouldn't read to a single character. They need one of the two options for their code to work:
    1) Use an array:
    Code:
    char n[BUFFER_SIZE]; /* Again, #define is perfectly fine here. */
    
    scanf( "%s", n );
    readIn( n, BUFFER_SIZE );
    2) Allocate memory for the pointer:
    Code:
    char *n;
    
    n = malloc( BUFFER_SIZE );
    scanf( "%s", n );
    readIn( n, BUFFER_SIZE );
    Though, I don't really know why they're using scanf at all, since they have some other "readIn" function they're using...

    [edit] Curses, foiled again! [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by quzah
    No, you do not need a const variable. The #define is prefectly fine. Reread my explanation of #define above.
    Oky I got it... But what would be the advantage of using #define instead ofthe const variable?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Maragato
    Oky I got it... But what would be the advantage of using #define instead ofthe const variable?
    In this case, there really isn't any advantage. Here they're interchangeable. Well, technicly, there is a slight advantage in that it doesn't require the extra memory requirement an actual variable would, because it's a pre-compile-time text subsitution. But other than that, they both will do the job just fine.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Quote Originally Posted by quzah
    No, you do not need a const variable. The #define is prefectly fine. Reread my explanation of #define above.
    No, he shouldn't read to a single character. They need one of the two options for their code to work:
    1) Use an array:
    Code:
    char n[BUFFER_SIZE]; /* Again, #define is perfectly fine here. */
    
    scanf( "%s", n );
    readIn( n, BUFFER_SIZE );
    2) Allocate memory for the pointer:
    Code:
    char *n;
    
    n = malloc( BUFFER_SIZE );
    scanf( "%s", n );
    readIn( n, BUFFER_SIZE );
    Though, I don't really know why they're using scanf at all, since they have some other "readIn" function they're using...

    [edit] Curses, foiled again! [/edit]

    Quzah.
    I did what you told me to and I still get errors

    times_ten.c: In function `main':
    times_ten.c:10: warning: assignment makes pointer from integer without a cast
    /tmp/cc4JTkQD.o(.text+0x4c): In function `main':
    : undefined reference to `readIn'
    collect2: ld returned 1 exit status

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by gqchynaboy
    I did what you told me to and I still get errors
    Post your latest code so we can see exactly what you're doing.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Code:
    #include <stdio.h>
    #include "readIn.h"
    #include <string.h>
    #define BUFFER_SIZE 9
    
    int main()
    {
    char *n;
    
    n=malloc(BUFFER_SIZE);
    
    printf("Enter a Hexadecimal >> ");
    scanf("%s",n);
    readIn(n, BUFFER_SIZE);
    
    return 0;
    }

    Code:
    #include <stdio.h>
    #include "readIn.h"
    
    int readIn(char *theString, int limit)
    {
    int length =0;
    limit--;
    
    read(STDIN_FILENO, the String, 1);
    
    while((*theString != '\n') && (length < limit))
      {
      theString++;
      length++;
    
      read(STDIN_FILENO, theString, 1);
      }
    while (*theString !='\n')
      read(STDIN_FILENO, theSTring, 1);
      *theString= '\0';
    
    return length;
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  5. IRC, reading the stream
    By Iyouboushi in forum C# Programming
    Replies: 6
    Last Post: 08-03-2006, 05:34 PM