string

This is a discussion on string within the C++ Programming forums, part of the General Programming Boards category; Hi, Is it possible for a user to input a string without allocating memory space for the string? I try ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    42

    string

    Hi,

    Is it possible for a user to input a string without allocating memory
    space for the string?
    I try something like this:

    [code]
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <math.h>
    #include <iostream.h>
    #include <iomanip>
    #include <ctype.h>
    #include <string.h>


    main()
    {

    char *text;


    printf("\nEnter text: ");
    scanf("%s", pt);

    printf("%s", pt);

    return (0);

    }
    [\code]

    but i got this error when i run:
    "The instruction at "0x00401f24" referenced memory at "0xffffffcc". The memoty could not be "written"."

    It compile and build without any problems.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    No, unless if your overwriting other allocated space, there is no other way the data containing the string could be accessed without it being allocated. But it's possible to write something like
    char c; scanf("%c", &c);. The user may then type in some very long string but only the first character of the string will be read in.

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    42
    Mmm ok, thanks guys.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is it possible for a user to input a string without allocating memory space for the string?
    It depends on how pedantic you are. If you mean without the programmer explicitly allocating memory then it is possible:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string name;
    
      std::cout<<"Name: ";
      if ( std::getline ( std::cin, name ) )
        std::cout<< name <<std::endl;
    }
    Or you could use an array and work with C-style strings:
    Code:
    #include <iostream>
    #include <cstdio>
    
    int main()
    {
      char name[BUFSIZ];
    
      std::cout<<"Name: ";
      if ( std::cin.getline ( name, sizeof name ) )
        std::cout<< name <<std::endl;
    }
    In neither of these are you explicitly calling new to get anonymous memory. If you mean not allocate memory at all in any part of the program to hold the string, not without great difficulty. On the other hand, if you're willing to allow for a single character in memory:
    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
      char c;
    
      cout<<"Name: ";
      while ( cin.get(c) && c != '\n' )
        cout<< c;
      cout<<endl;
    }
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    42

    Talking

    Thanks, Prelude! It's a great help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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