Thread: I thought a string could hold an infinite number of characters?

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Exclamation I thought a string could hold an infinite number of characters?

    Why doesnt it? I was told a long time ago that strings could hold an infinite number of characters. I never used strings until recently because they were a bit difficult to work with as opposed to character arrays. I was bored so i decided to make a little program in like 15 minutes; a diary/journal one. That just took the input buffer and saved it to a text file with the date. Well when i was testing it and writing random sentances it only let me write a certain amount than it wouldnt let me write anymore. I was under the impression you could write as much as you wanted and that the string would expand to fit your needs. Any help would be greatly appreciated. Thanks!

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    let's see some code yo
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    A string cannot hold infinite number of characters, it is bound by the memory on your machine. That's probably not the problem in your case, though.

    How do you know it stops letting you write? Does the console stop letting you type (which would be a problem with the console, not string)? Does the file only contain part of your sentence? Does it let you write additionaly entries in your diary, or does the file just stop allowing more data at a certain point?

  4. #4
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    I think this is more of a console limitation rather than one of the string class. I think the only thing you could do would be to move to something such as the Win32 API and make a giant edit box.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I was told a long time ago that strings could hold an infinite number of characters.
    Do you have any concept of what "infinity" is? If a string could hold an infinite number of characters, then the memory on your computer would have to be infinite. Do you believe that to be the case?

    I never used strings until recently because they were a bit difficult to work with as opposed to character arrays.
    In fact, the opposite is true: character arrays are much more difficult to work with. They are so difficult to work with that some very smart people went to the trouble of designing and programming the string class.

    I was under the impression you could write as much as you wanted and that the string would expand to fit your needs.
    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main()
    {
    	string str = "hello ";
    	str = str + "world";
    	str = str + " goodbye world";
    	
    	cout<<str<<endl;
    	
    	return 0;
    }
    Try that with char arrays.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Ok ill just make it up on the spot cause the program is on my moms laptop. Here it goes, it was prolly like this:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        while(1)
        {
            //universal vars
            char basefileloc[100];
            basefileloc[0]='/0';
            strcat(basefileloc, "C:/Journal Program/");
            //menu
            system("cls");
            cout<<"(1) New Entry\n";
            cout<<"(2) Read Entry\n";
            cout<<"(3) Exit\n\n";
            //get choice
            int mchoice;
            cout<<"Choice: ";
            cin>>mchoice;
            cin.ignore();
            if(mchoice==1)
            {
                //new
                char newdate[15];
                cin.getline(newdate, 15);
                //combine file location
                strcat(basefileloc, newdate);
                strcat(basefileloc, ".txt");
                //get string
                string newentry;
                getline(cin, newentry);
                ofstream fout(basefileloc);
                fout<<newentry;
                fout.close();
                continue;
            }
            if(mchoice==2)
            {
                //read
                char readdate[15];
                cout<<"Date of Entry: ";
                cin.getline(readdate, 15);
                strcat(basefileloc, readdate);
                strcat(basefileloc, ".txt");
                ifstream fin(basefileloc);
                if(!fin)
                {
                    cout<<"File doesnt Exist!";
                    cin.ignore();
                    break;
                }   
                char ch;
                while(fin.get(ch))
                {
                    cout<<ch;
                }
                fin.close();
                cout<<"\n\nDONE\n\n";
                cin.ignore();
                continue;     
            }
            if(mchoice==3)
            {
                //exit
                break;
            }
            else{
                //incorrect choice
                cout<<"\n\nIncorrect Choice!\n";
                cin.ignore();
                continue;
            }
        }
        return 0;
    }
    It may not work i keep getting a strange warning but thats the jist of the code.
    Last edited by Junior89; 11-29-2005 at 06:46 PM.

  7. #7
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Junior89
    Ok ill just make it up on the spot cause the program is on my moms laptop. Here it goes, it was prolly like this:
    Code:
    //...
    
            char basefileloc[100];
    
    // ...
    That is a character array not a string. In C++ when we refer to a "string", we're usually talking about the std::string class.

    do a google search on the differences between c char arrays and C++ strings.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  8. #8
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    It doesnt let me write anymore. I hit the keys but it already stopped writing.

    Quote Originally Posted by "7stud'
    Do you have any concept of what "infinity" is? If a string could hold an infinite number of characters, then the memory on your computer would have to be infinite. Do you believe that to be the case?
    No crap, i knew that, i think it isnt difficult to see what i meant. It can hold as much as my machine can, no duh. I meant i dont have to define it like a char array.

    For my general encoding purposes it was easier to use arrays that could be mathematically combined in order to encode them. You cannot XOR a string without turning it into a vector, i believe. I dont know vectors.

    And first of all 7stud, that code does me no good, i can combine things all day long, im using user input not hardcoded stuff. And secondly i could do that with character arrays. With maybe 1 or 2 extra lines of code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
         char combined[5];
         char arrayA[25]="Hello ";
         char arrayB[25]="World!";
    
         strcat(combined, arrayA);
         strcat(combined, arrayB);
    
         cout<<combined<<endl;
    
         return 0;
    }

  9. #9
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Quote Originally Posted by ChaosEngine
    That is a character array not a string. In C++ when we refer to a "string", we're usually talking about the std::string class.

    do a google search on the differences between c char arrays and C++ strings.
    That part was for the filename not for the actual contents of the file.

  10. #10
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    
    int main()
    {
    
      
    	ofstream out;
    	ifstream in;
    
    	string lot_o_junk;
    
    	out.open("temp.txt");
    	if ( out.fail() ) return 1;
    
    	for (int i = 0; i < 10000; i++)
    		out << "abcdefghijlkmnopqrstuvwxyz";
    	//10,000 X 26 = 260,000 characters
    
    	out.close();
    
    	in.open("temp.txt");
    	if ( in.fail() ) return 1;
    
    	in >> lot_o_junk; //This one string now has 260,000 character in it
    	//How many do you want in one string? You can add another 0 in the
    	//for loop and get 2,600,000 char in the one string (but it will
    	//take a long time for it to start outputing (function of computer
    	//memmory speed, ect
    
    
    	cout << lot_o_junk;
    
    	in.close();
    
    
    
    
    return 0;
    }
    So, if you are trying to put less than 100,000 char in it and it is not working, your computer is either really slow or your code is wrong.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> It doesnt let me write anymore. I hit the keys but it already stopped writing.
    That sounds like a limitation of your console window, and has nothing to do with your program code. Your program doesn't become involved until you hit enter.

    >> You cannot XOR a string without turning it into a vector, i believe.
    Sure you can. You can do anything with a string that you can do with a character array. If you want "infinite" size, use the C++ string class, then figure out how to get it to do what you want.

  12. #12
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Well that still doesnt solve the problem. I want the USER to input the information. Not to have an if() loop put in the alphabet 10,000 times.

  13. #13
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Thanks DAVED, is there anyway to change this limitation, i have yet to try it on this 'good' computer. Also my compiler (dev) wont let me do string ^ string. Any ideas? Thanks!

    PS- Can i use string[x] where x is the number corresponding to the postion of a character?

    Example:
    Code:
    ...
    for(int i=0; i<50; i++)
    {
         string3[i]=string1[i]^string2[i];
    }
    Thanks again!

  14. #14
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    The for loop was just so I could get lots of data fast. I had no desire to type out 260,000 characters. It works the same from the keyboard input.

  15. #15
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    ok im trying out the program on my newer computer and i dont see a limit on the console, thanks for explaining that daved, i appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Replies: 2
    Last Post: 05-05-2002, 01:38 PM